Prog. assignment 4
Prog. assignment 4
Objective
This assignment deepens your understanding of Binary Search Trees (BST) and
Red-Black Trees (RBT).
1
* find predecessor(key: int) -> int: Returns the in-order
predecessor of a key in the tree.
* find successor(key: int) -> int: Returns the in-order
successor of a key in the tree.
Example Usage
bst = BinarySearchTree ( )
bst . i n s e r t (10)
bst . i n s e r t (20)
bst . i n s e r t (5)
print ( b s t . s e l e c t ( 2 ) ) # 10 (2 nd s m a l l e s t e l e m e n t )
print ( b s t . rank ( 2 0 ) ) # 3 ( rank o f 20)
print ( b s t . f i n d p r e d e c e s s o r ( 1 0 ) ) # 5
print ( b s t . f i n d s u c c e s s o r ( 1 0 ) ) # 20
2
* select(k: int) -> int: Returns the k-th smallest ele-
ment in the tree (1-based indexing).
* rank(key: int) -> int: Returns the rank of a given key
in the tree (i.e., the number of elements smaller than the
given key).
* find predecessor(key: int) -> int: Returns the in-order
predecessor of a key in the tree.
* find successor(key: int) -> int: Returns the in-order
successor of a key in the tree.
Example Usage
r b t = RedBlackTree ( )
rbt . i n s e r t (10)
rbt . i n s e r t (20)
rbt . i n s e r t (5)
print ( r b t . s e l e c t ( 2 ) ) # 10 (2 nd s m a l l e s t e l e m e n t )
print ( r b t . rank ( 2 0 ) ) # 3 ( rank o f 20)
print ( r b t . f i n d p r e d e c e s s o r ( 1 0 ) ) # 5
print ( r b t . f i n d s u c c e s s o r ( 1 0 ) ) # 20
Note
You are expected to work individually on this assignment.