Question
Im trying to Implement a Binary Search Tree (BST) class.Why is it not working? c++ bst.h class Node { public: int key; Node* left; Node*
Im trying to Implement a Binary Search Tree (BST) class.Why is it not working? c++
bst.h
class Node { public: int key; Node* left; Node* right; Node(int data) { key = data; //left =NULL; //part of original code that caused error //right = NULL; //part of original code that caused error } };
class BST { public: BST(); ~BST(); /* insertion */ void insert(int data); Node* insert(Node* node, int data); /* search */ Node* search(int key); Node* search(Node* node, int key); /* delection */ void remove(int key); Node* remove(Node* node, int key); Node* leftmostNode(Node* node); /* in-order traversal */ void inorder(); void inorder(Node* node); private: Node* root; };
---------------------------------------------------------------------------------
bst.cpp
#include
using namespace std;
BST::BST() { root = NULL; } BST::~BST() { ////////////////// root=NULL; } void BST::insert(int data) { ////////////////// root = insert(root, data); }
Node* BST::insert(Node* node, int data) { //////////////////
node->left = NULL; node->right = NULL; if(node == NULL) { node = new Node(data); } else if(node->key > data) { node->left = insert(node->left, data); } else if(data > node->key) { node->right = insert(node->right, data); } return node; }
Node* BST::search(int key) {////////////////// return search(root,key); }
Node* BST::search(Node* node, int key) { ////////////////// if (root == NULL || root->key == key) return root; if (root->key < key) return search(root->right, key); return search(root->left, key); }
void BST::inorder() {////////////////// cout << "Elements in the IN order: " << endl; inorder(root); // start in-order traversal from the root } void BST::inorder(Node* node) { ////////////////// if (node != NULL) { inorder(node->left); cout << node->key<< endl; inorder(node->right); }
} void BST::remove(int key) { ////////////////// root= remove(root,key); }
Node* BST::remove(Node* node, int key) { ////////////////// if (node == NULL) return node; if (node->key > key) node->left = remove(node->left, key); else if (key > node->key) node->right = remove(node->right, key); else {//case1: node with only one child or no child if (node->left == NULL) { Node *temp = node->right; delete node; return temp; } else if (node->right == NULL)//case2: with only one child or no child { Node *temp = node->left; delete node; return temp; } Node* temp = leftmostNode(node->left);//case3: with two children node->key = temp->key; node->left = node->left, temp->key; } return node; } Node* BST::leftmostNode(Node* node) {////////////////// Node* current = node; /* loop down to find the leftmost leaf */ while (current->right != NULL) { current = current->right; return current; } }
---------------------------------------------------------------------
bstclient.cpp
#include
using namespace std;
int main() { /* BST bst; bst.insert(50); bst.insert(30); bst.insert(20); bst.insert(40); bst.insert(70); bst.insert(60); bst.insert(80); */ BST bst; cout << "done" << endl; bst.insert(80); cout << "done" << endl; cout << (bst.search(80) ==NULL ? "Element not fount," : "element found.") << endl; /* bst.inorder(); cout << (bst.search(50) == NULL ? "Element not found." : "Element found.") << endl; bst.remove(40); bst.remove(50); bst.inorder(); cout << (bst.search(50) == NULL ? "Element not found." : "Element found.") << endl; */ return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started