Sort the binary tree, balance the concept of binary tree and red black tree, and explain the relevant operation

Source: Internet
Author: User

1. Sort binary Tree

The sort binary tree is a special structure of a two-fork tree, which makes it very easy to sort and retrieve all the nodes in the tree.

The sort binary tree is either an empty binary tree or a two-fork tree with the following properties:

    • If its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node;
    • If its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
    • Its left and right subtrees are also sorted by binary tree respectively .

Figure 1 shows a sort of binary tree:

Figure 1. Sort two-fork tree

For a sort binary tree, an ordered sequence can be obtained from small to large if it is traversed by the middle sequence. 1 shows the binary tree, the middle sequence traversal:

{2,3,4,8,9,9,10,13,15,18}

1.1 Creating a Sort binary tree

To create a sort of binary tree, which is the process of adding nodes to the sort binary tree, the steps to add a node to the sort binary tree are as follows:

    1. Search begins at the root node of the current node.
    2. Compare the value of the new node with the value of the current node.
    3. If the value of the new node is large, the right child node of the current node is the new current node, and if the value of the new node is small, the current node's left child node is the new current node.
    4. Repeat 2 or 32 steps until the appropriate leaf node is found.
    5. Add the new node as a child of the leaf node found in step 4th, or to the right child node if the new node is larger; otherwise add as left dial hand node .
1.2 Deleting a node in a sorted binary tree

When a program removes a node from a sorted binary tree, the program must maintain the ordered binary tree in order for it to remain sorted binary tree. Maintenance can be divided into the following situations:

(1) The deleted node is a leaf node, so you simply remove it from its parent node.

(2) The deleted node p only left dial hand tree, the left sub-tree PL of P is added to the left subtree of the parent node of p; the deleted node p only has the right subtree, and the right subtree PR of P is added to the right subtree of the parent node of P.

(3) If the left and right subtrees of the deleted node p are not empty, there are two ways:

    • Set PL to the left or right child node of P's parent node Q (depending on p is the left and right child node of its parent node Q), set PR to the right child node of the middle order forward node s of the P node (s is the most right node of PL, which is the largest node in the PL subtree).
    • In order to replace the point of P node with the pre-order or successor of P, then delete the pre-order or successor node from the original sort binary tree. (That is, use the minimum node greater than P or the maximum node less than p to replace the P node).

2. Balanced binary Tree

Balanced binary tree: It is an empty tree or its left and right two sub-tree height difference of the absolute value of not more than 1, and the left and right two subtrees are a balanced binary tree. Construction and adjustment methods the common algorithms for balancing binary trees are red, black, AVL, treap and so on. The formula for the node of the minimum binary balance tree is as follows F (n) =f (n-1) +f (n-2) +1 This is similar to a recursive sequence, can refer to the Fibonacci sequence, 1 is the root node, F (n-1) is the number of nodes of Zuozi, F (n-2) is the number of nodes of the right subtree.

3. Red and black Trees

3.1 Nature of red and black trees

The red-black tree in the original sort binary tree adds the following several requirements:

    • Property 1: Each node is either red or black.
    • Property 2: The root node is always black.
    • Property 3: All leaf nodes are empty nodes (that is, null) and are black.
    • Property 4: The two child nodes of each red node are black. (There will be no two contiguous red nodes from each leaf to the root path)
    • Property 5: The path from either node to each leaf node in its subtree contains the same number of black nodes.

The red and black trees implemented in Java may have a structure of 6:


Figure 6. A schematic of the Java red and black tree

Note: In this article all about red and black trees are used in white to represent red. Black nodes are still represented by black.

According to Nature 5: The path of the red black tree from the root node to each leaf node contains the same number of black nodes, so the number of black nodes contained in the path from the root node to the leaf node is called the "black Height (black-height)" of the tree.

Property 4 guarantees that the longest path from the root node to the leaf node will not exceed twice times the length of any other path. If there is a black height of 3 red-black tree: From the root node to the leaf node the shortest path length is 2, the path is full of black nodes (black node-black node-black node). The longest path can also only be 4, inserting a red node between each black node (black node-red node-black node-red node-black node), Nature 4 guarantees that no more red nodes can be inserted. Thus, the longest path in the red and black trees is a red and black alternate path.

Red and black trees and balanced binary trees

Red and black trees are not really balanced binary trees, but in practical applications, the statistical performance of red and black trees is higher than that of balanced binary trees, but the extreme performance is slightly worse.

From this we can conclude that for a given black height of N, the shortest path length from root to leaf node is N-1, and the longest path length is 2 * (N-1).

Tip: The depth of the sort binary tree directly affects the performance of the retrieval, as pointed out earlier, when the insertion node itself is from small to large arrangement, the sorting binary tree will become a linked list, this sort of binary tree retrieval performance is lowest: N nodes of two tree depth is N-1.

The red and black trees are guaranteed to be roughly balanced by the above restrictions-because the height of the red and black trees does not increase indefinitely, so that the red and black trees are highly efficient in the worst case, and there is no normal sort of binary tree.

Since the red-black tree is just a special sort of binary tree, the read-only operation on the red-black tree is exactly the same as the read-only operation on the normal sort binary tree, except that the red-black tree maintains an approximate balance, so the retrieval performance is much better than a binary tree sort.

However, inserting and deleting on a red-black tree will cause the tree to no longer conform to the red-black tree, so both the INSERT and delete operations need to be maintained to ensure that the tree is still red and black when the node is inserted and deleted.

The answer is: ABCD

Sort the binary tree, balance the concept of the binary tree and the red and black tree, and the related operation.

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.