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

BITS F232

aft
Foundations of
Data Structures and Algorithms
Lect 18
Sameera Muhamed Salam
Dept of CSIS, BITS Pilani, Hyderabad Campus

Dr
Practice question

Your friend is found trespassing in King Ganon’s favorite garden and are sent away to
be executed. Executions in Ganon’s prisons are very odd. If there are n prisoners to be
executed, the prisoners are seated in a row, and then —
▶ The first prisoner is executed, then the third prisoner is executed, then the fifth,
and so on.
▶ Once the nth (if n is odd) or (n − 1)th (if n is even) prisoner is executed, the
process repeats, but this time it starts from the end and every alternate prisoner is
executed.
▶ This process repeats till only one prisoner is left and she or he is set free.
You bribe the prison guard so that your friend will sit in a place chosen by you. Given
the number of prisoners (including your friend), write a program to find which position
to sit so that your friend can survive the execution.
Expected Time complexity of your solution - O(log n).

BITS F232 Sameera M S 2


BST Operations

▶ SEARCH
▶ TRAVERSAL
▶ MINIMUM
▶ MAXIMUM
▶ PREDECESSOR
▶ SUCCESSOR: Find the node with the
smallest key greater than key[x].
▶ INSERT
▶ DELETE.
Figure: Inorder: 1 3 4 6 7 8 10 13 14

BITS F232 Sameera M S 3


INORDER-TREE-WALK(x) Analysis

▶ If x is a root then INORDER-TREE-WALK(x) takes Θ(n) time.


▶ Let n be the number of nodes in the BST.
▶ If root have k nodes in the left sub tree then it have n-k-1 nodes in the right sub tree
▶ That is T (n) = T (k) + T (n − k − 1) + d (d is a constand)
▶ Solve this recurssion using guess a solution and substitution method
▶ Guess: T (n) = (c + d)n + c
▶ This is true for n=0
▶ If n > 0, we have T (n) = T (k) + T (n − k − 1) + d =
T (n) = (c + d)(k) + c + (c + d)(n − k − 1) + c
▶ T (n) = (c + d)n + c ⇒ Θ(n)

BITS F232 Sameera M S 4


Search & Sucessor

Figure: Running Time O(h) The SEARCH, MINIMUM, MAXIMUM,


SUCCESSOR, and PREDECESSOR can
be made to run in O(h) time on a binary
search tree of height h

Figure: Algorithm to find inorder sucessor

BITS F232 Sameera M S 5


Insertion

BITS F232 Sameera M S 6


Deletion

▶ If one of the children of node w is an external node, say node z, we simply remove
w and z from T , and replace w with the sibling of z

BITS F232 Sameera M S 7


Deletion

▶ If both children of node w are internal nodes, we cannot simply remove the node
w from T , since this would create a “hole” in T . Instead, we proceed as follows
1. We find the first internal node y that follows w in an inorder traversal of T . Node y
is the left-most internal node in the right subtree of w, and is found by going first to
the right child of w and then down T from there, following left children. Also, the
left child x of y is the external node that immediately follows node w in the inorder
traversal of T .
2. We save the element stored at w in a temporary variable t, and move the item of y
into w. This action has the effect of removing the former item stored at w.
3. We remove x and y from T by replacing y with x’s sibling, and removing both x and
y from T
4. We return the element previously stored at w, which we had saved in the temporary
variable t.

BITS F232 Sameera M S 8


Deletion

BITS F232 Sameera M S 9


Deletion: Example

BITS F232 Sameera M S 10


Deletion: Example

BITS F232 Sameera M S 11


Deletion: Example

BITS F232 Sameera M S 12


Deletion

BITS F232 Sameera M S 13


Deletion: Example 1

BITS F232 Sameera M S 14


Deletion: Example 1

BITS F232 Sameera M S 15


Deletion: Example 1

BITS F232 Sameera M S 16


Deletion: Example 1

BITS F232 Sameera M S 17


Deletion: Example 1

BITS F232 Sameera M S 18


Deletion: Example 1

BITS F232 Sameera M S 19


Deletion: Example 1

BITS F232 Sameera M S 20


Deletion: Example 2

BITS F232 Sameera M S 21


Deletion: Example 2

BITS F232 Sameera M S 22


Deletion: Example 2

BITS F232 Sameera M S 23


Deletion: Example 2

BITS F232 Sameera M S 24


Demerit of BST

Every Operations run in O(h) time. what is the maximum height of a BST?

BITS F232 Sameera M S 25


References:

▶ Michael T. Goodrich and Roberto Tamassia, “ Algorithm Design Foundations,


Analysis, and Internet Examples”, Wiley Student Edition.
▶ Jon Kleinberg and Eva Tardos, “ Algorithm Design”, Pearson Publishers.
▶ Thomas H. Chorman, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein, “
Introduction to algorithms”, MIT Press Publishers.
▶ Sanjoy Dasgupta, Umesh Vazirani, Christos Papadimitriou, “ Algorithms”,
McGraw-Hill Education Publishers.

BITS F232 Sameera M S 26


Discussion Time!

BITS F232 Sameera M S 27


BITS F232 Sameera M S 28

You might also like