cout<<"Node Added To Right"<<endl;
BINARY SEARCH TREE return;
FUNCTIONS }
}
INSERT A NODE IN BST }
temp = new node;
cout<<"Enter the number to be inserted : "; DELETE A NODE IN BST
cin>>temp->info; if (root == NULL)
insert(root, temp); {
cout<<"Tree is empty, nothing to
insert(node *tree, node *newnode) delete"<<endl;
{ continue;
if (root == NULL) }
{ cout<<"Enter the number to be deleted : ";
root = new node; cin>>num;
root->info = newnode->info; del(num);
root->left = NULL;
root->right = NULL; del(int item)
cout<<"Root Node is Added"<<endl; {
return; node *parent, *location;
} if (root == NULL)
if (tree->info == newnode->info) {
{ cout<<"Tree empty"<<endl;
cout<<"Element already in the tree"<<endl; return;
return; }
} find(item, &parent, &location);
if (tree->info > newnode->info) if (location == NULL)
{ {
if (tree->left != NULL) cout<<"Item not present in tree"<<endl;
{ return;
insert(tree->left, newnode); }
} if (location->left == NULL && location->right == NULL)
else case_a(parent, location);
{ if (location->left != NULL && location->right == NULL)
tree->left = newnode; case_b(parent, location);
(tree->left)->left = NULL; if (location->left == NULL && location->right != NULL)
(tree->left)->right = NULL; case_b(parent, location);
cout<<"Node Added To Left"<<endl; if (location->left != NULL && location->right != NULL)
return; case_c(parent, location);
} free(location);
} }
else
{ CASE A: FOR LEAF NODE
if (tree->right != NULL) case_a(node *par, node *loc )
{ {
insert(tree->right, newnode); if (par == NULL)
} {
else root = NULL;
{ }
tree->right = newnode; else
(tree->right)->left = NULL; {
(tree->right)->right = NULL; if (loc == par->left)
par->left = NULL; {
else if (loc == par->left)
par->right = NULL; par->left = suc;
} else
} par->right = suc;
}
CASE B: FOR NODE WITH SINGLE CHILD suc->left = loc->left;
suc->right = loc->right;
NODE }
case_b(node *par, node *loc)
{
node *child; DISPLAY BST:
if (loc->left != NULL) cout<<"Display BST:"<<endl;
child = loc->left; display(root,1);
else
display(node *ptr, int level)
child = loc->right;
{
if (par == NULL)
int i;
{
if (ptr != NULL)
root = child;
{
}
display(ptr->right, level+1);
else
cout<<endl;
{
if (ptr == root)
if (loc == par->left)
cout<<"Root->: ";
par->left = child;
else
else
{
par->right = child;
for (i = 0;i < level;i++)
}
cout<<" ";
}
}
cout<<ptr->info;
CASE C: FOR NODE WITH TWO CHILD display(ptr->left, level+1);
NODE }
case_c(node *par, node *loc) }
{
node *ptr, *ptrsave, *suc, *parsuc;
ptrsave = loc;
SEARCH NODE IN BST:
find(int item, node **par, node **loc)
ptr = loc->right;
{
while (ptr->left != NULL)
node *ptr, *ptrsave;
{
if (root == NULL)
ptrsave = ptr;
{
ptr = ptr->left;
*loc = NULL;
}
*par = NULL;
suc = ptr;
return;
parsuc = ptrsave;
}
if (suc->left == NULL && suc->right == NULL)
if (item == root->info)
case_a(parsuc, suc);
{
else
*loc = root;
case_b(parsuc, suc);
*par = NULL;
if (par == NULL)
return;
{
}
root = suc;
if (item < root->info)
}
ptr = root->left;
else
else
ptr = root->right;
ptrsave = root;
while (ptr != NULL)
{ POSTORDER TRAVERSAL:
if (item == ptr->info) postorder(node *ptr)
{ {
*loc = ptr; if (root == NULL)
*par = ptrsave; {
return; cout<<"Tree is empty"<<endl;
} return;
ptrsave = ptr; }
if (item < ptr->info) if (ptr != NULL)
ptr = ptr->left; {
else postorder(ptr->left);
ptr = ptr->right; postorder(ptr->right);
} cout<<ptr->info<<" ";
*loc = NULL; }
*par = ptrsave; }
}
PREORDER TRAVERSAL:
preorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
INORDER TRAVERSAL:
inorder(node *ptr)
{
if (root == NULL)
{
cout<<"Tree is empty"<<endl;
return;
}
if (ptr != NULL)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
Best PDF Encryption Reviews