0% found this document useful (0 votes)
36 views

Unit 3b

Binary search trees allow for efficient searching with an average time complexity of O(log n). They maintain the property that all left descendants of a node are less than the node's value and all right descendants are greater. Common binary tree operations include insertion, deletion, and traversal. There are three traversal orders - preorder visits the root then left/right subtrees, inorder visits left subtree then root then right subtree, and postorder visits left/right subtrees then the root.

Uploaded by

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

Unit 3b

Binary search trees allow for efficient searching with an average time complexity of O(log n). They maintain the property that all left descendants of a node are less than the node's value and all right descendants are greater. Common binary tree operations include insertion, deletion, and traversal. There are three traversal orders - preorder visits the root then left/right subtrees, inorder visits left subtree then root then right subtree, and postorder visits left/right subtrees then the root.

Uploaded by

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

APPLICATIONS OF BINARY TREES

BINARY SEARCH TREES


Binary Search Tree:
A Binary tree T is a Binary Search Tree (BST) if each node N of T has the
following property : -

 The value at N is greater than every value in the left subtree of N and
 The value at N is less than every value in the right subtree of N.

 The reason why we go for a Binary Search tree is to improve the searching
efficiency.

 The average case time complexity of the search operation in a binary search tree
is O( log n ).
6

2 8

1 4
Empty

empty (T)
{
if (T!=NULL)
{
empty(T->left);
empty(T->right);
free(T);
}
return(NULL);
}

Insert Operation in a Binary Search Tree


insert(T,x)
{
if (T==NULL)
T=getnode();
else
{
T->info =x;
T->left =T->right =NULL;
}
}
6

2 8

1 4

Delete Operation in a Binary Search Tree

The delete operation in a Binary search tree follows three cases.

Case 1 : - THE NODE IS A LEAF


 Delete it immediately

2 8

1 4

Delete Node 3
3
Case 2 : NODE HAS 1 CHILD

 Adjust a pointer from the parent to bypass that node.

6 6

2 8
2 8

1 4
1 3

Fig :- Indicates Deletion of a node(4) with one child before and after.

Case 3 :- THE NODE HAS 2 CHILDREN

 Replace the key of that node with the minimum element at the right subtree
 Delete the minimum element

o Has either no child or only right child because if it has a left child that left
child would be smaller and would have been chosen.
o So Invoke Case 1 Or 2.
6
6 6

2 8 3 8
3 8

1 5 1 5
1 5

3 3

4
4
4

Fig :- Deletion of a node (2) with two children before and After.
delete(T,x)
{
if(T==NULL)
{
printf(“Element Not Found”):
exit(1);
}
else
{
if(x<T->info)
T->left=delete(x,T->left);
else
if(x>T->info)
T->right=delete(x,T->right);
else
if(T->left && T->right)
{
temp = findmin(T->right);
T->element = temp->element;
T->right = delete(T->element,T->right);
}
else
{
temp = T;
if(T->left= =NULL)
T=T->right;

else if (T->right = = NULL)


T=T->left;
free(temp);
}
return T;
}

Time Complexity = O( Height of the tree )

BINARY TREE TRAVERSALS


Traversal :- Visiting Each Nodes.

There are three standard ways of traversing a binary tree T with root R. They are:

( i ) Preorder Traversal
(ii ) Inorder Traversal
(iii) Postorder Traversal

Preorder Traversal:

 Process the root R.


 Traverse the left subtree of R in preorder.
 Traverse the right subtree of R in preorder.

Inorder Traversal:

 Traverse the left subtree of R in inorder.


 Process the root R.
 Traverse the right subtree of R in inorder.

Postorder Traversal:

 Traverse the left subtree of R in postorder.


 Traverse the right subtree of R in postorder.
 Process the root R.

Traversal algorithms using recursive approach

a. Preorder Traversal
PREORDER( Root )

Nodeptr Root;
{
if( Root!=NULL )
{
printf info( Root )
preorder( left(Root))
preorder ( right(Root))
}
}
b. Inorder Traversal
INORDER( Root )

Nodeptr Root;
{
if( Root!=NULL )
{
inorder( left(Root))
printf info( Root )
inorder ( right(Root))
}
}

c. Postorder Traversal
POSTORDER( Root )

Nodeptr Root;
{
if( Root!=NULL )
{
postorder( left(Root))
postorder ( right(Root))
printf info( Root )

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy