12.1 Trees
12.1 Trees
MSIT-2K24
TREES
Trees Introduction
Binary
Binary Tree
Search Tree
12 6
A rooted tree in which the number of children of each
node is at most two. The order is not important i.e., 7 9 4
the left node may or may not be less than its parent or
the right node. 5 7 10
Tree Traversing
The keys in a binary search tree are always stored in such a way as to satisfy the
binary-search-tree property
Binary Search Tree (BST)
Binary-search-tree property
In Figure:
The key of the root is 6.
The keys 2, 5, and 5 in its left subtree are no larger than 6.
The keys 7 and 8 in its right subtree are no smaller than 6.
Searching Algorithm
Given a pointer to the root of the tree and a
key k, TREE-SEARCH returns a pointer to a
node with key k if one exists; otherwise, it
returns NIL.
Binary Search Tree (BST)
Searching Algorithm (Recursion)
pointer to 15 13 Y Y Y 𝒙=𝟔
pointer to 6 13 Y Y N 𝒙=𝟕
pointer to 7 13 Y Y N 𝒙 = 𝟏𝟑
pointer to 13 13 Y N Pointer to 13
Binary Search Tree (BST)
MINIMUM Algorithm
pointer to 15 N 𝒙=𝟔
pointer to 6 N 𝒙=𝟑
pointer to 3 N 𝒙=𝟐
pointer to 2 Y Pointer to 2
Binary Search Tree (BST)
MAXIMUM Algorithm
pointer to 15 N 𝒙 = 𝟏𝟖
pointer to 18 N 𝒙 = 𝟐𝟎
pointer to 20 Y Pointer to 20
BST CONSTRUCTION
Binary Search Tree (BST)
A binary search tree (BST) is a rooted binary tree whose internal nodes each store a key greater
than all the keys in the node's left subtree and less than those in its right subtree.
Binary-search-tree property
Binary Search Tree (BST)
Construction of BST for the following items
12, 5, 14, 3, 30, 50, 20, 8 12
5 14
12 12
3 30
12
12 12 5 14 5 14 20 50
12 14
5
5 5 14 3 30 3 30
3
50 12
5 14
3 8 30
20 50
Binary Search Tree (BST)
Insertion
Let we want to insert a node with value = 40.
First, we must find the right location for 40.
Move the value 50 to the right to create place for 40.
Insert 40 by creating a new node.
12
12
5 14
5 14
3 8 30
3 8 30
20 40
20 50
50
Binary Search Tree (BST)
Insertion Algorithm Node z for which z.key = v , z.left = NIL, and z.right = NIL.
y x 𝐱 ≠ 𝑵𝑰𝑳 y=x 𝒛. 𝒌𝒆𝒚 < 𝒙. 𝒌𝒆𝒚 𝒙 = 𝒙. 𝒍𝒆𝒇𝒕 𝒙 = 𝒙. 𝒓𝒊𝒈𝒉𝒕 z.p = y y == NIL T.Root = z 𝒛. 𝒌𝒆𝒚 < 𝒚. 𝒌𝒆𝒚 𝒚. 𝒍𝒆𝒇𝒕 = 𝒛 𝒚. 𝒓𝒊𝒈𝒉𝒕 = 𝒛
12
Deleting a node with 1 child
Replace the element with its child 5 14
50
Binary Search Tree (BST)
Deletion:
Deleting a node with 0 child.
Let delete a node with 0 child i.e. 50.
12 12
5 14 5 14
3 8 30 3 8 30
20 40 20 40
50
Binary Search Tree (BST)
Deletion:
Deleting a node with 1 child
Let delete a node with 1 child i.e. 14.
12
12
5 30
5 14
3 8
3 8 30 40
20
20 40
Binary Search Tree (BST)
Deletion (Inorder predecessor):
Deleting a node with 2 children
Let delete a node with 2 children i.e. 12.
8
12
5 30
5 30
3
3 8 Inorder predecessor 20 40
20 40 Replace the node to
be deleted with the
node of maximum
element in the left
tree.
Binary Search Tree (BST)
Deletion (Inorder Successor):
Deleting a node with 2 children
Let delete a node with 2 children i.e. 12.
20
12
5 30
5 30
3 8
3 8 Inorder successor 40
20 40 Replace the node to
be deleted with the
node of minimum
element in the right
tree.
THANK YOU