CH 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 69

1

Data Structures and Algorithms

BITS-Pilani K. K. Birla Goa Campus

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

I Dynamic set : Linked list, Hash Table

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees

I Dynamic set : Linked list, Hash Table


I Binary Search Tree is a data structure that supports many
dynamic set operations:

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees

I Dynamic set : Linked list, Hash Table


I Binary Search Tree is a data structure that supports many
dynamic set operations:
Search, Minimum, Maximum, Predecessor,
Successor, Insert and Delete.

2
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Ch. 12: Binary Search Trees

I Dynamic set : Linked list, Hash Table


I Binary Search Tree is a data structure that supports many
dynamic set operations:
Search, Minimum, Maximum, Predecessor,
Successor, Insert and Delete.
I The basic operations take time proportional to the height of
the tree (i.e. O(h) time).

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

I Does the above binary tree satisfy the Binary-search-tree


property?

4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Binary search tree

I Does the above binary tree satisfy the Binary-search-tree


property?
I The worst-case running time of the search tree operations will
be less efficient, because the height of the search tree is more.
4
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Inorder tree walk

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

I Operations: Search, Minimum, Maximum,


Predecessor, Successor, Insert and Delete

7
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Querying a binary search tree

I Operations: Search, Minimum, Maximum,


Predecessor, Successor, Insert and Delete
I All the operations can be performed in O(h) time.

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)

I How can we search node 13?

8
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-search(x, k)

I How can we search node 13?


I How can we search node 12?

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)

Running time of Tree-search procedure?


9
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-minimum

I How can we find the minimum element?

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-minimum

I How can we find the minimum element?

10
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-maximum

I How about the maximum element?

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-maximum

I How about the maximum element?

11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-maximum

I How about the maximum element?

I Tree-minimum and Tree-maximum run in O(h) time.


11
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Using Dijkstra’s algorithm

I Let G (V , E ) be a graph having negative edge weights. Can


we use Dijkstra’s algorithm?

12
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Successor and Predecessor

I Successor of a node x in the BST is the node that comes


after node x during an inorder tree walk.

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

I How will we find the successor of 15?

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor

I How will we find the successor of 15?


Tree-minimum(15.right)

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor

I How will we find the successor of 15?


Tree-minimum(15.right)
I What if there is no right child?

14
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor

I How will we find the successor of 15?


Tree-minimum(15.right)
I What if there is no right child?
E.g. How will we find the successor of 13?

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

I Tree-successor runs in O(h) time.

16
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-successor

I Tree-successor runs in O(h) time.


I The procedure Tree-predecessor is symmetric to
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

I After we Insert or Delete a node from the BST, the


binary-search-tree property must continue to hold.

17
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Insert and Delete

I After we Insert or Delete a node from the BST, the


binary-search-tree property must continue to hold.

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

I For deleting a node, we need to consider multiple cases.

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete

I For deleting a node, we need to consider multiple cases.


I We want to delete node z, where z has at most one child
node.

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete

I For deleting a node, we need to consider multiple cases.


I We want to delete node z, where z has at most one child
node.

19
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete

I For deleting a node, we need to consider multiple cases.


I We want to delete node z, where z has at most one child
node.

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

I Transplant procedure moves subtrees within a binary


search tree.

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure

I Transplant procedure moves subtrees within a binary


search tree.

21
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Transplant procedure

I Transplant procedure moves subtrees within a binary


search tree.

I Transplant procedure simplifies the Tree-Delete


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

I When node z has two child nodes, we replace z with its


successor node.

23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes

I When node z has two child nodes, we replace z with its


successor node.
(c) Successor node is the right child.
(d) Successor node is not the right child.
23
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes

(c) Successor of node z is its right child.

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes

(c) Successor of node z is its right child.

24
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes

(c) Successor of node z is its right child.

I Suppose node y is the right child and the successor of node z.


Is it necessary that the left child of node y be NIL?

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

(d) Successor of node z is not its right child

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes

(d) Successor of node z is not its right child

27
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Tree-Delete : Node z has two child nodes

(d) Successor of node z is not its right child

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

I Tree-delete procedure takes O(h) time.

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

I Theorem: The expected height of a randomly built binary


search tree on n distinct keys is O(lg n).

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms
Building binary search trees

I Theorem: The expected height of a randomly built binary


search tree on n distinct keys is O(lg n).
I Proof given in section 12.4. (Proof not part of the syllabus.)

32
BITS-Pilani K. K. Birla Goa Campus Data Structures and Algorithms

You might also like