CH 12
CH 12
CH 12
1
Material for the presentation taken from Cormen, Leiserson, Rivest and
Stein, Introduction to Algorithms, Third Edition; 1
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees
2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees
2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees
2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees
2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary search tree
I Binary-search-tree property
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary search tree
I Binary-search-tree property
Let x be a node in a binary search tree. If y is a node in the
left subtree of x, then y .key ≤ x.key . If y is a node in the
right subtree of x, then y .key ≥ x.key .
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary search tree
I Binary-search-tree property
Let x be a node in a binary search tree. If y is a node in the
left subtree of x, then y .key ≤ x.key . If y is a node in the
right subtree of x, then y .key ≥ x.key .
I Each node contains a key, satellite data; and attributes left,
right and p.
3
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary search tree
4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary search tree
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Inorder tree walk
function Inorder-Tree-Walk(x)
if x 6= NIL then
Inorder-Tree-Walk(x.left)
print x.key
Inorder-Tree-Walk(x.right)
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Inorder tree walk
function Inorder-Tree-Walk(x)
if x 6= NIL then
Inorder-Tree-Walk(x.left)
print x.key
Inorder-Tree-Walk(x.right)
I Inorder-Tree-Walk(T .root)
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Inorder tree walk
function Inorder-Tree-Walk(x)
if x 6= NIL then
Inorder-Tree-Walk(x.left)
print x.key
Inorder-Tree-Walk(x.right)
I Inorder-Tree-Walk(T .root)
I Inorder tree walk prints the keys in a sorted order for a binary
search tree.
5
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Inorder tree walk
function Inorder-Tree-Walk(x)
if x 6= NIL then
Inorder-Tree-Walk(x.left)
print x.key
Inorder-Tree-Walk(x.right)
I What is the running time of
Inorder-Tree-Walk(T .root)?
6
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Querying a binary search tree
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Querying a binary search tree
7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-search(x, k)
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-search(x, k)
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-search(x, k)
8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-search(x, k)
9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-search(x, k)
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-minimum
10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-maximum
11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-maximum
11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-maximum
12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Successor and Predecessor
13
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
15
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor
16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Insert and Delete
17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Insert and Delete
17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Insert and Delete
17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Insert
18
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete
19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-delete procedure
20
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure
21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure
21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure
21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure
22
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-delete procedure
25
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure
26
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes
27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-delete procedure
28
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure
29
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-delete procedure
30
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees
I What will the BST look like if we insert the keys in the
following order?
5, 1, 3, 6, 2, 4
31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees
I What will the BST look like if we insert the keys in the
following order?
5, 1, 3, 6, 2, 4
I What if the order was the following?
1, 2, 3, 4, 5, 6
31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees
I What will the BST look like if we insert the keys in the
following order?
5, 1, 3, 6, 2, 4
I What if the order was the following?
1, 2, 3, 4, 5, 6
I We can perform the following operations in O(h) time :
Search, Minimum, Maximum, Predecessor,
Successor, Insert and Delete
31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees
I What will the BST look like if we insert the keys in the
following order?
5, 1, 3, 6, 2, 4
I What if the order was the following?
1, 2, 3, 4, 5, 6
I We can perform the following operations in O(h) time :
Search, Minimum, Maximum, Predecessor,
Successor, Insert and Delete
I However, the height h depends on the order in which the keys
are inserted.
31
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees
32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms