Exercise
- Please implement a BST which contains both keys and values. In other words, it works like a map, and it supports:
put(key, value)
: Ifkey
is found, update its associated value tovalue
; otherwise, insert a new node.get(key)
: Return value associated withkey
; returnnull
if key is not present.delete(key)
: Delete the node whose key iskey
; do nothing if key is not present.
- When updating (i.e.,
put()
ordelete()
) a BST, it is required that the binary-search-tree property holds true. Please design an algorithm to check whether a given BST is valid.
-
In our implementation of
remove()
of a BST,x
can be replaced with the leftmost (i.e, min) nodey
in the tree rooted atx.right
. Alternatively,x
can also be replaced with the the rightmost (i.e., max) nodez
in the tree rooted atx.left
. Essentially,z
is the predecessor ofx
. Please design an algorithm using this idea.10 8 / \ delete(10) / \ 7 15 ---------> 7 15 / \ / \ / / \ 5 8 11 18 5 11 18
- Please implement
removeMax(x, key)
which returns a new root for a BST in an iterative way.
- Ordered-based operations.
- Please implement all order-based operations for a BST introduced in Binary Search Tree (2).
- Can you implement them in an iterative way?
- Please argue that inserting a new key into a RBT will never violate Property 1, 3 and 4.
- Try to implement a RBT with a global
NIL
object.
- Please plot the height of a RBT with random N keys.
- A node
x
is inserted into a RBT withput()
and then is immediately deleted withremove()
. Is the resulting tree always the same as the initial red-black tree? Justify your answer.
- If we didn't introduce
NIL
object, then how to handle the case whenx
isnull
for the deletion on a RBT?
- Draw the RBT that results when you insert items with the keys (10, 2, 30, 40, 26, 32, 11, 18) in that order into an initially empty tree.
- Please design an algorithm to check whether a given RBT is valid.
- A k-d tree (short for k-dimensional tree) is a space-partitioning data structure for organizing points in a k-dimensional space.
Please complete the code based on kd_tree.py or KDTree.java.