Data Structures - Trees, Forests, Huffman Trees




  • tree and forest
1. The storage structure of the tree
(1) Parent representation: Using the property that each node has only one parent, an indicator is attached to each node to indicate the position of its parent node in the linked list
(2) Child representation: Arrange each child node as a linear list, and use a singly linked list as the storage structure
(3) Child sibling representation (called binary tree representation or binary linked list representation)
typedef struct CSNode{
  ElemType          data;
  struct CSNode *firstchild,*nextsibling;//The two chain fields of the node point to the first child node and the next sibling node of the node respectively
}CSNode,*CSTree;
2. Conversion of forests and binary trees
Both binary trees and trees can use a binary linked list as a storage structure, and a binary linked list can be used as a medium to derive a corresponding relationship between a tree and a binary tree.
(1) Convert the forest to a binary tree
F={T1,T2,...,Tm} means forest
B={root, LB, RB} represents a binary tree
The root of B is the root of the first tree (T1) in the forest; the left subtree LB is transformed from T1, and the right subtree is transformed from the rest.
(2) Convert binary tree to forest
The root of T1 is the root of B, and the rest of the trees are transformed from the right subtree
3. Forest and tree traversal
Ways to traverse the tree:
(1) First root traversal tree: first visit the root node of the tree, and then traverse each subtree of the root in turn, ABCDE
(2) Post-root traversal tree: first traverse each subtree in turn, and then visit the root node, BDCEA
Two ways to traverse the forest:
(1) Preorder traversal of the forest (non-empty):
  1. Visit the root node of the first tree in the forest
  2. Preorder traversal of the subtree forest of the root node in the first tree
  3. Preorder traversal of the forest of remaining trees after removing the first tree
(2) In-order traversal of the forest (non-empty):
  1. Inorder traversal forest of subtrees of the root node of the first tree in China
  2. visit the root node of the first tree
  3. Inorder traversal of the forest of remaining trees after removing the first tree
The pre-order and in-order of the above forest can be regarded as the pre-order and in-order traversal of the corresponding binary tree. When the binary linked list is used as the storage structure of the tree, the pre-root traversal and post-root traversal of the tree can be borrowed from the pre-order traversal of the binary tree. and inorder traversal algorithm implementation

Binary Sort Tree : Binary Sort Tree, also known as Binary Search Tree.
If it is not empty, it has the following properties: (1) If the left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node; 
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node; 
(3) The left and right subtrees are also binary sorting trees;

//Define a binary sorting tree structure
typedef int DataType;
typedef struct BST_Node {
    DataType data;
    struct BST_Node *lchild, *rchild;
}BST_T, *BST_P;
// Build a binary sorting tree, use the Insert_BST method
void CreateBST(BST_P *T, int a[], int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        Insert_BST(T, a[i]);
    }
}
Binary sorting tree search: It is very simple to find the largest and smallest elements in the binary tree. Go from the root node to the left until there is no way to go to get the minimum value; go from the root node to the right until There is no way to go, you can get the maximum value.
Binary sorting tree insertion: When inserting a new element, you can start from the root node, turn left when the key value is larger, and turn right when the key value is small, until the end, which is the insertion point.
Binary sorting tree deletion: (1) If A has only one child node, directly connect the child node of A to the parent node of A, and delete A;
      (2) If A has two child nodes, we replace A with the smallest node in the right subtree, how to get the smallest node, I have said before.

Balanced Binary Tree Definition (AVL) : If it is not an empty tree, it has the following properties: the absolute value of the difference between the depths of its left subtree and right subtree (balance factor) does not exceed 1, and its left subtree Both the tree and the right subtree are a balanced binary tree.
Balance factor (bf): The depth of the left subtree of the node minus the depth of the right subtree, then obviously -1<=bf<=1, here we define:
#define EH 0, #define LH 1, #define RH -1. The order is equal height, left height, right height.
//Define a binary tree and its balance factor
typedef struct _BitNode
{
     int data;
     int bf;//balance factor
    struct _BitNode *lchild,*rchild;
}BitNode,*BiTree;
Balanced binary tree is faster when searching

Huffman tree: also known as the optimal tree, is a kind of tree with the shortest weighted path length
Huffman algorithm:
Basic idea: make the node with larger weight close to the root
* According to the given n weights {w1,w2,...wn}, construct n binary trees with only root nodes.
* In the forest, select two trees with the smallest root node weights as left and right subtrees, construct a new binary tree, and set the root node weight of the new binary tree as the sum of the weights of its left and right subtree root nodes.
* Delete these two trees in the forest and add the newly obtained binary tree to the forest.
* Repeat the above two steps until there is only one tree, which is the Huffman tree.

Huffman coding:
If you want to design encodings of different lengths, the encoding of any character must not be a prefix of another encoding. This encoding is called prefix encoding
Basic idea: use short codes for characters with high probability, use long codes for small characters, and construct a Huffman tree
example:

A few conclusions about Huffman coding:
* Huffman encoding is unequal length encoding
* Huffman encoding is a prefix encoding, that is, the encoding of any character is not a prefix of another character encoding
* There are no nodes of degree 1 in the Huffman coding tree. If the number of leaf nodes is n, the total number of nodes in the Huffman coding tree is 2n-1
* Sending process: Send character data according to the encoding table obtained by the Huffman tree
* Receiving process: According to the provisions of left 0 and right 1, go from the root node to a leaf node to complete the decoding of a character. Repeat this process until the end of receiving data


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324524639&siteId=291194637