Binary Search Tree
1/9/2024 1
Binary Tree VS Binary Search Tree
❑ Binary Tree property
– each node has 2 children 8
– result:
• storage is small
5 11
• operations are simple
• average depth is small
❑ Binary Search Tree property 2 6 10 12
– all keys in left subtree smaller than
root’s key
– all keys in right subtree larger than 4 7 9 14
root’s key
– result:
• easy to find any given key 13
• Insert/delete by changing links
Example and Counter-Example
8
5
5 11
4 8
2 7 6 10 18
1 7 11
4 15 20
3
NOT A 21
BINARY SEARCH TREE BINARY SEARCH TREE
Binary Search Tree (BST)
▪ Left subtree of a node contains the values less than or equal to (left biased)
the node’s value.
▪ Right subtree of a node contains the values greater than or equal to (right biased)
the node’s value.
▪ Follow single pattern (left/right biased); not use both in a BST.
Example:
8 Root R
3 12
Left Subtree
1 6 9 14
Right Subtree
2 10
1/9/2024 4
Binary Search Trees
A binary search tree Not a binary search tree
Binary Search Tree
Two binary search trees representing
the same set:
How to Search a Binary Search Tree?
(1) Start at the root
(2) Compare the value of the
item you are searching
for with the value stored
at the root
(3) If the values are equal,
then item found;
otherwise, if it is a leaf
node, then not found
How to Search a Binary Search Tree?
(4) If it is less than the value
stored at the root, then search
the left subtree
(5) If it is greater than the value
stored at the root, then search
the right subtree
(6) Repeat steps 2-6 for the root of
the subtree chosen in the
previous step 4 or 5
Example:
AA
15 Searching Item: 12
Steps:
BB CC
1. Search(Root, 12) Search(AA,12)
13 100
2. Search(AA->Left,12) Search(BB, 12)
DD EE 3. Search(BB->Left, 12) Search(DD, 12)
12 90 4. Return DD and Print: Item is in BST.
1/9/2024 9
Algorithm
• Search_BST(Root, Left, Right, Key) Here,
1. Loc := Search(Root, Key, NULL) Root = Root of BST
2. If Loc = NULL then Print: “Item not in BST”. Left = Left Subtree
3. Else Print: “Item is in BST”. Right = Right Subtree
4. End. Key = Searching Item
1/9/2024 11
Inserting a Node into a BST
▪ Examine the root R.
▪ If the new value is less than the root’s, then it must be inserted at the left subtree.
▪ Otherwise it must be inserted at the right subtree.
▪ This process continues until the new value is compared with a leaf node and then it is
added as that node’s (leaf node) left or right child depending on its value.
▪ Insert 13
1/9/2024 12
Example:
New Item: 110
AA
15
FF.Value=110 (FF is the New Node address)
BB CC Steps:
13 100 1. Insert(Root, FF) Insert(AA, FF)
DD 2. Insert(AA->Right, FF) Insert(CC, FF)
EE FF
12 90 110 3. Insert(CC->Right, FF)
Figure: BST with 5 Nodes
1/9/2024 13
Algorithm
Here,
• Insert_BST(Root, Left, Right, Item)
Root = Root of BST
1. Set NewNode.Value := Item.
Left = Left Subtree
2. Insert(Root, NewNode).
Right = Right Subtree
End.
Item = New Item to be inserted
1/9/2024 14
Deleting a Node from BST
❑ A node N has no children. N is deleted from BST by simply replacing the
location of N in the parent node P(N) by the NULL pointer.
❑ A node N has one child. N is deleted from BST by simply replacing the
location of N in the parent node P(N) by the location of the only child of N.
❑ A node N has two children. Let S(N) denote the inorder successor of N. N is
deleted from BST by first deleting S(N) from BST and then replacing node N
by S(N).
1/9/2024 15
Deleting a node from BST
▪ First, find the item; then, delete it
▪ Binary search tree property must be preserved!!
▪ We need to consider three different cases:
(1) Deleting a leaf
(2) Deleting a node with only one child
(3) Deleting a node with two children
(1) Deleting a leaf
▪ Simply deleted the leaf node
(2) Deleting a node with only one child
▪ Simply replacing the location of parent node (add the
child to the deleted node’s parent node)
(3) Deleting a node with two children (cont.)
▪ Find predecessor (i.e., rightmost node in the left
subtree)
▪ Replace the data of the node to be deleted with
predecessor's data
▪ Delete predecessor node
(3) Deleting a node with two children
Example:
Deleted Item: 15
AA
15 Loc = AA and Par = NULL
Steps:
BB CC
1. Delete_A(Root, Loc, Par)
13 100
2. Inorder Successor, S(Loc) = EE
DD EE FF 3. Delete EE from BST
12 90 110 4. Replace AA by EE by EE.Left = AA.Left and
EE.Right := AA.Right.
Figure: Given BST
EE
90
BB CC
13 100
After Delete Operation DD Figure: Resulting BST
FF
12 110
1/9/2024 21
Deleting a Node from BST
Algorithm
Delete_BST(Root, Left, Right, Key)
1. Set Loc := Search_BST(Root, Left, Right, Key)
2. If Loc = NULL then Print: “Item not in BST” and return
3. If Loc.Left ≠ NULL and Loc.Right, Delete_A(Root, Left, Right, Loc)
4. Else Delete_B(Root, Left, Right, Loc)
5. End.
1/9/2024 22
Does the order of inserting elements into a
tree matter?
❑ Yes, certain orders might produce very unbalanced trees!
• Does the order
of inserting
elements into
a tree matter?
Does the order of inserting elements into a tree
matter? (cont’d)
❑ Unbalanced trees are not desirable because search time
increases!
Tree Traversals
❑ There are mainly three ways to traverse a tree:
▪ Inorder Traversal
▪ Postorder Traversal
▪ Preorder Traversal
Inorder Traversal A E H J M T Y
Visit second
tree
‘J’
‘E’ ‘T’
‘A’ ‘H’ ‘M’ ‘Y’
Visit left subtree first Visit right subtree last
27
Preorder Traversal J E A H T M Y
Visit first
tree
‘J’
‘E’ ‘T’
‘A’ ‘H’ ‘M’ ‘Y’
Visit left subtree second Visit right subtree last
28
Postorder Traversal raversal:
AHEMYTJ
Visit last
tree
‘J’
‘E’ ‘T’
‘A’ ‘H’ ‘M’ ‘Y’
Visit left subtree first Visit right subtree second
29
Tree
Traversals:
another
example
Example:
AA
15
Inorder Traversal: 12, 13, 15, 90, 100, 110
BB CC Preorder Traversal : 15, 13, 12, 100, 90, 110
13 100
Postorder Traversal : 12, 13, 90, 110, 100, 15
DD EE FF
12 90 110
Figure: Binary Search Tree
1/9/2024 31
Exercises
END!!!
1/9/2024 33