Red-black tree Guide, Meaning , Facts, Information and Description
A red-black tree is a type of self-balancing binary search tree, a data structure used in computer science. It was invented in 1972 by Rudolf Bayer who called them "symmetric binary B-trees". It is complex, but has good worst-case running time for its operations and is efficient in practice: it can search, insert, and delete in O(log n) time, where n is the number of elements in the tree.
| Table of contents |
|
2 Uses and advantages 3 Properties 4 Operations 5 See also 6 References 7 External links |
A red-black tree is a special type of binary tree, which is a structure used in computer science to organize pieces of data, such as numbers. Each piece of data is stored in a node. One of the nodes always functions as our starting place, and is not the child of any node; we call this the root node or root. It has up to two "children", other nodes which it connects to. Each of these children can have children of its own, and so on. The root node thus has a path connecting it to any other node in the tree.
If a node has no children, we call it a leaf node, since intuitively it is at the edge of the tree. A subtree is the portion of the tree that can be reached from a certain node, considered as a tree itself.
As red-black trees are also binary search trees, they must satisfy the constraint that every node contains a value greater than or equal to all nodes in its left subtree, and less than or equal to all nodes in its right subtree. This makes it quick to search the tree for a given value.
Red-black trees, along with AVL trees, offer the best possible worst-case guarantees for insertion time, deletion time, and search time. Not only does this make them valuable in time-sensitive applications such as real-time applicationss, but it makes them valuable building blocks in other data structures which provide worst-case guarantees; for example, many data structures used in computational geometry can be based on red-black trees.
Red-black trees are also particularly valuable in functional programming, where they are one of the most common persistent data structures, used to construct associative arrays and setss which can retain previous versions after mutations. The persistent version of red-black trees requires O(log n) space for each insertion or deletion, in addition to time.
A red-black tree is a binary search tree where each node has a color attribute, the value of which is either red or black. In addition to the ordinary requirements imposed on binary search trees, we make the following additional requirements of any valid red-black tree:
These constraints enforce a critical property of red-black trees: that the longest possible path from the root to a leaf is no more than twice as long as the shortest possible path. The result is that the tree is roughly balanced. Since operations such as inserting, deleting, and finding values requires worst-case time proportional to the height of the tree, this theoretical upper bound on the height allows red-black trees to be efficient in the worst-case, unlike ordinary binary search trees.
To see why these properties guarantee this, it suffices to note that no path can have two red nodes in a row, due to property 3. The shortest possible path has all black nodes, and the longest possible path alternates between red and black nodes. Since all maximal paths have the same number of black nodes, by property 4, this shows that no path is more than twice as long as any other path.
A common source of confusion with these properties is that they assume that all the leaves in the tree are nil leaves, which contain no data and serve merely to indicate where the tree ends. These nodes are often omitted in drawings, resulting in a tree which seems to contradict the above principles, but which in fact does not.
Read-only operations on a red-black tree require no modification from those used for binary search trees, since it is a binary search tree. However, after an insertion or removal, the red-black properties might become violated. Restoring the red-black properties requires a small number (O(log n)) of color changes (which are very quick in practice) and no more than three tree rotations (two for insertion). This allows insertion and deletion to remain O(log n) time, but it also turns them into very complex operations.Background and terminology
Uses and advantages
Properties
Operations
| Case 3: If both the parent node and the uncle node are red, then we can repaint them both black and repaint the grandparent node red. Now our new red node has a black parent. Since any path through the parent or uncle must pass through the grandparent, the number of black nodes on these paths has not changed. However, the grandparent may now be a red node with a red parent, violating property 3. If so, we perform this entire procedure recursively on it. |
Note: In the remaining cases, we assume the parent node P is the left child of its parent. If it is the right child, left and right should be reversed throughout cases 4 and 5.
| Case 4: The parent is red but the uncle is black; also, the new node is the right child of its parent, and the parent in turn is the left child of its parent. In this case, we can perform a left rotation that switches the roles of the new node and its parent; then, we deal with the former parent node using Case 5. This causes some paths to pass through either the new node or the parent where they did not before, but both these nodes are red, so property 4 is not violated. |
| Case 5: The parent is red but the uncle is black, the new node is the left child of its parent, and the parent is the left child of its parent. In this case, we perform a right rotation about the grandparent; the result is a tree where the former parent is now the parent of both the new node and the former grandparent. We know that the former grandparent is black, since the parent could not have been red otherwise. We then switch the colors of the former parent and grandparent nodes, and the resulting tree satisfies property 3. Property 4 also remains satisfied, since all paths that went through any of these three nodes went through the grandparent before, and now they all go through the former parent. In each case, this is the only black node of the three. |
| Case 2: S is red, and N is the left child of its parent. In this case we rotate left at N's parent, turning the red sibling into N's grandparent. We then reverse the colors of N's new parent and grandparent. Although all paths still have the same number of black nodes, now N has a black sibling and a red parent, so we can proceed to step 4, 5, or 6. (Its new sibling is black because it was once the child of the red S.) |
| Case 3: N's parent, S, and S's children are black. In this case, we simply repaint S red. The result is that all paths passing through S, which are precisely those paths not passing through N, have one less black node. Because deleting N's original parent made all paths passing through N have one less black node, this evens things up. However, all paths through P now have one fewer black node than paths that do not pass through P, so property 4 is still violated. To correct this, we perform the rebalancing procedure on P, starting at case 1. |
| Case 4: S and S's children are black, but N's parent is red. In this case, we simply exchange the colors of N's sibling and parent. This doesn't affect the number of black nodes on paths not going through N, but it does add one to the number of black nodes on paths going through N, making up for the deleted black node on those paths. |
| Case 5: S is black, S's left child is red, S's right child is black, and N is the left child of its parent. In this case we rotate right at S, so that S's left child becomes S's parent and N's new sibling. We then exchange the colors of S and its new parent. All paths still have the same number of black nodes, but now N has a black sibling whose right child is red, so we fall into case 6. Neither N nor its parent are affected by this transformation. |
|
Case 6: S is black, S's right child is red, and N is the left child of its parent. In this case we rotate left at N's parent, so that S becomes the parent of N's parent and S's right child. We then exchange the colors of N's parent and S, and make S's right child black. The subtree still has the same color at its root, so property 3 is not violated. However, N now has one additional black ancestor: either N's parent has become black, or it was black and S was added as a black grandparent. Thus, the paths passing through N pass through one additional black node. Meanwhile, if a path does not go through N, then there are two possibilities:
|
This is an Article on Red-black tree. Page Contains Information, Facts Details or Explanation Guide About Red-black tree See also
References
External links
