Tree (Binary Tree, red/black tree, and left-handed right-hand adjustment operation)

Source: Internet
Author: User

When analyzing the source code of the Java Collection class, it involves the knowledge of the tree. It is found that the tree structure is much more complex than arrays and linked lists. Therefore, you can consolidate and supplement your knowledge.

TreeTree can have several definitions. One of the methods is recursion.

A Tree is a finite set T of n (n ≥ 0) nodes. If T is null, it is called an empty Tree. Otherwise, it meets the following two conditions:

  1. There is only one specific Root node;
  2. The remaining nodes can be divided into m (m ≥ 0) subsets of non-Intersecting Tl, T2 ,..., Tm, where each subset is itself a tree and is called as the root SubTree ).

It can be seen from the definition that a tree is the combination of N nodes and the N-1 edge, one of which is called the root. The existence of a N-1 edge is like this: each edge connects a node to its parent, and each node has a parent except the root node.

Basic tree-related definitions:

  1. A node without a subnode is calledLeaf, OrLeaf node.
  2. Nodes with the same father are calledSibling Node.
  3. For any nodeDepthIt is the length of the unique path from the root to the node.
  4. TreeHighIt refers to the most common path from the root node to all nodes.

Binary Tree

A binary tree is a tree, and each node cannot have two sons.

Tree nodes can be implemented as follows:

1 class Node {2 Object element; // store data 3 Node left; // left child 4 Node right; // right child 5}

Binary Search Tree

An important application of binary tree is in search. The binary tree is called the binary search tree. For each node X in the tree, the value of all items in its left subtree is smaller than that in X, the value of all items in the right subtree is greater than that in X.

Insert and delete nodes in the binary search tree

The insert operation searches along the tree. If the X to be inserted into the tree is found, nothing is done. Otherwise, insert X to the last node of the traversal path. To insert a node to a node, you only need to modify the left or right reference of the inserted node.

The delete operation is much more complex than the search operation, because if the deleted node is not a leaf node, you need to re-connect the child node of the deleted node to the deleted tree and maintain the nature of the search tree. Delete a node in three cases:

If the deleted node is a leaf node, delete it immediately.

The deleted node has a child. The child node replaces the deleted node. The Father's Day point of the deleted node will be changed to the reference of the deleted node, this allows you to "Bypass" the deleted node.

The deleted node has two children. The general deletion policy is to replace the deleted node with the minimum data of the right subtree (the "leftmost" node in the right subtree, and recursively Delete the node. Because the minimum data of the right subtree does not have the left subtree, it is easier to delete it than to delete it for the first time.

AVLTree

The AVL Tree is a binary search tree with a balance condition. This balance condition must be easily maintained, and it ensures that the depth of the tree must be O (log N ). An AVL Tree is a binary search tree with a maximum height difference of 1 between the left subtree and the right subtree of each node (the height of the empty tree is defined as-1 ).

Inserting an AVL tree may cause it to lose its balance. However, you can simply correct the tree to maintain its balance. This operation is calledRotate.

After insertion, only the paths from the insertion point to the root node may be balanced, so only the Subtrees of these nodes may change. When going up to the root and updating the balance information along this path, you can find a node whose new balance breaks the AVL condition. Mark this node as. Any node can have two sons at most, so there are four situations:

  1. Insert the left subtree of the Left son of a once.
  2. Insert the right subtree of the Left son of
  3. Insert the left subtree of the right son of a once.
  4. Insert the right subtree of the right son of

The above four situations can be further summarized into two types of operations, that is, insertion occurs outside (left-left (1), right-right (4 )); insert occurs in "internal" (left-right (2), right-left (3 )). Only one external request is required.Single RotationYou can complete the adjustment, and the "internal" needs to passDouble Rotation.

Single Rotation

Shows how single rotation is performed for scenario 1. The left side is before rotation, and the right side is after rotation. In the tree on the left, a new node is inserted in subtree X so that node k2 no longer meets AVL's balanced nature, because its left subtree is two layers deeper than the right subtree (the dotted line in the middle of the graph represents the layers of the tree ).

Demonstrate single rotation correction in case 4:

Double Rotation

For "internal" situations, rotation cannot be handled, for example.

In this case, you need to use double rotation (the following two cases of double Rotation Processing ):

Red/black tree

Another popular change in AVL Tree history isRed/black tree. The red and black trees have the following colored binary search trees:

  1. Each node may be red or black.
  2. The root is black.
  3. If a node is red, its child nodes must be black.
  4. Each path from a node to a null reference must contain the same number of black nodes.

The following is a red-black tree:

When a new node is inserted into the red/black tree, if it is painted black, it is definitely in violation of Condition 4 because a longer black Node path will be created. Therefore, this item must be red. If its parent node is black, the insertion is complete. If its parent node is red, the continuous red node violates Condition 3. In this case, you must adjust the overview to ensure that the property of the red/black tree is maintained. The basic operations used to complete this task are color change and tree rotation.

By default, the above content is red when you exit the insert node. If the tree balance is damaged after the insert node is inserted, You need to rotate it to reach a balance, then modify the color to ensure the properties of the red/black tree.

For more details about the red and black trees, you can refer to the following blogs, which are much better than mine.

Http://blog.csdn.net/v_JULY_v/archive/2010/12/29/6105630.aspx

Http://blog.csdn.net/v_JULY_v/archive/2010/12/31/6109153.aspx

Http://blog.csdn.net/v_JULY_v/article/details/6124989

 

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.