Question

In: Computer Science

In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....

In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below

#include <iostream>
#include "BSTNode.h"
using namespace std;

#ifndef BST_H_
#define BST_H_

class BST {

public:

        BSTNode *root;
        int size;

        BST() {
                root = NULL;
                size = 0;
        }

        ~BST() {
                if (root != NULL)
                        deepClean(root);
        }

        BSTNode *search(int key) { // complete this method
        }

        BSTNode *insert(int val) { // complete this method
        }

        bool remove(int val) { // complete this method
        }

private:

        void removeLeaf(BSTNode *leaf) { // complete this method
        }

        void removeNodeWithOneChild(BSTNode *node) { // complete this method
        }

        static BSTNode *findMin(BSTNode *node) {
                if (NULL == node)
                        return NULL;
                while (node->left != NULL) {
                        node = node->left;
                }
                return node;
        }

        static BSTNode *findMax(BSTNode *node) {
                if (NULL == node)
                        return NULL;
                while (node->right != NULL) {
                        node = node->right;
                }
                return node;
        }

        void print(BSTNode *node) {
                if (NULL != node) {
                        node->toString();
                        cout << " ";
                        print(node->left);
                        print(node->right);
                }
        }

        static int getHeight(BSTNode *node) {
                if (node == NULL)
                        return 0;
                else
                        return 1 + max(getHeight(node->left), getHeight(node->right));
        }
    
    static void deepClean(BSTNode *node) {
        if (node->left != NULL)
            deepClean(node->left);
        if (node->right != NULL)
            deepClean(node->right);
        delete node;
    }

public:

        int getTreeHeight() {
                return getHeight(root);
        }

        void print() {
                print(root);
        }

        int getSize() {
                return size;
        }
};

#endif

Solutions

Expert Solution

#include <iostream>

#include "BSTNode.h"

using namespace std;

#ifndef BST_H_

#define BST_H_

class BST

{

public:

    BSTNode *root;

    int size;

    BST()

    {

        root = NULL;

        size = 0;

    }

    ~BST()

    {

        if (root != NULL)

            deepClean(root);

    }

    BSTNode *search(BSTNode *root, int key)

    {

        //check case 0, where root can be null or root is the key

        if (root == NULL || root->val == key)

            return root;

        //check case 1, where key is greater tha root's val

        if (root->val < key)

            return search(root->right, key);

        //check case 2, where key is less than root's val

        if (root->val > key)

            return search(root->left, key)

    }

    BSTNode *insert(BSTNode *root, int val)

    { // complete this method

        if(root==NULL) // check case 0 , where root is empty

    {

        treeNode *temp;

        temp=new treeNode;

        temp -> val = val;

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

        return temp;

    }

    if(val >(root->val))// check case 1 , where root val is less than val

    {

        root->right = Insert(root->right,val);

    }

    else if(val < (root->val))// check case 1 , where root val is greater than val

    {

        root->left = Insert(root->left,val);

    }

    /* Else there is nothing to do as the val is already in the tree. */

    return root;

    }

    bool remove(BSTNode *root, int val)

    { // complete this method

        BSTNode *temp;

        if (root == NULL)

        {

            cout << "Element Not Found";

        }

        else if (val < root->val)

        {

            root->left = remove(root->left, val);

        }

        else if (val > root->val)

        {

            root->right = remove(root->right, val);

        }

        else

        {

            /* Now We can remove this root and replace with either minimum element

        in the right sub tree or maximum element in the left subtree */

            if (root->right && root->left)

            {

                /* Here we will replace with minimum element in the right sub tree */

                temp = findMin(root->right);

                root->val = temp->val;

                /* As we replaced it with some other root, we have to removee that root */

                root->right = remove(root->right, temp->val);

            }

            else

            {

                /* If there is only one or zero children then we can directly

            remove it from the tree and connect its parent to its child */

                temp = root;

                if (root->left == NULL)

                    root = root->right;

                else if (root->right == NULL)

                    root = root->left;

                free(temp); /* temp is longer required */

            }

        }

        return true;

    

    }

private:

    void removeLeaf(BSTNode *root)

    { // complete this method

        if (root == NULL)// chech case 0, where root can be empty

            cout<<"there is no BST";

        if (root->left == NULL && root->right == NULL)// chech case 1, where root is the only data in BST

        {

            free(root);

            cout<<"there is only root in BST, hence removed";

        }

        // else recursively delete in left and right

        // subtrees.

        root->left = removeLeaf(root->left);

        root->right = removeLeaf(root->right);

        cout<<"Leaf Nodes Removed";

    }

    void removeNodeWithOneChild(BSTNode *root)

    { // complete this method

        if (root->left == NULL)

        {

            BSTNode  *temp = root->right;

            free(root);

            cout<<"Removed successfully";

        }

        else if (root->right == NULL)

        {

            BSTNode *temp = root->left;

            free(root);

            cout<<"Removed successfully";

        }

    }

    static BSTNode *findMin(BSTNode *node)

    {

        if (NULL == node)

            return NULL;

        while (node->left != NULL)

        {

            node = node->left;

        }

        return node;

    }

    static BSTNode *findMax(BSTNode *node)

    {

        if (NULL == node)

            return NULL;

        while (node->right != NULL)

        {

            node = node->right;

        }

        return node;

    }

    void print(BSTNode *node)

    {

        if (NULL != node)

        {

            node->toString();

            cout << " ";

            print(node->left);

            print(node->right);

        }

    }

    static int getHeight(BSTNode *node)

    {

        if (node == NULL)

            return 0;

        else

            return 1 + max(getHeight(node->left), getHeight(node->right));

    }

    static void deepClean(BSTNode *node)

    {

        if (node->left != NULL)

            deepClean(node->left);

        if (node->right != NULL)

            deepClean(node->right);

        removee node;

    }

public:

    int getTreeHeight()

    {

        return getHeight(root);

    }

    void print()

    {

        print(root);

    }

    int getSize()

    {

        return size;

    }

};

#endif

----------summary------------

I assume your BSTNode data structure is like {

int val; ( if it is not 'val' then replace it with your keyword in above code )

BSTNode* left;

BSTNode* right

}


Related Solutions

In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below. CPP code also provided for reference, nothing to be changed on cpp code. #include #include "BSTNode.h" using namespace std; #ifndef BST_H_ #define BST_H_ class BST { public: BSTNode *root; int size; BST() { root = NULL; size = 0; } ~BST() { if (root != NULL) deepClean(root); } BSTNode *search(int key) { // complete this method } BSTNode *insert(int val) {...
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below #include <iostream> #include "BSTNode.h" using namespace std; #ifndef BST_H_ #define BST_H_ class BST { public: BSTNode *root; int size; BST() { root = NULL; size = 0; } ~BST() { if (root != NULL) deepClean(root); } BSTNode *search(int key) { // complete this method } BSTNode *insert(int val) { // complete this method } bool remove(int val) { // complete this...
Implement two methods, find() and replace() relating to Binary Search Trees. Both of these methods must...
Implement two methods, find() and replace() relating to Binary Search Trees. Both of these methods must be recursive functions. The signatures for the functions must be: /* This method takes a tree node and a key as an argument and returns the tree node if the key is found and returns null if the key is not found. */ BST find(BST T, char key) /* This method takes a tree node and a key as an argument and inserts the...
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...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order...
In MobaXterm, with the use of the binary search tree. Implement four operations of insert, in-order traversal, preorder traversal, and find. Please separate the code in the four parts and explain in detail what is happening. Also, if you can please basic C language. If not, then I understand. Thank you for your time. The test cases are 'm', 'd', 'g', 'r', 'p', 'b', and 'x'. Output: Enter choice (lower case is also acceptable) --- (I)nsert, (F)ind, (Q)uit: i Enter...
Considering the following different search trees, binary search trees, AVL trees, red-black trees, and others. Compare...
Considering the following different search trees, binary search trees, AVL trees, red-black trees, and others. Compare their advantages and disadvantages, running times, etc
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
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT