An introduction to binary search and red-black trees


So lookup , insert , and delete will always be logarithmic in the number of nodes but insert and delete may be more complicated than for binary search trees. Before we describe what red-black trees are, it will be useful to introduce the concept of an external node. An external node is a "place-holder" node used to indicate the status "no child" instead of using null to indicate "no child".

No information is stored inside an external node i. If we were to use external nodes in a binary search tree, then the children of a leaf node would be external nodes and not null , that is:. It will be easier to describe the algorithms for inserting and deleting on a red-black tree if we use external nodes it cuts out a number of situations where we would otherwise have to worry about null.

When red-black trees are implemented e. As with the binary search tree, we will want to be able to perform the following operations on red-black trees:. Because a red-black tree is a binary search tree and operations that don't change the structure of a tree won't affect whether the tree satisfies the red-black tree properties, the lookup and print operations are identical to lookup and print for binary search trees.

The goal of the insert operation is to insert key K into tree T , maintaining T 's red-black tree properties. A special case is required for an empty tree. If T is empty, replace it with a single black node with black external nodes containing K. This ensures that the root property is satisfied. Recall that the BST insert algorithm always adds a leaf node which, in our representation, will be a node with two black external nodes. Because we are dealing with a non-empty red-black tree, adding a leaf node will not affect T 's satisfaction of the root property.

Moreover, adding a red leaf node will not affect T 's satisfaction of the black property. However, adding a red leaf node may affect T 's satisfaction of the red property, so we will need to check if that is the case and, if so, fix it step 3.

In fixing a red property violation, we will need to make sure that we don't end up with a tree that violates the root or black properties. For step 3 for inserting into a non-empty tree, what we need to do will depend on the color of K 's parent.

Let P be K 's parent. We need to consider two cases:. K 's parent P is red. If K 's parent P is red, then P now has a red child, which violates the red property. Note that P 's parent, G, K 's grandparent must be black why? In order to handle this double-red situation, we will need to consider the color of G 's other child, that is, P 's sibling, S.

Because we are using external nodes, we do not need to worry about whether the other child of G is null or not. We have two cases:.

We also need to make sure that any subtrees of P and S end up in the appropriate place once the restructuring is done. There are four possibilities for the relative ordering of K, P, and G.

The way a restructuring is done for each of the possibilities is shown below. Once a restructuring is done, the double-red situation has been handled and there's nothing more to do you should convince yourself, by looking at the diagrams above, that restructuring will not result in a violation of the black property. P 's sibling S is red. Recoloring does not affect the black property of a tree: Here we will look at yet another kind of balanced tree called a red-black tree.

The important idea behind all of these trees is that the insert and delete operations may restructure the tree to keep it balanced.

So lookup , insert , and delete will always be logarithmic in the number of nodes but insert and delete may be more complicated than for binary search trees. As with the binary search tree, we will want to be able to perform the following operations on red-black trees:. Because a red-black tree is a binary search tree and operations that don't change the structure of a tree won't affect whether the tree satisfies the red-black tree properties, the lookup and print operations are identical to lookup and print for binary search trees.

The goal of the insert operation is to insert key K into tree T , maintaining T 's red-black tree properties. A special case is required for an empty tree. If T is empty, replace it with a single black node containing K. This ensures that the root property is satisfied. Recall that the BST insert algorithm always adds a leaf node. Because we are dealing with a non-empty red-black tree, adding a leaf node will not affect T 's satisfaction of the root property.

Moreover, adding a red leaf node will not affect T 's satisfaction of the black property. However, adding a red leaf node may affect T 's satisfaction of the red property, so we will need to check if that is the case and, if so, fix it step 3. In fixing a red property violation, we will need to make sure that we don't end up with a tree that violates the root or black properties.

For step 3 for inserting into a non-empty tree, what we need to do will depend on the color of K 's parent. Let P be K 's parent. We need to consider two cases:. K 's parent P is black. K 's parent P is red. If K 's parent P is red, then P now has a red child, which violates the red property. Note that P 's parent, G, K 's grandparent must be black why?

In order to handle this double-red situation, we will need to consider the color of G 's other child, that is, P 's sibling, S. Note that S might be null, i. We have two cases:. P 's sibling S is black or null. If P 's sibling S is black or null, then we will do a trinode restructuring of K the newly added node , P K 's parent , and G K 's grandparent.

We also need to make sure that any subtrees of P and S if S is not null end up in the appropriate place once the restructuring is done. There are four possibilities for the relative ordering of K , P , and G.

The way a restructuring is done for each of the possibilities is shown below. If S is null, then in the pictures above, S and it's subtrees labelled 2 and 3 would be replaced with nothing i. Once a restructuring is done, the double-red situation has been handled and there's nothing more to do you should convince yourself, by looking at the diagrams above, that restructuring will not result in a violation of the black property.