Question

In: Computer Science

1. Add the function int getNodeLevel (T value) to the Binary Search Tree class provided below....

1. Add the function int getNodeLevel (T value) to the Binary Search Tree class provided below. The function should return the level of the node value in the Binary Search Tree.

Hint:

Set currentNode to root

Initialize level = 0

while currentNode != Null

         if currentNode element = value return level

         else move to either the right child or the left child of the currentNode, update the loop, and repeat the loop

Function should return -1 if value is not in the tree

#include <iostream>
#include <vector>
using namespace std;

//****** The Node class for the Binary Search Tree ******
template<typename T>
class Node
{
public:
        Node();
        Node(T e, Node<T>* r, Node<T>* l);
        T element;          // holds the node element
        Node<T>* right;
        Node<T>* left;
};

//============ implementation of the constructors of the Node
template<typename T>
Node<T>::Node() { right = left = NULL; }

template<typename T>
Node<T>::Node(T e, Node<T>* r, Node<T>* l) { element = e; right = r; left = l; }

//===============  Binart Searct Tree (BST) class ===========

template<typename T>
class BTree
{
public:
        BTree() { root = NULL; }
        BTree(Node<T>* rt) { root = rt; }
        void BSTInsert(T value);
        void BSTRemove(T value);
        Node<T>* & getRoot() { return root; }  // returns the pointer to the root
        Node<T>*  BSTsearch(T value);

private:
        Node<T>* root;   // a pointer to the root of the tree
};

template<typename T>
Node<T>*  BTree<T>::BSTsearch(T value)
{
        // set cur to the tree root, traverse down the tree to find the element.
        // Do not use the root, if you move the root the tree will be lost.
        Node<T>* cur = root;
        while (cur != NULL)
        {
                if (value == cur->element)
                        return cur;
                else if (value < cur->element)
                        cur = cur->left;
                else
                        cur = cur->right;
        }
        return NULL;
}



// traverse down the tree and inserts at the bottom of the tree as a new leaf
template<typename T>
void BTree<T>::BSTInsert(T value)
{
        Node<T>* newNode = new Node<T>(value, NULL, NULL); // dynamically create a node with the given value
        if (root == NULL)    //Empty tree, fisrt node.
                root = newNode;
        else
        {
                Node<T>* r = root;
                while (r != NULL)
                {
                        if (value < r->element)
                        {
                                if (r->left == NULL)
                                {
                                        r->left = newNode; //insert the node 
                                        r = NULL;  // end the loop.
                                }
                                else
                                        r = r->left; // keep going to the left child
                        }
                        else
                        {
                                if (r->right == NULL)
                                {
                                        r->right = newNode;
                                        r = NULL;
                                }
                                else
                                        r = r->right;

                        }
                }
        }
}

//Three cases to consider when removing an element from a BST  
template<typename T>
void BTree<T>::BSTRemove(T value)
{
        Node<T>* parent = NULL;  // Need to track the parent of the node to be deleted
        Node<T>* current = root; // current will point to the node to be deleted
        //find the node to be removed
        while (current != NULL && current->element != value)
        {
                if (value < current->element)
                {
                        parent = current; current = current->left;
                }
                else
                {
                        parent = current; current = current->right;
                }
        }

        if (current != NULL) // The node to be deleted is found
        {
                //Case A : the node is a leaf node
                if (current->left == NULL && current->right == NULL)
                {
                        if (parent->right == current)
                                parent->right = NULL;
                        else
                                parent->left = NULL;
                        delete current;
                }

                //Case B: the node has two children
                // Must find the smalles element in the right subtree of the node, which is
                //found by going to the right child of the node then all the way to the left.
                else if (current->left != NULL && current->right != NULL)
                {
                        Node<T>* succ = current->right; // go to the right child of the node to be removed
                        parent = current;     // initialize parent node
                        if (succ->left == NULL)  // right child of the node has no left child
                        {
                                parent->right = succ->right;
                                current->element = succ->element;
                                delete succ;
                        }
                        else  //otherwise keep going left
                        {
                                while (succ->left != NULL)      // then find the smallest element in the left subtree
                                {
                                        parent = succ;
                                        succ = succ->left;
                                }
                                current->element = succ->element; //Replace the node to be deleted by the succ node
                                parent->left = NULL;  // skip the succ node
                                delete succ;
                        }
                }
                else  // Case C: Node has one child
                {
                if (root->element == value)    //if the node is the root node treat differently
                {
                        cout << "here\n";
                        if (root->right != NULL)
                                root = root->right;
                        else
                                root = root->left;
                }
                else    // a non root node with one child to be removed
                {
                        if (current->left != NULL)
                                parent->left = current->left;
                        else
                                parent->right = current->right;
                }
                delete current;
                }
        }
}

int main()
{
    BTree<int> bst;
    bst.BSTInsert(29);
    bst.BSTInsert(50);
    bst.BSTInsert(78);
    bst.BSTInsert(39);
    bst.BSTInsert(21);

    return 0;
}

Solutions

Expert Solution

//C++ program

#include<iostream>
using namespace std;

template<typename T>
class Node
{
public:
        Node();
        Node(T e, Node<T>* r, Node<T>* l);
        T element;          // holds the node element
        Node<T>* right;
        Node<T>* left;
};

//============ implementation of the constructors of the Node
template<typename T>
Node<T>::Node() { right = NULL;left = NULL; }

template<typename T>
Node<T>::Node(T e, Node<T>* r, Node<T>* l) { element = e; right = r; left = l; }

//=============== Binart Searct Tree (BST) class ===========

template<typename T>
class BTree
{
public:
        BTree() { root = NULL; }
        BTree(Node<T>* rt) { root = rt; }
        void BSTInsert(T value);
        void BSTRemove(T value);
        Node<T>* & getRoot() { return root; } // returns the pointer to the root
        Node<T>* BSTsearch(T value);
       int getNodeLevel (T value) ;
private:
        Node<T>* root;   // a pointer to the root of the tree
};

template<typename T>
Node<T>* BTree<T>::BSTsearch(T value)
{
        // set cur to the tree root, traverse down the tree to find the element.
        // Do not use the root, if you move the root the tree will be lost.
        Node<T>* cur = root;
        while (cur != NULL)
        {
                if (value == cur->element)
                        return cur;
                else if (value < cur->element)
                        cur = cur->left;
                else
                        cur = cur->right;
        }
        return NULL;
}

// traverse down the tree and inserts at the bottom of the tree as a new leaf
template<typename T>
void BTree<T>::BSTInsert(T value)
{
        Node<T>* newNode = new Node<T>(value, NULL, NULL); // dynamically create a node with the given value
        if (root == NULL)    //Empty tree, fisrt node.
                root = newNode;
        else
        {
                Node<T>* r = root;
                while (r != NULL)
                {
                        if (value < r->element)
                        {
                                if (r->left == NULL)
                                {
                                        r->left = newNode; //insert the node
                                        r = NULL; // end the loop.
                                }
                                else
                                        r = r->left; // keep going to the left child
                        }
                        else
                        {
                                if (r->right == NULL)
                                {
                                        r->right = newNode;
                                        r = NULL;
                                }
                                else
                                        r = r->right;

                        }
                }
        }
}

//Three cases to consider when removing an element from a BST
template<typename T>
void BTree<T>::BSTRemove(T value)
{
        Node<T>* parent = NULL; // Need to track the parent of the node to be deleted
        Node<T>* current = root; // current will point to the node to be deleted
        //find the node to be removed
        while (current != NULL && current->element != value)
        {
                if (value < current->element)
                {
                        parent = current; current = current->left;
                }
                else
                {
                        parent = current; current = current->right;
                }
        }

        if (current != NULL) // The node to be deleted is found
        {
                //Case A : the node is a leaf node
                if (current->left == NULL && current->right == NULL)
                {
                        if (parent->right == current)
                                parent->right = NULL;
                        else
                                parent->left = NULL;
                        delete current;
                }

                //Case B: the node has two children
                // Must find the smalles element in the right subtree of the node, which is
                //found by going to the right child of the node then all the way to the left.
                else if (current->left != NULL && current->right != NULL)
                {
                        Node<T>* succ = current->right; // go to the right child of the node to be removed
                        parent = current;     // initialize parent node
                        if (succ->left == NULL) // right child of the node has no left child
                        {
                                parent->right = succ->right;
                                current->element = succ->element;
                                delete succ;
                        }
                        else //otherwise keep going left
                        {
                                while (succ->left != NULL)      // then find the smallest element in the left subtree
                                {
                                        parent = succ;
                                        succ = succ->left;
                                }
                                current->element = succ->element; //Replace the node to be deleted by the succ node
                                parent->left = NULL; // skip the succ node
                                delete succ;
                        }
                }
                else // Case C: Node has one child
                {
                if (root->element == value)    //if the node is the root node treat differently
                {
                        cout << "here\n";
                        if (root->right != NULL)
                                root = root->right;
                        else
                                root = root->left;
                }
                else    // a non root node with one child to be removed
                {
                        if (current->left != NULL)
                                parent->left = current->left;
                        else
                                parent->right = current->right;
                }
                delete current;
                }
        }
}

template<typename T>
int BTree<T>::getNodeLevel (T value) {
   Node<T> *currentNode = root;
   int level = 0;
   while (currentNode != NULL){
       if(currentNode->element == value) return level;
       else if(currentNode->element < value)
           currentNode = currentNode->right;
       else
           currentNode = currentNode->left;
       level = level + 1;
   }
   return -1;
}

int main()
{
    BTree<int> bst;
    bst.BSTInsert(29);
    bst.BSTInsert(50);
    bst.BSTInsert(78);
    bst.BSTInsert(39);
    bst.BSTInsert(21);

    return 0;
}


Related Solutions

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++)
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...
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: 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 i) { // implement } 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;...
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
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has...
Binary Search Trees with Lazy Deletion Implement binary search tree class with lazy deletion that has TreeNode as nested class in Java. Design the class, TreeNode to have following class variables: int key; // All Keys are in the range 1 to 99 TreeNode leftChild; TreeNode rightChild; boolean deleted; Your program method must have routines to do the following operations. 1. insert //Should insert a new element to a leaf node. If new element is aduplicatethen do nothing. If the...
​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.
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT