# On algorithm and data structure nine balances find red and black trees of trees

Source: Internet
Author: User
Tags bool

The previous article introduced the 2-3 lookup tree, and you can see that the 2-3 lookup tree can ensure that the tree is balanced after inserting the element, at worst, all the child nodes are 2-node, the height of the tree is LGN, thus ensuring the worst-case time complexity. However, the 2-3-tree is more complex, this paper introduces a simple data structure of 2-3-tree, that is, red-black trees (red-black tree)

Defined

The main image of the red-black tree is to encode the 2-3 lookup tree, especially to add additional information to the 3-nodes node in the 2-3 lookup tree. The red-black tree divides the links between nodes into two different types, red links, which he uses to link two 2-nodes nodes to represent a 3-nodes node. Black links are used to link common 2-3 nodes. In particular, the two 2-nodes with a red link are used to represent a 3-nodes node and tilt to the left, i.e. one 2-node is the left child node of another 2-node. The advantage of this approach is that you do not need to make any changes to the search, and the normal two-fork lookup tree is the same.

According to the above description, the red-black tree is defined as follows:

The red-black tree is a balanced lookup tree with red and black links, while satisfying:

Red node Tilt left

One node cannot have two red links

The entire book is completely black balance, that is, from the root node to the path of the leaf node, the number of black links are the same.

The image below shows that the red-black tree is actually another manifestation of the 2-3 tree: If we draw the red line horizontally, then the two 2-node nodes he links to are a 3-node node in the 2-3 tree.

Said

We can add a new flag to represent the color on each node of the binary lookup tree. The tag indicates the color of the node to its parent node.

```Private Const BOOL RED = TRUE;
Private Const BOOL Black = FALSE;

private Node root;

Class node
{public
node left {get; set;}
Public Node Right {get;
Public TKey Key {get; set;}
Public TValue Value {get; set;}
public int number {get; set;}
public bool Color {get; set;}

Public Node (TKey key, TValue value,int number, bool color)
{this
. key = key;
This. value = value;
This. Number = number;
This. color = color;
}

private bool Isred (node node)
{
if (node = null) return false;
Return node. Color = RED;
}```

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.