Last active
October 15, 2024 16:34
-
-
Save harish-r/a7df7ce576dda35c9660 to your computer and use it in GitHub Desktop.
Binary Search Tree Implementation in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
** Binary Search Tree implementation in C++ | |
** Harish R | |
*/ | |
#include<iostream> | |
using namespace std; | |
class BST { | |
struct node { | |
int data; | |
node* left; | |
node* right; | |
}; | |
node* root; | |
node* makeEmpty(node* t) { | |
if(t == NULL) | |
return NULL; | |
{ | |
makeEmpty(t->left); | |
makeEmpty(t->right); | |
delete t; | |
} | |
return NULL; | |
} | |
node* insert(int x, node* t) | |
{ | |
if(t == NULL) | |
{ | |
t = new node; | |
t->data = x; | |
t->left = t->right = NULL; | |
} | |
else if(x < t->data) | |
t->left = insert(x, t->left); | |
else if(x > t->data) | |
t->right = insert(x, t->right); | |
return t; | |
} | |
node* findMin(node* t) | |
{ | |
if(t == NULL) | |
return NULL; | |
else if(t->left == NULL) | |
return t; | |
else | |
return findMin(t->left); | |
} | |
node* findMax(node* t) { | |
if(t == NULL) | |
return NULL; | |
else if(t->right == NULL) | |
return t; | |
else | |
return findMax(t->right); | |
} | |
node* remove(int x, node* t) { | |
node* temp; | |
if(t == NULL) | |
return NULL; | |
else if(x < t->data) | |
t->left = remove(x, t->left); | |
else if(x > t->data) | |
t->right = remove(x, t->right); | |
else if(t->left && t->right) | |
{ | |
temp = findMin(t->right); | |
t->data = temp->data; | |
t->right = remove(t->data, t->right); | |
} | |
else | |
{ | |
temp = t; | |
if(t->left == NULL) | |
t = t->right; | |
else if(t->right == NULL) | |
t = t->left; | |
delete temp; | |
} | |
return t; | |
} | |
void inorder(node* t) { | |
if(t == NULL) | |
return; | |
inorder(t->left); | |
cout << t->data << " "; | |
inorder(t->right); | |
} | |
node* find(node* t, int x) { | |
if(t == NULL) | |
return NULL; | |
else if(x < t->data) | |
return find(t->left, x); | |
else if(x > t->data) | |
return find(t->right, x); | |
else | |
return t; | |
} | |
public: | |
BST() { | |
root = NULL; | |
} | |
~BST() { | |
root = makeEmpty(root); | |
} | |
void insert(int x) { | |
root = insert(x, root); | |
} | |
void remove(int x) { | |
root = remove(x, root); | |
} | |
void display() { | |
inorder(root); | |
cout << endl; | |
} | |
void search(int x) { | |
root = find(root, x); | |
} | |
}; | |
int main() { | |
BST t; | |
t.insert(20); | |
t.insert(25); | |
t.insert(15); | |
t.insert(10); | |
t.insert(30); | |
t.display(); | |
t.remove(20); | |
t.display(); | |
t.remove(25); | |
t.display(); | |
t.remove(30); | |
t.display(); | |
return 0; | |
} |
Hello @harish-r! In your implementation of BST search, you are changing the root of the tree every time you search. This is undesired behavior in a BST.
Perhaps there's a mistake in search()
since it's just a public interface "returning" what find()
returns. Thus root
MUST NOT BE MODIFIED
Once it's fixed, everything is good.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am a student at DAIICT, India. Studying ICT with machine learning specialization. This is an article, I have written while practicing the algorithms. I gathers a lot of information and wrote this articles of a binary tree in java: this is the link-> Click here