Here we first study Binary Trees.
I. Three basic properties of Binary Trees
1. If the level of the Binary Tree node starts from 1, the I layer of the binary tree has a maximum of 2 (I-1) to the power of the node. (I ≥ 1)
2, the depth of k binary tree has at least k nodes, a maximum of 2 (k-1) to the power of the node. (K ≥ 1)
3. For any binary tree, if there are n0 leaf nodes and n2 non-leaf nodes with a degree of 2, there is n0 = n2 + 1.
Below is a simple proof of nature 3.
A binary tree contains three vertices: vertices with zero degrees (that is, leaf nodes), which are recorded as n0, vertices with one degree, n1, vertices with two degrees, and n2. Then the Total node tree of a binary tree is n = n0 + n1 + n2. At the same time, we can clearly understand that the edge e of the binary tree is n-1. A vertices with two degrees contribute two edges. One degree contributes one edge to a vertex with zero degrees without contributing edges. So s = 2 * n2 + n1. From the two equations of e, we can obtain n0 = n2 + 1. PS: if you do not remember the nature of the question, you can push it yourself when writing the question!
Ii. binary tree traversal
1. Hierarchical Traversal
2. First-order traversal, middle-order traversal, and later-order traversal.
In the definition of mathematical expressions, the infix expression corresponds to the middle-order traversal, And the suffix expression corresponds to the back-order traversal.
The following describes the implementation of hierarchical traversal.
Hierarchical traversal is to traverse the binary tree from top to bottom and from left to right. For each node, you must print out the current node and print the Left and Right sons. Therefore, you can use a queue to store each node and its two sons. However, each node needs to print its own, which is also the son of its parent node. Therefore, in addition to the root node, each node must be printed twice. Therefore, only one queue is used to store the pointer of the node to be scanned, and another array is used to store the lady data field of the node to be printed. (Directly record pointer alive) for the code, see:
DFS () {Node * q [MAXN]; int ans [MAXN]; int front = 1, rear = 1; q [front] = root; while (front <rear) {Node * u = q [front ++]; // each time a Node is retrieved from the first Node of the queue, this ensures that no node obtains the if (u-> data = 0) break only once; // The data field of the node is empty, the cyclic ans [n ++] = q [front] is introduced; // store the nodes in the output queue if (u-> l! = NULL) q [rear ++] = u-> l; // Add the left son to the queue if (u-> r! = NULL) q [rear ++] = u-> r; // Add the right son to the queue }}
In fact, taking a closer look, this is like traversing the image's breadth first (BFS ).
It is easy to use the first-order traversal, the middle-order traversal, and the later-order traversal. It is written recursively. However, there is a problem about rebuilding Binary Trees. It is the result of first-order traversal of a binary tree and Middle-order traversal to find the result of the second-order traversal. Or, you can know the result of his post-order traversal and Middle-order traversal to find the result of his first-order traversal. Note that the central order cannot be obtained from the first and last orders. Because the root node cannot be found. Binary Tree Reconstruction uses recursion to easily write code.
Void rebuild (int n, char * s1, char s2, char s) {if (n <0) return 0; int p = strchr (s2, s1 [0]) -s2; // use the strchr function to quickly find the root node build (p + 1, s1 + 1, s2, s); // recursively construct the left subtree, here p + 1 is taken as the root node build of the Left subtree (n-p-1, s1 + p + 1, s2 + p + 1, s); // recursive construction of the right subtree, here let's use n-p-1 as the root node of the right subtree. In this way, the recursion continues until s [n-1] = s1 [0]; // returns the post-order traversal}
Write the binary tree here. Write the huffman tree next time! Continue ~~~
Iii. optimal binary tree
If each edge band of a binary tree has a valid value, the tree with the minimum sum of the edge length and the weight of the edge is the optimal binary tree .. WPL is called the weighted path length of the tree.
The smallest of WPL is the optimal binary tree.
But how to build an optimal binary tree? Here, the introduction to algorithms has a pseudo code, which is easy to write. In fact, algorithm books should be written in this way, ignoring the details of the language and focusing on expressing algorithms. You can take a look at the ultraviolet A 11234 Expressions. Although this question is not a huffman tree, the operations are the same. They store the tree pointer, but one is stack and the other is priority queue. This question is also used for hierarchical traversal. Oh, you have to brush this question!
Stack, queue, and binary tree traversal are very closely linked. I feel a lot of gains from the question of the ultraviolet 11234-Expressions. Well, it is a good sequence of lrj columns.