0% found this document useful (0 votes)
15 views3 pages

Class BST (

This document defines a binary search tree (BST) class with methods for searching, inserting, removing, and printing nodes from the tree. The class contains a root node pointer and methods to recursively traverse the tree to search, insert, and remove nodes by comparing the target data to the node's data and traversing left or right. It also contains helper methods to recursively perform the operations and manage memory allocation and deallocation for the nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Class BST (

This document defines a binary search tree (BST) class with methods for searching, inserting, removing, and printing nodes from the tree. The class contains a root node pointer and methods to recursively traverse the tree to search, insert, and remove nodes by comparing the target data to the node's data and traversing left or right. It also contains helper methods to recursively perform the operations and manage memory allocation and deallocation for the nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

class BST {

BinaryTreeNode<int>* root;
public:
BST() {
root=NULL;
}
~BST()
{
delete root;
}
bool searchhelper(BinaryTreeNode<int>* node,int data)
{
if(root==NULL)
{
return false;
}
if(root->data>data)
{
return searchhelper(node->left,data);
}
if(root->data==data)
{
return true;
}
if(root->data<data)
{
return searchhelper(node->right,data);
}
return false;
}
BinaryTreeNode<int>* inserthelper(BinaryTreeNode<int>* node,int data)
{
if(node==NULL)
{
BinaryTreeNode<int>* newnode=new BinaryTreeNode<int>(data);
return newnode;
}
if(data>node->data)
{
return inserthelper(node->right,data);
}
if(data<node->data)
{
return inserthelper(node->left,data);
}
}
BinaryTreeNode<int>* deletenode(BinaryTreeNode<int>* node,int data)
{
if(node==NULL)
{
return NULL;
}
if(node->data<data)
{
node->right=deletenode(node->right,data);
}
if(node->data>data)
{
node->left=deletenode(node->left,data);
}
else
{
if(node->left==NULL && node->right==NULL)
{
delete node;
return NULL;
}
if(node->left==NULL)
{
BinaryTreeNode<int>* temp=node->right;
node->right=NULL;
delete node;
return temp;
}
if(node->right==NULL)
{
BinaryTreeNode<int>* temp=node->left;
node->left=NULL;
delete node;
return temp;
}
else{
BinaryTreeNode<int>* min1=node->right;
while(min1->left!=NULL)
{
min1=min1->left;
}
int rightans=min1->data;
node->data=rightans;
deletenode(node->right,rightans);
}
}
}
void printf(BinaryTreeNode<int>* node)
{
if(root==NULL)
{
return ;
}
cout<<root->data<<":";
if(root->left!=NULL)
{
cout<<"L:"<<root->left->data;
}
if(root->right!=NULL)
{
cout<<"R:"<<root->right->data;
}
cout<<endl;
printf(node->left);
printf(node->right);
}
/*----------------- Public Functions of BST -----------------*/

void remove(int data) {


root=deletenode(root,data);
}
void print()
{
printf(root);
}

void insert(int data) {


root=inserthelper(root,data);
}

bool search(int data) {


return searchhelper(root,data);
}
};

You might also like