Introduction and implementation of The Red/black tree (1)

Source: Internet
Author: User

I. Red/black tree (Red-Black TreeIs a binary search tree (Binary Search Tree. In the worst case, the binary search tree may become a linked list (after all nodes are inserted in ascending order ). The cause of this inefficiency is that the tree does not maintain a certain balance. To improve the search efficiency, we need to find a way to maintain the balance on the left of the tree, that is, to minimize the height of the tree, A feasible approach is to use some strategies to adjust the structure of the tree after each modification of the tree content, so as to meet certain balance conditions. One of them meets certain balancing conditions and is currently widely used in the red and black trees. It can be spent every time a node is inserted or deleted.O(Log N) To modify the tree structure to maintain the tree balance. The method of searching the red and black trees is the same as that of the binary tree.O (log N)Time. The method for inserting and deleting nodes in the red-black tree is to maintain the properties of the tree after the original binary tree search and deletion algorithm.

Each node of the red/black tree has one attribute.Key,3Pointers:Parent,Left,RightIn addition, there is an additional attribute:Color. It can only be in two colors: red or black,Of course, you can also add someKeyTo index the data. In addition to all the properties of the Binary Search Tree, the red/black tree also has the following5Point nature:

1.Each node is black or red.

2.The root node is black.

3.The empty node is black (the root node in the red/black treeParentAnd all leaf nodesLeft,RightDo not pointNULL, But points to a defined null

Related Article

Contact Us

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.