0% found this document useful (0 votes)
49 views

Prog. assignment 4

Uploaded by

Sake Anila
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Prog. assignment 4

Uploaded by

Sake Anila
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Assignment 4: Binary Search Trees

and Red-Black Trees


CS212 : Design and Analysis of Algorithms

Objective
This assignment deepens your understanding of Binary Search Trees (BST) and
Red-Black Trees (RBT).

Part 1: Binary Search Tree (BST)


Tasks
1. Node Structure (Augmented with Additional Data):
ˆ Define a class/function/method BSTNode to represent a node in a
binary search tree. Each node should contain:
– A key (integer).
– Left child, right child and parent references/pointers.
2. BST Class with Augmentations:
ˆ Create a BinarySearchTree class with the following methods:
– insert(key: int): Inserts a key into the BST.
– search(key: int) -> bool: Returns True if the key is found
in the tree, otherwise False.
– delete(key: int): Deletes a key from the BST.
– inorder traversal() -> List[int]: Returns a list of keys us-
ing in-order traversal.
– find minimum() -> int: Returns the minimum key in the BST.
– find maximum() -> int: Returns the maximum key in the BST.
– Advanced Operations:
* select(k: int) -> int: Returns the k-th smallest ele-
ment in the tree.
* 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).

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

Part 2: Red-Black Tree (RBT)


Tasks
1. Node Structure (Augmented with Additional Data):
ˆ Define a class RBTNode to represent a node in a Red-Black Tree. Each
node should contain:
– A key (integer).
– Left and right child references.
– A parent reference.
– A color (RED or BLACK).
2. RBT Class with Balancing and Augmentations:
ˆ Create a RedBlackTree class with the following methods:
– insert(key: int): Inserts a key into the Red-Black Tree and
maintains its balancing properties.
– search(key: int) -> bool: Returns True if the key is found
in the tree, otherwise False.
– delete(key: int): Deletes a key from the Red-Black Tree and
rebalances the tree.
– inorder traversal() -> List[int]: Returns a list of keys us-
ing in-order traversal.
– find minimum() -> int: Returns the minimum key in the RBT.
– find maximum() -> int: Returns the maximum key in the RBT.
– Advanced Operations:

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.

Balancing Logic and Augmentations


Ensure that rotations and recoloring are implemented correctly to maintain
Red-Black Tree properties during insertion and deletion.

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.

ˆ Plagiarism will not be tolerated. Any suspected plagiarism will result in


penalties as per the course policy.

You might also like