tree definition
A tree is a finite set of n nodes. When n = 0, it is called an empty tree.
A non-empty tree should meet the following conditions
There is one and only one specific node, called the root, with no direct predecessor and zero or more direct successors;
The remaining n-1 nodes can be divided into m disjoint finite sets, and each finite set is a tree, called a subtree of the root. The root node of each subtree has one and only one direct predecessor, but zero or more direct successors.
Basic terminology for trees
Node: contains a data element and several branches pointing to its subtree
Degree of node: the number of subtrees owned by the node
Degree of the tree: the maximum value of the degree of each node in the tree
Leaves: nodes with degree 0, also called terminal nodes;
Non-terminal nodes and branch nodes: nodes with a degree other than 0. Branch nodes other than the root node are called internal nodes
Children: The root of the subtree of a node is called the child of the node.
Parents: This node is called the child’s parents
Brothers: Children of the same parents are called brothers
Nodes between parents on the same level are called cousins
Path, path length: The path refers to the set of nodes passed from the root node to any node, and each one is the successor of the latter one, that is, they are all different layers and have the shortest descent. The path length is the number of nodes the path passes - 1.
Node level: starting from the root node is defined as the first level, and so on.
Depth: The maximum level of the hierarchy.
Binary tree
definition
The degree of each node is not greater than 2;
The order of the child nodes of each node cannot be reversed arbitrarily, so the children of the tree are divided into left children and right children.
Full binary tree and complete binary tree
Full binary tree↑
nature:
- There are at most 2^(i-1) nodes on the i-th level of a binary tree; there are at most m^(i-1) nodes on the i-th level of a tree with degree m
- A binary tree with depth k has at most 2^k-1 nodes; an m-ary tree with depth k has at most m^k-1/m-1 nodes.
- For any binary tree, if the number of terminal nodes is n0 and the number of nodes with degree 2 is n2, then n0 = n2+1
- The depth of a complete binary tree with n nodes is K = (log2n) rounded down + 1 or (log2(n+1)) rounded up
- For node number i in a complete binary tree :
- i = 1, the node is the root of the binary tree
- i > 1, the parent number is (i/2), rounded down;
- When i is an even number, it is the left child of the parent node. The parent node number is i/2,
- When i is an odd number, it is the right child of the parent node. The number of the parent node is (i-1)/2
- The left child number of the node numbered i is 2i, and the right child number is 2i+1
- n is the number of nodes, 2i>n, i has no left child; 2i+1>n has no right child
- n is the number of nodes
- If it is an odd number, then each node has both sons and daughters.
- If it is an even number, the branch node with the largest number only has the left child.
Storage structure of binary tree (two types)
Sequential storage and chained storage
sequential storage
It is stored in an array according to the node number, and the subscript position in the array is i-1
General binary tree sequential storage: storage in complete order according to node numbers and subscripts;
In the worst case, a right single branch binary tree with depth k requires 2^k-1 storage space.
chain storage
Binary tree traversal
Recursive implementation
preorder traversal
Visit the node -> traverse the left child in order -> traverse the right child in orderinorder traversal
In-order traverse the left child -> visit the node -> in-order traverse the right childSubsequent traversal
Subsequent traversal of the left child -> subsequent traversal of the right child -> access to the node
The results of in-order traversal and the results of pre-order and post-order traversal can restore a binary tree; but the results of pre-order and post-order traversal cannot restore a binary tree.
How to find the output sequence?
Draw a line from left to right starting from the root node, and each pass through the node is recorded as one pass; after drawing a circle, each node will pass three times, and the order is to output the node content during the first pass; in The order is to output the node content during the second pass; the postorder is to output the node content during the third pass. Note that the terminal node should be stepped twice to indicate the completion of the traversal of the left and right children of the node.
clue binary tree
In order to increase space utilization, when there is no left child, the pointer to its predecessor is stored; when there is no right child, the pointer to its successor is stored, and two more tags are stored. When 0, it means storing the left child, and when 1, it means What is stored is the successor.
Determination of the successor: First output the binary tree according to the first-center-then-traversal method, and then you can know the successor of each node. If there is no predecessor or successor, points to NULL
Access the binary tree to find the predecessor or successor of the specified node
Tree storage structure (three methods)
parent representation
Use a set of continuous spaces to store the nodes in the book. While saving each node, a pointer is attached to indicate the position of its parent node in the table.
DATA|Parent
Stored in the form of an array;
Advantages: Find mom quickly;
Disadvantages: difficult to find children;
child representation
Option 1. The number of pointer fields is equal to the degree of the tree. The degree of the tree is the maximum value of the degree of each node in the tree.
Number of empty link domains S=nk-(n-1) = n(k-1)+1 //k degree, n nodes
Solution 2: Make the number of pointer fields of each node equal to the degree of the node. Dedicate a location to store the number of pointer fields
Human words: allocate storage space according to the number of children stored
Since the linked lists of each node have different structures, and the degree of the node needs to be maintained, it will cause time loss in calculations.
Option 3, child linked list method
Arrange the child nodes of each node to form a singly linked list, called a child linked list. n nodes produce n child linked lists (the child linked lists of leaf nodes are empty lists), and the data of n nodes and the head pointers of n child linked lists form a sequence list.
child brother representation
The storage structure is similar to a binary tree, which facilitates various operations on the tree.
tree to binary tree
- Add a horizontal connection between all adjacent sibling nodes
- For each non-leaf node k, except for its leftmost child node, delete the connections between k and other child nodes.
- All horizontal lines are rotated 45 degrees clockwise (...?) with the left node as the axis, making the structure clearly hierarchical.
forest to binary tree
Forests can also conveniently use child brother lists.
- Convert each tree in the forest into a binary tree
- The first binary tree does not move. Starting from the second tree, the root node of the next binary tree is used as the right child of the root node of the previous binary tree. When all binary trees are connected together, the resulting binary tree is the binary tree obtained by forest transformation.
Binary tree reduced to tree or forest
Restore a binary tree to a tree or forest.
- If a node is the left child of its parents, then connect the right child of the node, the right child of the right child, the right child of the right child of the right child...all with the parent nodes of the node.
- Delete all connections between parent nodes and right child nodes in the original binary tree
- Organize the trees or forests obtained in the first two steps to make them look better.
Traversing trees and forests
Tree traversal: root traversal first
If the tree is not empty, then traverse the method:
- Access the root node (read storage information)
- From left to right, traverse each subtree of the root node first.
Equivalent to preorder traversal of the converted binary tree
Tree traversal: back root traversal
If the tree is not empty, the traversal method is:
- From left to right, traverse each subtree of the root node in sequence.
- Access the root node (read storage information)
Equivalent to in-order traversal of the converted binary tree
Taking the above figure as an example, the sequence traversed at this time: EBHFGCDA
Forest traversal: pre-order traversal
If the forest is not empty, the traversal method is:
- Visit the root node of the first tree in the forest.
- Pre-order traverse the subtree forest of the root node of the first tree
- Preorder traversal of the forest consisting of the remaining trees after removing the first tree
Equivalent to preorder traversal of the converted binary tree
Forest traversal: in-order traversal
When the forest is not empty, the traversal method is
- Inorder traverses the subtree forest of the root node of the first tree in the forest
- Visit the root node of the first tree
- In-order traversal of the forest consisting of the remaining trees after removing the first tree
Equivalent to in-order traversal of the converted binary tree
Restore a tree based on its root-first traversal and post-root traversal sequences?
Observe that in groups, divide first, the first one of each group in the root-first traversal sequence and the last one in each group of the root-posterior traversal sequence are the same! And this element is the root node of the group. Then group them to find correspondences.
Restore the forest based on the first and middle traversal sequences of the forest?
Observe, in the same way of finding groups, the first time you find the groups there are several trees, and then you can restore them according to the previous principles.
Huffman tree and its applications
Path length:
How many steps does it take to get from one node to another?
Weighted path length:
Add weight to each path
Huffman tree
Assume that a binary tree has n weighted leaf nodes, then the sum of the path length from the root node to each leaf node and the product of the corresponding node weight is called the weighted path length of the binary tree
A binary tree with a minimum weighted path length is called a Huffman tree
As long as the leaf weights are fixed, there is a fixed optimal binary tree with the shortest weighted path length, which is called a Huffman tree.
Given the weight w = (1, 3, 5, 7), construct a Huffman tree
Each time, the root node with the smallest weight is taken, and the sum is the parent node, and this is constructed.
Huffman coding
coding
How to encode the message so that the length of the message is as short as possible?
Design codes with different lengths so that any code is not the prefix of another code, then such a code is called a prefix code.