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. 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) { // 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
testCorrectness.cpp
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "BST.h"
#include "Hashing.h"
using namespace std;

int arrayIns[] = { 11, 12, 15, 17, 12, 19, 4, 5, 11, 19, 20, 32, 77, 65, 66, 88,
                99, 10, 8, 19, 15, 66, 11, 19 };
const int numInsert = sizeof(arrayIns) / sizeof(int);

int searchArray[] = { 29, 3, 19, 27, 12, 34, 4, 5, 19, 20, 32, 45, 37, 25, 99,
                25, 8, 24, 12, 16 };
const int numSearch = sizeof(searchArray) / sizeof(int);

int deleteArray[] = { 16, 12, 15, 5, 17, 19, 4, 5, 19, 20, 32, 17, 19, 39, 99,
                10, 8, 19, 15, 21 };
const int numDelete = sizeof(deleteArray) / sizeof(int);

const int cleanUp[] = { 11, 77, 65, 66, 88 };
const int numCleanUp = sizeof(cleanUp) / sizeof(int);

const int TABLE_SIZE = 7;

void printArray(int *A, int len) {
        if (0 == len)
                cout << "[]";
        else {
                cout << "[";
                for (int i = 0; i < len - 1; i++) {
                        if (A[i] == INT_MAX)
                                cout << "infty, ";
                        else
                                cout << A[i] << ", ";
                }
                if (A[len - 1] == INT_MAX)
                        cout << "infty]";
                else
                        cout << A[len - 1] << "]";
                cout << endl;
        }
}

void printList(list &A) {
        if (0 == A.size())
                cout << "[]" << endl;
        else {
                list::iterator it;
                cout << "[";
                for (it = A.begin(); next(it) != A.end(); ++it)
                        cout << *it << ", ";
                cout << *it << "]";
                cout << endl;
        }
}

void printList(list *A) {
        if (0 == A->size())
                cout << "[]" << endl;
        else {
                list::iterator it;
                cout << "[";
                for (it = A->begin(); next(it) != A->end(); ++it)
                        cout << *it << ", ";
                cout << *it << "]";
                cout << endl;
        }
}

//HASHING -------------------------------------------
Hashing *testHashing() {
        cout << "****************** Test Hashing Correctness ******************"
                        << endl << endl;
        Hashing *hChain = new Hashing(TABLE_SIZE);

        cout << "Inserting the following numbers: ";
        printArray(arrayIns, numInsert);

        for (int i = 0; i < numInsert; i++) {
                hChain->insert(arrayIns[i]);
        }

        cout << endl << "*** Hash Table Structure (after insertion) ***" << endl;
        int size = 0;
        for (int i = 0; i < TABLE_SIZE; i++) {
                cout << "Slot " << i << ": ";
                printList(hChain->getList(i));
                size += hChain->getList(i)->size();
        }
        cout << endl << "Size of hash table: " << size << endl;

        cout << "\n*** Searching Hash Table ***" << endl;
        list foundList;
        list notFoundList;

        for (int i = 0; i < numSearch; i++) {
                int val = searchArray[i];
                bool found = hChain->search(val);
                if (found)
                        foundList.push_back(val);
                else
                        notFoundList.push_back(val);
        }
        cout << "Found: ";
        printList(foundList);
        cout << "Did not find: ";
        printList(notFoundList);
        cout << endl << "*** Deleting Hash Table ***" << endl;

        list deleteList;
        list deleteNotFoundList;
        for (int i = 0; i < numDelete; i++) {
                int val = deleteArray[i];
                bool deleted = hChain->remove(val);
                if (deleted)
                        deleteList.push_back(val);
                else
                        deleteNotFoundList.push_back(val);
        }
        cout << "Deleted: ";
        printList(deleteList);
        cout << "Did not find: ";
        printList(deleteNotFoundList);

        cout << endl << "*** Hash Table Structure (after deletion) ***" << endl;
        size = 0;
        for (int i = 0; i < TABLE_SIZE; i++) {
                cout << "Slot " << i << ": ";
                printList(hChain->getList(i));
                size += hChain->getList(i)->size();
        }
        cout << endl << "Size of hash table: " << size << endl;
        return hChain;
}

//BST
BST *testBST() {
        cout << "\n****************** Test BST Correctness ******************"
                        << endl << endl;

        BST *bst = new BST();
        cout << "Inserting the following numbers: ";
        printArray(arrayIns, numInsert);

        for (int i = 0; i < numInsert; i++) {
                bst->insert(arrayIns[i]);
        }

        cout << endl << "*** BST Structure (after insertion) ***" << endl;
        bst->print();
        cout << endl << endl << "Size of BST: " << bst->getSize() << endl;

        cout << "\n*** Searching BST ***" << endl;
        list foundList;
        list notFoundList;

        for (int i = 0; i < numSearch; i++) {
                int val = searchArray[i];
                if (bst->search(val) != NULL)
                        foundList.push_back(val);
                else
                        notFoundList.push_back(val);
        }
        cout << "Found: ";
        printList(foundList);
        cout << "Did not find: ";
        printList(notFoundList);
        cout << endl << "*** Deleting BST ***" << endl;

        list deleteList;
        list deleteNotFoundList;
        for (int i = 0; i < numDelete; i++) {
                int val = deleteArray[i];
                bool deleted = bst->remove(val);
                if (deleted)
                        deleteList.push_back(val);
                else
                        deleteNotFoundList.push_back(val);
        }
        cout << "Deleted: ";
        printList(deleteList);
        cout << "Did not find: ";
        printList(deleteNotFoundList);

        cout << endl << "*** BST Structure (after deletion) ***" << endl;
        bst->print();
        cout << endl << endl << "Size of BST: " << bst->getSize() << endl;
        return bst;
}

static void cleanTest(Hashing *hashing, BST *bst) {
        cout << "\n****************** Clean up ******************" << endl;
        for (int i = 0; i < numCleanUp; i++) {
                hashing->remove(cleanUp[i]);
                bst->remove(cleanUp[i]);
        }
        int size = 0;
        for (int i = 0; i < TABLE_SIZE; i++)
                size += hashing->getList(i)->size();

        cout << "\nSize of hash table: " << size << endl;
        cout << "Size of BST: " << bst->getSize();
        delete hashing;
        delete bst;
}

int main() {
        cleanTest(testHashing(), testBST());
        return 0;
}

BSTnode.h

#include <iostream>
using namespace std;

#ifndef BSTNODE_H_
#define BSTNODE_H_

class BSTNode {

public:

        int value;
        BSTNode *left, *right, *parent;

        BSTNode(int val) {
                // each node is inserted as a leaf
                value = val;
                left = NULL;
                right = NULL;
                parent = NULL;
        }

        void toString() {
                if (parent == NULL)
                        cout << "<" << value << ", null>";
                else
                        cout << "<" << value << ", " << parent->value << ">";
        }
};

#endif

Solutions

Expert Solution

Implemented all the functions asked.. Do tell me in comments if you have any doubt and if the solution is helpful, pls do upvote!!!

*****************************************

Code-

BSTNode* removeNodeWithOneChild_helper(BSTNode* root, BSTNode* node){
if(root==NULL){
return NULL;
}
  
if(root->data==node->data){
if (root->left!=NULL){
root->left=NULL;
}
if(root->right!=NULL){
root->right=NULL;
}
  
}
  
if(root->data<node->data){
root->right=removeNodeWithOneChild_helper(root->right,node);
}
  
else{
root->left=removeNodeWithOneChild_helper(root->left,node);
}
  
return root;
  
  
  
}


void removeNodeWithOneChild(BSTNode *node){
  
  
removeNodeWithOneChild_helper(root,node);
  
}


BSTNode* removeLeaf_helper(BSTNode* root,BSTNode* leaf){
  
if(root==NULL){
return NULL;
}
  
if(root->data==leaf->data && root->left==NULL && root->right==NULL){
return NULL;
}
  
  
if(leaf->data<root->data){
root->left=removeLeaf_helper(root->left,leaf);
  
  
}
  
else{
root->right=removeLeaf_helper(root->right,leaf);
}
  
return root;
}

void removeLeaf(BSTNode* leaf){
  
removeLeaf_helper(root,leaf);
  
}


BSTNode* insert_helper(BSTNode* root,int val){

if(root==NULL){
BSTNode* new_node=new BSTNode(val);
return new_node;
}
  
  
else{
if(root->data==val){
return root;
}
else if(root->data<val){
root->right=insert_helper(root->right,val);
}else{
root->left=insert_helper(root->left,val);
}
}
return root;

}

BSTNode* insert(int val){
return insert_helper(root,val);
}


BSTNode* search_helper(BSTNode* root,int key){
  
if(root==NULL){
return root;
}
  
if(root->data==key){
return root;
}
  
if(key<root->data){
return search_helper(root->left,key);
}
  
return search_helper(root->right,key);
}

BSTNode* search(int key){
  
return search_helper(root,key);
  
}


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 #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...
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