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

Binary Search Tree Functions: Insert A Node in BST Delete A Node in BST

The document contains code for functions to perform operations on a binary search tree (BST) including inserting and deleting nodes, and traversing the tree using preorder, inorder, and postorder traversal. It includes functions to insert a new node, delete a node, search for a node, and display the tree. It also contains helper functions to handle different cases when inserting, deleting, or replacing nodes depending on the number of child nodes.

Uploaded by

vicky chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Binary Search Tree Functions: Insert A Node in BST Delete A Node in BST

The document contains code for functions to perform operations on a binary search tree (BST) including inserting and deleting nodes, and traversing the tree using preorder, inorder, and postorder traversal. It includes functions to insert a new node, delete a node, search for a node, and display the tree. It also contains helper functions to handle different cases when inserting, deleting, or replacing nodes depending on the number of child nodes.

Uploaded by

vicky chaudhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like