Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

PROGRAM 14

AIM: WAP to implement insertion, searching, deletion and traversal


algorithm on Binary search tree.

THEORY: In binary search tree all the left subtree elements should be
less than root data and all the right subtree elements should be
smaller than data. This is called binary search tree property. Note
that this property should be satisfied at every node in the tree.

Since root data is always between left subtree data and right subtree
data performing inorder traversal on binary search tree produces a
sorted list. If we are searching for an element and if left subtree data
is less than the element we want to search then skip it. Same is the
case with right subtree. In other words the binary seach tree
considers either the left or right subtree to search an element but
not both.

ALGORITHM:

a. Insertion:
1. If root == NULL create new node and return the same.
2. If root->data>=d
i. root->left=Insertion(root->left,d)
3. else root->right=Insertion(root->right,d)
b. deletion:
1. If root==NULL return NULL
2. if d<root->data
i. call deletinbst(root->left,d)
ii. return root

3. else if (d==root->d)

i. if (it has no left or right children)


-delete root

-return NULL

ii. if (it has left child but no right child)

-node* temp=root->right

- delete root

- return temp

iii. if(it has no right child but right chid)

-node* temp=root->right

- delete root

- return temp

iv. if(it has both left and right child)

-traverse using a temporary node to the leftmost


child and then

root->data=rep->data;

root->right=deleteinbst(root->right,rep->data);

return root;

4. else

root->right=deleteinbst(root->right,d);

return root;

c. searching:
1.if(root is NULL)
return false;

2. if(root->data==key)

return true;

3.else if(key<=root->data)

return searchh(root->left,key);

4. else return searchh(root->right,key);

d. traversal:

(inorder)

1. traverse the right subtree in order

2. visit the root

3. traverse the right subtree in order.

CODE:

#include<iostream>

using namespace std;

/*5 3 7 1 6 8 -1*/

class node

public:

int data;

node* left;

node* right;
node(int d)

data=d;

left=right=NULL;

};

void printin(node* root)

if(!root)

return;

printin(root->left);

cout<<root->data<<",";

printin(root->right);

node* insertbst(node* root,int d)

if(!root)

return new node(d);

if(root->data>=d)
root->left=insertbst(root->left,d);

else

root->right=insertbst(root->right,d);

node* build()

int d;

cin>>d;

node* root=NULL;

while(d!=-1)

root=insertbst(root,d);

cin>>d;

return root;

bool searchh(node* root,int key)

if(!root)
return false;

if(root->data==key)

return true;

if(key<=root->data)

return searchh(root->left,key);

return searchh(root->right,key);

node* deleteinbst(node* root,int d)

if(!root)

return NULL;

if(d<root->data)

root->left=deleteinbst(root->left,d);

return root;

else if(root->data==d)

if(root->left==NULL&&root->right==NULL)
{

delete root;

return NULL;

if(root->left==NULL&&root->right!=NULL)

node* temp=root->right;

delete root;

return temp;

if(root->left!=NULL&&root->right==NULL)

node* temp=root->left;

delete root;

return temp;

node* rep=root->right;

while(rep->left!=NULL)

rep=rep->left;

}
root->data=rep->data;

root->right=deleteinbst(root->right,rep->data);

return root;

else

root->right=deleteinbst(root->right,d);

return root;

int main()

cout<<"Enter the elements in the binary search tree.\nPress -1 if


current node is NULL."<<endl;

node* root=build();

cout<<"\nThe inorder traversal of BST gives following order:\n";

printin(root);

cout<<"\nEnter the element you want to search\n";

int d;

cin>>d;

if(searchh(root,d))

{
cout<<"\npresent\n";

else{

cout<<"\nabsent\n";

cout<<"Enter the element you want to delete\n";

cin>>d;

root=deleteinbst(root,d);

cout<<"The inorder traversal of tree after deletion of "<<d<<"


is: \n"<<endl;

printin(root);

return 0;

OUTPUT:
DISCUSSION:

As we can clearly see from the output figure the inorder traversal
of a binary search tree is always sorted.

LEARNING:

The time complexity of inorder traversal is O(n).

Its space complexity is also O(n).

The basic operations on binary search tree take time proportional to


height of the tree.

Hence there exist variations in time complexities of best case


average case and worst case.
Flowcharts:

a. insertion:

Insertion(root,d
ata)

no

yes
Root==NULL Root->data>=d
?yes

return new node(d)

end
b. searching:

Search(root,data)

yes
Root==N Return false
ULL?

no

Root- Return true


>data==key

Key<=root-
>data

c. Traversal:

Inorder(root)
yes
Root==NULL? return

NO

Inorder(root
->left)

cout<<root->data

Inorder(root-
>right)

You might also like