You should master the tree and the two-fork tree

Source: Internet
Author: User

Reprinted from:

http://blog.csdn.net/yi_zz/article/details/7396987

When I was in class, because of various reasons, the class teacher said he always do not love to listen, now to burn, just know the importance of these basic knowledge, now think, also not so difficult. To understand these underlying concepts, and then test the exam is a few very simple concepts and calculations, here I will explain the tree and the two-fork tree of some testing centers.

Basic knowledge can not be sloppy. So let's start with the basics.


There are several basic concepts in this tree.


node degree : The number of subtrees of a node is called the degree of the node. (For example, Node 1 node has a degree of 3, Node 2 node is 3, node 3 node is 0).

the degree of the tree : one of the highest degrees of all nodes. (The degree of the tree above is 3).

leaf knot : The image above should be: 3, 5, 6, 7, 9, 10

points: In addition to the leaf node, the other is called the node, and the leaf knot form a complementary relationship. (1, 2, 4, 8)

Internal node : The point of the node except the root node. (2, 4, 8)

parent Node : If the number 5th node is the sub-node of node 2nd.

Sub-node : Node 2nd is the parent node of node 5th.

Brothers Knot : 5, 6, 7 is called the Brothers Knot, from the same Father 2nd knot point.

These three concepts are a relative concept.

level : 0 layer, 1 layer, 2 layer, 3 layer.

There is one more formula that can be done : sum of points = +1 of all degree nodes (should be parent node)


traversal of the tree :


Let's look at three kinds of traversal of trees according to this graph.

Pre-sequence traversal : Start from the root, then from left to right, a tree to complete the traversal. First access the root and then access the leaf node, this is my drawing out of the pre-sequence traversal diagram, the direction of the arrows to indicate the order of traversal. A is the starting point.


The results are: 1, 2, 5, 6, 7, 3, 4, 8, 9, 10


Post-order traversal : As the name implies, is from the leaf node, the first step through the leaf node to the root node, and finally to the parent node. I can draw the order more intuitively.


The results are: 5, 6, 7, 2, 3, 9, 10, 8, 4, 1


Hierarchical traversal : traverse from left to right by 0, 1, 2, 3


The results are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10


We will then be able to understand the important characteristics of the binary tree:

We look for five binary trees to analyze: So the analysis is much simpler, I do not think that analysis, but let's talk about it.

1. In a binary tree, there is a maximum of 2 i-th nodes (i>=0) on the first layer. This is very basic, which is also the difference between a binary tree and a tree.

2. The two-tree with a depth of K has a maximum of 2 (k+1) secondary-1 nodes (k>=0). (depth is two the number of layers of the leaf node with the largest number of trees), and a depth of two forks is 2, then there are 7 nodes.

3, to any of the binary tree, if its leaf knot points for n0, the degree of 2 of the knot is N2, then n0=n2+1; (Be sure not to forget the root node is also 2 degrees).


two traversal of a fork tree


Pre-order traversal : What should be the flow: We look at the picture.


The results of the traversal are: 1, 2, 4, 5, 7, 8, 3, 6. From the root node divided into two parts, first the left of the traversal, all from left to right.


Middle Sequence Traversal:

The result is: 4,2,7,8,5,1,3,6.


Post-post traversal:


The results are: 4, 8, 7, 5, 2, 6, 3, 1


Hierarchical traversal:


The results are: 1, 2, 3, 4, 5, 6, 7, 8

So the tree and two fork tree said so much, I believe that the mastery of so much, also almost enough, oh, for the above basic knowledge, if I have the wrong place, I hope you point out Kazakhstan.

Now found that two of trees and trees are really endless talk, just published after the blog found that it is not enough to express the tree. We continue to improve.

The tree and the two fork tree traversal is really important, but there are some important points of knowledge that you may forget, and let's take a look at what's interesting: I don't really like things that are too esoteric, so I'm generally talking about these points in a simple way that I can understand.


the transformation of a tree with a two-fork tree

We all know that the binary tree is different from the tree, because the binary tree is fastidious, such as: two fork tree I-level up to only 2 of the I-th node, so when the tree into a binary tree. We have a knack for it, or to speak with a graph, and then we'll look at a diagram to see how it's transformed:


This look knows is a tree, because does not conform to our binary tree characteristics, such as node 3 with the leaf knot is not in line with our binary tree requirements, so we have to find ways to convert to a binary tree.

Very simple, just a word: the left child node of the tree as the left sub-tree node of the binary tree, the Brothers node as the right child of the binary tree. Let's see how the picture is converted:

In this figure, 2 is the left sub-tree node of the root node, with 2 tied to the 3, 4 is 2 of the brothers knot, it is converted to a binary tree, as 2 of the right child nodes, similar, 5 is 3 of the left sub-tree node, it is also the binary tree 3 of the left node, 6, 7 and 5 side by side, as 5 of the right child node, similar to the 4th junction is the same , let's look at the picture:

We can easily draw a ballpark in this sentence, and then we'll erase the black line. After wiping it out, it's like this:


This proves that my sentence is not a problem at all. Do not worry, then things are not as complicated as imagined.


To find a binary tree:

We look at the image edge analysis:

Analyze some recursive conditions for finding binary trees:

A. The left and right subtrees of the find tree are each a search tree. This is very clear and understandable.

B. If the left subtree of the lookup tree is not empty, then the nodes on the left subtree are small values of the root node. Figures: 2 and 4 are less than 5.

C. If the right subtree of the lookup tree is not empty, the values of each node on its right subtree are greater than the value of the root node. Figures: 6 and 8 are greater than 5.

So combining the graph to analyze the problem, all theories are floating clouds. Remember, I can't remember. Must be understood.

Now that we have this understanding of finding binary trees, it's easy to analyze them later.

Find some basic operations for binary trees: Find, insert nodes, delete nodes:

Find:

For example, if we want to find 56, then we can find it immediately, because the left node is smaller than the root node, and the right node is larger than the root junction point. So we were very easy to find.

Insert node:

Inserting nodes is interesting, but it is still very simple, the same rules, left small right big. Go ahead: For example, insert 29: From the above figure, we can see from the first glance, 29 must follow 20, because 56 of the right node is larger than 56, so it is impossible, then 112 of the left node. Even more unlikely, because 112 of all child nodes are larger than the root node 89, so very easy. Can only be used as the right child node of 20.

To Delete a node:

If the leaf node is directly deleted, if you bring a leaf knot point directly to the leaf node to the parent node to be deleted, if with two sub-nodes, using the middle sequence traversal to find the largest node, directly delete the maximum node.


Huffman Tree:

We look at the picture to understand:


Tree Path length : The tree reaches each node has a path, the value of all the paths are added up, the sum of the path length is the path length of the tree.

As for the first tree: Look at the picture:

The path length of the node 8 is the path length of the 3;2 path length is 2,4:3. The path length of the sibling node is the same.

and knot points are closely related to the same node of the tree, the path length of the complete binary tree is the shortest.

right : Assigning a meaningful value to a node is actually the value we see, such as the weight of the leaf node: 2, 4, 8, 1.

Weighted path length : weights multiplied by path length, such as leaf node 8 with the weighted path length: 8*3=24.

the weighted path length of the tree : All values with the weighted path length are added together. Huffman tree is the shortest length of a weighted path. Like the second tree:

The result is: 1*8+2*4+3*3=25

On the left is:

The result is: 1*1+2*2+ (4+8) *3=41, the same structure, the same knot points, the machine operation will take different time, Huffman tree is optimal.

How to construct Huffman tree:

For example, we have a set of weights {5,29,7,8,14,23,3,11}:

We start from the smallest, the smallest as a leaf node, as to why, because the weight of the number of times to represent the traversal, so we consider the problem of efficiency, the weight of the smallest as a leaf node, a tree to splice together is OK.

The first step we get is:


Huffman tree is also a binary tree, so it is also in accordance with the rules of the binary tree, left small right large.

Continue a one to add these weights to the two trees above, from small to large "piled up" up, the final formation of the tree. Look at the picture:


Huffman code: The left is 1, the right is 1, the right sign, such as 23 of the code is: 00. 11 of the code is: 010. Look at the picture:


As simple as that, all the nodes are coded out.


Clue two fork tree:

Clue two fork Tree Let's talk about how to put a two-fork tree into a clue two fork tree.

Let's look at this binary tree:

Pre-order clue two fork tree:

What kind of steps should we take? We first write this binary tree's pre-sequence traversal: ABDEHCFGI, then we fill the pointer of the node, mainly to find the leaf node of the precursor and successor.

We can see that the precursor node of the D node is the B node, and the successor node is the e node, similar to filling the null pointer of these nodes in the previous sequence traversal abdehfcgi. , it should be this diagram:


Middle sequence Clue two fork tree , we write the middle sequence binary tree traversal: dbheafcgi; then our middle sequence clue two fork tree is easy to draw out, look at the picture:


Post-secondary Clues two fork tree , we put forward the second Fork tree traversal written out: DHEBFIGCA; then our post-trail Clue two fork tree is easy to draw out, look at the picture:


Clue two the other contents of the tree, there are many, use the time to say it, the clue two fork tree and two fork tree conversion, it should be everyone needs it.


Balanced binary tree:

1, or an empty tree;

2, or a: the depth of the left and right subtree of any node in the book does not exceed 1. We analyze a tree:


We know by feeling that this tree is not a balanced binary tree, one-sided, smart: we analyze to see the conditions that meet the balanced binary tree:


In the process of analysis, we regard each node as a tree, such as Node 1 as an empty tree, is a blank tree; node 5 has no right subtree, so the difference is 1. , and we analyzed to node 39 when we see that it has no right subtree, and Zuozi is 3, can be 1, 5, 7 as the left subtree to judge.

So this tree is not in accordance with the requirements of our balanced binary tree.

Let's analyze one of the requirements and see what the two-fork tree looks like:

In fact, we can be identified with the naked eye, but if the confidence is not enough, you can analyze and analyze.


The adjustment of the balance tree : You know a few spins and you're done. Let's see what this rotation is all about:

ll balance rotation : Right-handed balance treatment, in fact, is to find a way to balance these binary trees, look at the picture:

RR balance rotation : l-treatment.


From the above two rotation, the main point is the middle node as the root nodes, and then the other two nodes as the left and right sub-tree, you can achieve the purpose.

The next thing we see is a little bit more complicated, but it's also simple:


lr Balanced rotation : We have the above two basis, and then look at this is easy: Read the following:

Do not be frightened by this figure, in fact, I was a look at that time, but after our analysis, are paper tigers: we see

1, we take the left side of the graph decomposition to see:

Zuozi is added a new node, a look very familiar, we put this left sub-tree to remove BL and cl, then the RR rotation is the same reason, the C node as the root node, it became: so this tree on this side of the problem, next we look at the whole tree:

The whole tree is still unbalanced, so we are still the old way, as the LL type: we can get the result:

There is also a rotational LR rotation, the same, we will not say. Oh, I hope to help everyone.

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.