Tree-related knowledge

Source: Internet
Author: User

The tree node structure is defined as follows:

struct bitnode{    int num;    struct bitnode *lchild;    struct bitnode *rchild;} TREENODE;

Chained storage structure

Traversal of a binary tree

(1) Sequential traversal (first root traversal)

1. Access root node

2. First Order traversal Zuozi

3. First-order traversal of the right sub-tree

(2) Middle sequence traversal (middle root traversal)

1. Middle Sequence Traversal Zuozi

2. Access root node

3. The middle sequence traverses the right sub-tree

(3) Sequential traversal (post-root traversal)

1. Post-Zuozi traversal

2. Post-the-loop traversal of right sub-tree

3. Access root node

For traversal results:

First Order traversal: abdecfg

Middle Sequence Traversal: DBEAFCG

Post-post traversal: DEBFGCA


SGI STL's associative containers (map, set, Multimap, Multiset) are based on red Black TREE,RBT, a widely used two-fork search tree (binary search tree,bst). Have a better operating efficiency.

1. Two fork find Tree
Two fork Find tree provides element insertion and access in logarithmic time, It follows the following rules: The key value of any node must be greater than the key value of each node in its left subtree, and is less than the key value of each node in its right child tree. In extreme cases, however, when all nodes are on a chain, the binary lookup tree operates at O (N). Binary find Tree Sample:

1.1 Maximum and minimum values
Depending on the nature of the binary lookup tree, the minimum value is continuously descending along the left dial hand tree along the root node, which is the node with the lowest value, and the same maximum value goes down the right subtree along the root node, and the last node is the node with the largest value.
1.2 Insertion of a node
Starting from the root node, if the value of the new insertion node is greater than the value of the current node, select the right child node to continue the comparison, otherwise select the left Dial hand node to continue the comparison, so that the operation is done until the leaf node. Make the new insertion node A child of the leaf node (set to left Dial hand node or right child node depending on the size relationship).

1.3 Deletion of nodes
Depending on the node being deleted, there are several scenarios:
1.3.1 is deleted node as leaf node
Delete the node directly (the left [right] subtree corresponding to the parent node is set to null);
1.3.2 deleted node only Zuozi (or only right subtree)
The parent node of the deleted node points to the deleted node's pointer to the child node of the deleted node;
1.3.3 deleted nodes exist both right and left subtree
Method One: Find the successor of the deleted node, update the value of the deleted node to the value of the successor node, and finally delete the successor node;
Method Two: Find the first trend of the deleted node, update the value of the deleted node to the value of the forward node, and finally delete the forward node;
Shows method one Delete method:

We are traversing the two trees in order to find an ordered sequence of data: "1 2 3 4 5 6 7 8"

Efficiency analysis of Binary search tree

So let's take a look at the efficiency of binary search tree

Obviously, finding a data in a two-fork lookup tree structure in a A-B two graph does not need to traverse all of the node elements, and the lookup efficiency does improve. But there is a very serious problem: we need to compare 5 times to find 8 in the a graph, and only 3 times in the B graph. What is even more serious is that if you set up a binary search tree in ordered sequence [1 2 3 4 5 6 7 8], the whole tree is degenerate into a linear structure (e.g. C input graph: Single tree), where finding 8 requires comparing 8 data, and sequential lookups are no different.

To summarize: In the worst case, the two-fork sort tree is transformed into a single tree, the depth of which is N, and its lookup time complexity is the same as the Order lookup O (N). The best case is that the two-fork sort tree has the same shape as the binary lookup tree, and its average lookup length is proportional to log2 (n) (O (log2 (n))).


1.4 CLRS 12.2-5
Proof: If a node in a binary search tree has two children, its successor has no left child and its predecessor has no right child.
This conclusion is a support of 1.3.3. Can be proved by contradiction:
The successor is the least-valued node in the right subtree, which is the leftmost leaf node in the right subtree, which is certainly impossible to have left children, otherwise it is impossible to be the leftmost leaf node. The same can be proven before the right children.

2. AVL Tree
The AVL tree belongs to the binary lookup tree, and the height of the two subtrees of any node in the AVL tree is one, so it is also called a height-balanced tree. Find, insert, and delete are O (log n) in both average and worst cases, and additions and deletions may require the tree to be rebalanced by one or more tree rotations.
The lookup and insertion operations of binary lookup trees are at worst the complexity of O (N), while the AVL tree is still O (LgN) at its worst.

Rotation of 2.1 nodes
The insert and delete operations of the AVL tree require the rotation of the nodes to maintain a high balance of the tree. The rotation of the nodes is divided into left-and right-handed. The nature of the binary search tree remains after rotation.

The inverse operation of the left-hand operation in the right-handed operation is well understood as long as it is remembered to maintain the properties of BST.

2.2 Insertion of a node
The insertion result of a node can be divided into four cases. If the balance of the AVL tree is compromised after the node is inserted, then x is the deepest (lower) node in the node where the balance state is compromised.
1. The insertion point is at the left of the left Dial hand node of x-left;
2. The insertion point is located in the right sub-tree of the left Dial hand node of x-around;
3. The insertion point is located in the left sub-tree of the right child node of X-right left;
4. The insertion point is at the right sub-tree of the right child node of X-right;
For the 1th and 4th cases, the nature of the AVL tree is maintained by a single rotation, and two rotations are required for 2nd and 3rd to maintain the nature of the AVL tree.

Situation One:

Situation Two:

3. Red and black Trees
If BST satisfies how red-black nature, then the BST is a red-black tree:
1. Each node is either red or black;
2. The root node is black;
3. If a node is red, its child nodes must be black;
4. Any path from any node to a null node, containing the same number of black nodes;
When inserting a node into a red-black tree, in order to ensure as little damage as possible of the nature of the red-black tree, we let: The new node is red. At this point, if the entire tree after the insertion of a node, then the nature of 2 is destroyed, as long as the section Dianran is black, and if the parent node of the new node is also red, it destroys the nature of 3, in this case the operation is a bit complicated.

The keeping of the nature of the red-black tree
If the nature of the red-black tree is destroyed after inserting a node, we need to do something to keep the red-black tree node in character. This is divided into four cases (there are four other cases that are symmetrical), with the following conventions:
1. New node: X
2. Parent node: P
3. Grandfather node: G
4. Uncle Node: S
5. Grandfather node: GG

Situation One
S is black and x is outside, rotate PG and change PG color:

Situation Two
S is black and x is inserted inside, the PX is first rotated once and the GX color is changed, and then the GX is rotated one at a time:

Situation Three
S is red and X for the outer side of the insertion, first to do a single rotation of the PG, and change the color of x, if GG is black, then complete, otherwise enter the situation four:

Situation Four
S is red and X is inserted on the outer side, first a single rotation of the PG, and change the color of x, when GG is red, continue to do, until there is no longer a parent-child node continuous red.

All four of the above cases are left dial hand nodes with P G, which is similar when p is the right child node of G.


Tree-related knowledge

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.