Red/black tree (study notes)

Source: Internet
Author: User

1. Why do we propose a red/black tree?

The time complexity of searching, inserting, and deleting a binary search tree is O (h), where H is the height of the tree. Assume that the number of nodes in the binary search tree is N. If the binary search tree is balanced, H = O (log n). If the binary search tree is seriously unbalanced, if the tree height H is greater than O (log n), the time complexity of the binary search tree search, insertion, and deletion operations is relatively high.

The balanced search tree is the data structure proposed to solve the problem that the time complexity of searching, inserting, and deleting operations is relatively high when the height of the Binary Search Tree is H.

So far, I have come into contact with the following balanced search trees: AVL Tree, B tree, and red/black tree. (There are many other balanced search trees)

1) AVL Tree: The self-balancing Binary Search Tree first invented. The maximum height difference between the two son trees at any node in aVL is 1.

2) B tree: A Balanced Multi-Cross Tree.

3) Red/black tree: it is a symmetric binary tree.

2. What is a red/black tree?

In short, the red-black tree is a binary search tree with an O (log n) height.

Each node in the red/black tree has five fields: Key (node value), color (node color), P (pointing to the parent), and left (pointing to the left child), right (pointing to the right child ).

The red/black tree has five properties:

Property 1. nodes are red or black. Property 2. The root node is black. Nature 3. Each leaf node (nil node, empty node) is black. (Note: The NIL node is considered a leaf node. Generally, the parent node of the root node is also a nil node, indicating the outer node relative to the inner node of the red/black tree. the two subnodes of each Red node are black. (There cannot be two consecutive red nodes in all paths from each leaf to the root.) 5. All paths from any node to each leaf contain the same number of black nodes.

 

Red/black tree (study notes)

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.