Question

In: Computer Science

Write an implementation of the set class, with associated iterators using a binary search tree. Add...

Write an implementation of the set class, with associated iterators using a binary search tree. Add to each node a link to the parent node. (C++)

Solutions

Expert Solution

// C++ program to demonstrate insert operation

// in binary search tree with parent pointer

#include<bits/stdc++.h>

struct Node

{

    int key;

    struct Node *left, *right, *parent;

};

// A utility function to create a new BST Node

struct Node *newNode(int item)

{

    struct Node *temp = new Node;

    temp->key = item;

    temp->left = temp->right = NULL;

    temp->parent = NULL;

    return temp;

}

// A utility function to do inorder traversal of BST

void inorder(struct Node *root)

{

    if (root != NULL)

    {

        inorder(root->left);

        printf("Node : %d, ", root->key);

        if (root->parent == NULL)

          printf("Parent : NULL \n");

        else

          printf("Parent : %d \n", root->parent->key);

        inorder(root->right);

    }

}

/* A utility function to insert a new Node with

   given key in BST */

struct Node* insert(struct Node* node, int key)

{

    /* If the tree is empty, return a new Node */

    if (node == NULL) return newNode(key);

    /* Otherwise, recur down the tree */

    if (key < node->key)

    {

        Node *lchild = insert(node->left, key);

        node->left = lchild;

        // Set parent of root of left subtree

        lchild->parent = node;

    }

    else if (key > node->key)

    {

        Node *rchild = insert(node->right, key);

        node->right = rchild;

        // Set parent of root of right subtree

        rchild->parent = node;

    }

    /* return the (unchanged) Node pointer */

    return node;

}

// Driver Program to test above functions

int main()

{

    /* Let us create following BST

              50

           /     \

          30      70

         / \    / \

       20   40 60   80 */

    struct Node *root = NULL;

    root = insert(root, 50);

    insert(root, 30);

    insert(root, 20);

    insert(root, 40);

    insert(root, 70);

    insert(root, 60);

    insert(root, 80);

    // print iNoder traversal of the BST

    inorder(root);

    return 0;

}

Output :

Node : 20, Parent : 30 
Node : 30, Parent : 50 
Node : 40, Parent : 30 
Node : 50, Parent : NULL 
Node : 60, Parent : 70 
Node : 70, Parent : 50 
Node : 80, Parent : 70 

Related Solutions

In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class...
In C++ Consider the binary search tree implementation in file bintree.cp a)Add to the TreeNode class a pointer to the parent node. Modify the insert and erase functions to properly set those pointers. b)Then define a TreeIterator class that contains a pointer to a TreeNode and has member functions get and next. i)The iterator’s get member function should return the data value of the node to which it points. ii)The iterator’s next member function should find the next element in...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print...
1. Modify a binary search tree implementation code (that AVL tree code depends on) to print out the data value for each node it encounters in the insert operation. Remember that the module AVL tree code gets part of its functionality from the BST type, since an AVL tree is a binary search tree, but adds some additional functionality. 2. Add code to the main method to meet the following requirements: (a) Create an AVL tree to hold names stored...
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT