binary-search-and-red-black-trees – topcoder
binary-search-and-red-black-trees – topcoder
topcoder.com
We’ll start off by looking at some of the general terms and concepts
used in dealing with trees We’ll then focus on binary search trees
(BST), a special type of tree that keeps elements sorted (but
doesn’t guarantee efficient insertion and retrieval). Finally we’ll look
at red-black trees, a variation of binary search trees that overcome
BST’s limitations through a logarithmic bound on insertion and
retrieval.
Trees terminology
The number of lines you pass through when you travel from the
root until you reach a particular node is the depth of that node in
the tree (node G in the figure above has a depth of 2). The height
of the tree is the maximum depth of any node in the tree (the tree in
Figure 1 has a height of 3). The number of children emanating from
a given node is referred to as its degree — for example, node A
above has a degree of 3 and node H has a degree of 1.
Traversing BST
Procedure Inorder_traversal(Node n)
if(n == nil)
return;
Inorder_traversal(n.left_subtree);
Print(n.value);
Inorder_traversal(n.right_subtree);
…
Inorder_traversal(root);
BST insertion:
When adding a new node to a binary search tree, note that the new
node will always be a leaf in the tree. To insert a new node, all we
will do is navigate the BST starting from the root. If the new node
value is smaller than the current node value, we go left – if it is
As the new node is smaller than the current node (4 < 5), we will
go left and set current node to 2.
As the new node is greater than current node (4 > 2), we will go
right and set the current node to 3.
We’ve reached a leaf, so the last step is to attach the new node to
the right of the current node. Here is how the new BST looks:
BST Deletion:
Deleting a node from BST is a little more subtle. Formally there are
three cases for deleting node n from a BST:
Remove m from the tree (if you have reached this case then m
will always have no left child, though I’ll leave the proof to the
reader), so we apply one or the other of the above cases to do
this.
BST Retrieval:
Retrieving an element from binary search trees requires simple
navigation, starting from the root and going left, if the current node
is larger than the node we are looking for, or going right otherwise.
Red-black Trees
2. All leaf (nil) nodes are colored with black; if a node’s child is
missing then we will assume that it has a nil child in that place and
this nil child is always colored black.
least h/2 by property 3 above (as each red node strictly requires
black children).
For any non-leaf node v (height > 0) we can see that the black
height of any of its two children is at least equal to bh(v)-1 — if the
child is black, that is, otherwise it is equal to bh(v) . By applying the
hypothesis above we conclude that each child has at least
2^[bh(v)-1]-1 internal nodes, accordingly node v has at least
2^[bh(v)-1]-1 + 2^[bh(v)-1]-1 + 1 = 2^bh(v)-1
internal nodes, which ends the proof.
Rotations
There are two types of rotations: left rotation and right rotation. Left
rotation swaps the parent node with its right child, while right
rotation swaps the parent node with its left child. Here are the steps
involved in for left rotation (for right rotations just change “left” to
“right” below):
Conclusion
Although you may never need to implement your own set or map
classes, thanks to their common built-in support, understanding
how these data structures work should help you better assess the
performance of your applications and give you more insight into
what structure is right for a given task. For more practice with these
concepts, check out these problems from the TopCoder archive
that involve trees:
References