/**
* 通过前序遍历的数据序列反向生成二叉树
* A
* B C
* D E # F
* # # # # # #
* <p>
* ABD##E##C#F##
*/publicvoidcreateBinaryTreePre(ArrayList<String> data){createBinaryTree(data.size(),data);}public TreeNode createBinaryTree(int size, ArrayList<String> data){if(data.size()==0){return null;}
String d = data.get(0);
TreeNode node;int index = size - data.size();if(d.equals("#")){
node = null;
data.remove(0);return node;}
node =newTreeNode(index, d);if(index ==0){//创建根节点
root = node;}
data.remove(0);
node.leftChild =createBinaryTree(size, data);
node.rightChild =createBinaryTree(size, data);return node;}
Tree into a binary tree
1, plus line. Plus a connection between all the sibling nodes.
2, to the wire. Each node in the tree, leaving only its connection with the first child node, delete the connection between it and the other child nodes.
3, level adjustment. To the root of the tree as the axis, the rotation of the whole tree a certain angle clockwise, so that the structure structured. Note that the first child is left child node of a binary tree, brothers conversion over children is the right child node.
Forest conversion binary tree
1, to convert each binary tree.
2, fixed on a binary tree, starting from the second binary tree, the binary tree sequentially to the right child of the root node as a root node of a binary tree before, with the connecting lines. When after all the binary tree connected by the forest has been converted to a binary tree.
Convert binary tree
1, plus line. If the left child node of a node exists, it is the left child of the right child node, right child of the right child node, right child of the right child of the right child node ............ Ha, anyway, It is the left child of n.
2, to the wire. Delete the original binary tree nodes to connect all of its right child nodes.
3, level adjustment. So that structure structured.
Binary tree forest conversion
Determining a binary tree can be converted into a tree or forest, the standard is very simple, it is just by looking tree binary tree root has no right child, there is the forest, not a tree is. So if it is the conversion of forest, as follows: 1, start from the root, if the right child exists, the connection put the child node and the right to delete, and then view the binary tree after the separation, if the right child exists, then delete the connection ········ until all children the right connections are deleted so far, been the separation of a binary tree. 2, each of them then the separated binary tree can be converted to.
Huffman tree
Uncle Huffman said configuration path between two nodes from the tree to a node between the other branch nodes, the number of branches on path is called the path length. In a binary tree, the root node to the node D path length is 4, the binary tree root node to the node b, D a path length of 2. Is the path length from the root of the tree to each node, and the path length. A binary tree on a path length of 1 + 1 + 2 + 3 + 2 + 3 + 4 + 4 = 20. B binary tree on the path length of 1 + 3 + 3 + 2 + 2 + 2 + 2 + 1 = 16.
Considering junction weighted, weighted path length is the product of node weights from that node to the path length between the root and the node. Weighted path length tree tree is weighted in the path length and all leaf nodes. Suppose there are n weights {w₁, w₂, w₃, ····, wn}, there is configured a binary tree of n leaf nodes, each leaf node with weights wk, each leaf of the path length lk we generally denoted, wherein the weighted minimum path length WPL called binary Huffman tree. For example as follows:
We simplify this first binary two leaf nodes into binary weighted, wherein A represents failed, B represents pass, C represents moderate, D denotes good, E is excellent. Digital branch line of each leaf is we have just mentioned five-point scale score accounted logarithmic scale.
A binary tree of WPL = 5 × 1 + 15 × 2 + 40 × 3 + 30 × 4 + 10 × 4 = 315
The binary tree b WPL = 5 × 3 + 15 × 3 + 40 × 2 + 30 × 2 + 10 × 2 = 220
Huffman tree structure
1, first leaf nodes in accordance with the right values arranged in ascending order of an ordered sequence, i.e. A5, E10, B15, D30, C40.
2, take the first two junction minimum weight as a new node of the two child nodes N₁, note left child is relatively small, this is the left child N1 is A, E is the right child of N1, as FIG. 6-12-5, the new right node is a leaf of the two weights and 5 + 10 = 15.
3, replacing A with the N₁ E, ordered sequence is inserted, held in ascending order. That N₁15, B15, D30, C40.
4. Repeat step 2. The B N₁ and N₂ as a new node of the two child nodes. 6-12-6 shown in FIG. Weights N₂ = 15 + 15 = 30.
5, replacing the N₂ and N₁ B, ordered sequence insertion, holding in ascending order. That N₂30, D30, C40.
6. Repeat step 2. The N₂ D with a new node N₃ as the two child nodes. 6-12-7 shown in FIG. N₃ weights = 30 + 30 = 60.
7, replacing the N₂ and N₃ D, ordered sequence is inserted, held in ascending order. Namely: C40, N₃60.
8. Repeat step 2. The C and N₃ as a new node T of the two sub-nodes, as shown in FIG 6-12-8. Since that is the root of T, to complete the Huffman tree structure.
Huffman tree construction process
Just by step, we can come to construct Huffman tree Huffman algorithm description.
Binary search tree (Binary Search Tree), (Also: a binary search tree, binary sort tree) which is either an empty tree or a binary tree with the following properties: if its left subtree is not empty, then left values of all the sub-tree nodes are less than the value of the root node; if its right subtree is not empty, then the right sub-tree, all the nodes which are greater than the value of the root; its left , they were also right subtree binary sort tree.
Is divided into four cases, no left and right subtrees node (leaf node); no left subtree node; no right subtree of nodes; node has left and right subtrees.
1 to remove the node is a leaf node: delete.
2, delete the sub-tree node exists only left or right subtree: delete the parent node directly to the sub-tree node.
3, delete nodes exist left subtree and right subtree: will replace the deleted node node leftmost rightmost node in the left subtree delete nodes or right subtree, replace and delete nodes, and then delete nodes to sub-tree node.