Balanced Search Tree

Source: Internet
Author: User

------------------------------------------------------ After reading Algorithm

For a long time, I have not had a good understanding of the red and black trees. I have guided me to read the algorithm and the Open Class on Coursera, so I have a good understanding of it. Now I want to write it down and share it with you.

2-3 Search Tree

Although the Binary Search Tree solves most search problems, the performance in the worst case is still poor (~ N) to ensure the balance of the search tree. We introduced 3-nodes (compared with 2-nodes in the binary search tree) and constructed 2-3 search trees accordingly. Its nature is as follows:

Allow 1 or 2 keys per node.

2-nodes: one key, two children.

3-nodes: two keys, three children.

Invalid Ric order. Inorder traversal yields keys in ascending order.

Perfect balance. Every path from root to null link has same length.

For queries of 2-3 trees, the inserted search is relatively simple. For more information, see the following figure.

 

Image reference from: http://algs4.cs.princeton.edu

Red/Black Binary Search Tree

The basic idea of the red/Black Binary Search Tree is to use the standard binary search tree (composed of 2-nodes) and some additional information (replace 3-node) to represent 2-3 trees.

. 1 replace 3-node

An equivalent definition of the red/black tree:

. Red links are left links

. No node is connected to two red chains at the same time.

The tree is completely black balanced, that is, the number of black links on the path connecting any blank to the root node is the same

 

. 2 Color Representation

. 3 rotate

.. Rotate the right link of H to the left

.. Right rotate the left link of H

.. Color conversion: it refers to the color conversion of two Red Sub-nodes at a node, and the color of the parent node is converted from black to red.

Balanced Search Tree

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.