Title: Binary Search Trees
1Binary Search Trees
2 Binary Search Trees (BST)
- A binary search tree is a binary tree that is
either empty or in which each node contains a key
that satisfies the following conditions - All keys (if any) in the left subtree of the root
precede (are less than or equal to) the key of
the root. - The key in the root precedes (is less than) all
keys (if any) in its right subtree. - The left and right subtrees of the root are again
binary search trees. - Sometimes we store only distinct elements and
store lt and gt elements in left and right subtrees
respectively
3Examples of BST
r
50
Right subtree of root r
Left subtree of root r
75
30
53
45
12
80
20
12
20
35
48
85
78
Inorder Traversal of a BST prints the key values
in ascending order
4 BST keys need not only be integers(Need a
comparison operator on keys)
a
c
b
e
d
f
i
h
g
k
j
5BST ADT
- typedef int ElementType
- struct TreeNode
- ElementType Element
- struct TreeNode Left
- struct TreeNode Right
-
- typedef struct TreeNode Position
- typedef struct TreeNode SearchTree
-
SearchTree MakeEmpty( SearchTree T ) Position
Find( ElementType X, SearchTree T ) Position
FindMin( SearchTree T ) Position FindMax(
SearchTree T ) SearchTree Insert( ElementType
X, SearchTree T ) SearchTree Delete(
ElementType X, SearchTree T ) ElementType
Retrieve( Position P )
Note BST could maintain pointers to other
fields/records. For example, a Roll Number
based BST for student records. Other information
may be stored in the BST record or may be
stored separately and a pointer to it kept in the
BST.
6MakeEmpty, Find
- SearchTree
- MakeEmpty( SearchTree T )
-
- if( T ! NULL )
-
- MakeEmpty( T-gtLeft )
- MakeEmpty( T-gtRight )
- free( T )
-
- return NULL
-
Position Find( ElementType X, SearchTree T )
if ( T NULL ) return NULL if (X
T-gtElement) return T if ( X lt T-gtElement
) return Find( X, T-gtLeft ) if ( X gt
T-gtElement ) return Find( X, T-gtRight
)
7FindMin, FindMax, Retrieve
- Position
- FindMin( SearchTree T )
-
- if( T NULL )
- return NULL
- else
- if( T-gtLeft NULL )
- return T
- else
- return FindMin( T-gtLeft )
-
Position FindMax( SearchTree T ) /
Non-recursive algorithm / if( T ! NULL )
while( T-gtRight ! NULL ) T
T-gtRight return T
ElementType Retrieve( Position P )
return P-gtElement
8Insert
SearchTree Insert( ElementType X, SearchTree T
) if( T NULL ) / Create and
return a one-node tree / T malloc(
sizeof( struct TreeNode ) ) if( T
NULL ) FatalError( "Out of space!!!"
) else T-gtElement X
T-gtLeft T-gtRight NULL
else if( X lt T-gtElement )
T-gtLeft Insert( X, T-gtLeft ) else
if( X gt T-gtElement ) T-gtRight Insert(
X, T-gtRight ) / Else X is in the tree
already we'll do nothing / return T /
Do not forget this line!! /
If we wish to keep duplicates of same key, we
can keep counters or insert on one side of the
BST (left or right subtree)
9Delete
else / Found element to be deleted / if(
T-gtLeft T-gtRight ) / 2 children / /
Replace with smallest in right subtree /
TmpCell FindMin( T-gtRight ) T-gtElement
TmpCell-gtElement T-gtRight Delete(
T-gtElement, T-gtRight ) else / One or
zero children / TmpCell T if(
T-gtLeft NULL ) T T-gtRight else if(
T-gtRight NULL ) T T-gtLeft free(
TmpCell ) return T
SearchTree Delete( ElementType X, SearchTree T
) Position TmpCell if( T NULL )
Error( "Element not found" ) else if( X lt
T-gtElement ) / Go left / T-gtLeft
Delete( X, T-gtLeft ) else if( X gt T-gtElement
) / Go right / T-gtRight Delete( X,
T-gtRight )
10Some Notes
- BST can be used to efficiently handle many types
higher level data types like sets, etc. - BST is usually much more efficient than a singly
linked list for such operations due to
partitioning of search space for various
operations. - The average number of steps is of the order of
log n where n nodes are there in the tree - However the worst case can be of the order of n
when the BS is skewed (like a chain) as shown in
the figure alongside. This depends on the order
of the insert and delete operations.
50
75
80
85