According to Sister Luofang and notes made online
Semi-original, intrusion
The extremely important properties of binary trees
Degree 0 node number = Degree 2 node number + 1
Degree of tree
The nature of the binary tree
The definition of an international complete binary tree:
1. The leaf nodes are in the last layer or the penultimate layer
2. If there are leaf nodes, there must be two leaf nodes
1. Sequential storage
2. Chain storage
Typedef struct BiTNode
{
TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
Recursive algorithm for in-order traversal
void InOrderTraverse(BiTree T)
{
if(T)
{
InOrderTraverse(T->lchild);
cout<<T->data;
InOrderTraverse(T->rchild);
}
}
As for the pre-order and post-order traversal, only the order is exchanged
Non-recursive implementation of in-order traversal
void InOrderTraverse(BiTree &T)
{
InitStack(S);
p=T;
q=new BiTNode;
while(p||!StackEmpty(S))
{
if(p)
{
push(S,p);
p=p->lchild;
}
else
{
Pop(S,q);
cout<<q->data;
p=p->rchild;
}
}
}
Create a binary linked list in the order of preorder traversal
void CreatBiTree(BiTree &T)
{
ci>>ch;
if(ch=='#') T=NULL;
else
{
T=new BiTNode;
T->data=ch;
CreatBiTree(T->lchild);
CreatBiTree(T->rchild);
}
}
Copy Binary Tree
void Copy(BiTree &T,BiTree &NewT)
{
if(T==NULL)
{
NewT=NULL;
return;
}
else
{
NewT=new BiTNode;
NewT->data=T->data;
Copy(T->lchild,NewT->lchild);
Copy(T->rchild,NewT->rchild);
}
}
Calculate the depth of the
binary tree The depth of the binary tree is the maximum number of nodes in the tree, and the depth of the binary tree is the greater of the left and right subtree depths + 1
int Depth(BiTree &T)
{
if(T==NULL) return 0;
else
{
m=Depth(T->lchild);
n=Depth(T->rchild);
if(m>n)
return m+1;
else
return n+1;
}
}
Count the number of nodes in the binary tree
int NodeCount(BiTree &T)
{
if(T==NULL) return 0;
else
return NodeCount(T->lchild)+NodeCount(T->rchild)+1;
}
Haverman tree and its application
It feels like the most thorough explanation of the Huffman tree in the whole network
From the reason to the process, to the application is very comprehensive! ! ! Push! ! !
Huffman tree: also known as optimal tree, refers to the tree with the shortest weighted path length
Huffman coding:
For a Huffman tree with n nodes, if 0 is assigned to each left branch and 1 is assigned to the right branch in the tree, then the path from the root to each leaf node, the assignment of each branch constitutes a binary String, this binary string is called Huffman code
Prefix encoding:
In a coding scheme, any code is not a prefix (leftmost substring) of any other code, then the code is called a prefix code.
(It looks very daunting here)
Huffman coding satisfies two characteristics:
(1) Huffman coding prefix coding (that is, Huffman coding does not produce a prefix) that is to say, any Huffman code will not be completely identical to the prefix of any other Huffman code Overlapped.
(2) Huffman coding is the optimal prefix coding
example:
Example: If the message to be sent is'ABACCDA',
It only uses four kinds of characters, which can be distinguished by two binary codes.
Assuming that the codes of A, B, C, and D are 00, 01, 10, and 11 respectively, the above message will be '00010010101100' (14 bits in total), and the decoder can group and decode two bits to restore the original Message.
Can the total length of the code be shorter?
In practical applications, the appearance frequency of each character is different. Short (long) codes are used to represent characters with large (small) frequencies, which minimizes the total length of the code sequence and minimizes the total amount of space required
Data's minimum redundant coding problem
In the above example, if the codes of A, B, C, and D are assumed to be 0, 00, 1, 01, then the message'ABACCDA' will be '000011010' (9 bits in total), but this code is ambiguous: Can be translated as:'BBCCDA','ABACCDA','AAAACCACA' etc.
Uniqueness of Decoding
It is required that the encoding of any character cannot be the prefix of another character encoding. This encoding is called prefix encoding (in fact, it is a non-prefix code). Two issues should be considered in the encoding process, the least redundant encoding of data, the uniqueness of decoding, the use of optimal binary tree can solve the above two problems well
Design binary prefix code with binary tree
Construct a binary tree with the characters in the text as leaf nodes. Then lead the node in the binary tree to its left child's branch label '0' and lead to its right child's branch label '1'; the code of each character is the 0 obtained from the path from the root to each leaf, 1 Sequence. The result is the binary prefix code.
Example: If the message to be transmitted is'ABACCDA', that is, the frequencies (ie weights) of A, B, C, and D are 0.43, 0.14, 0.29, 0.14, respectively, try to construct a Huffman code
Example: If the message to be transmitted is'ABCACCDAEAE', that is, the frequencies (ie weights) of A, B, C, D, and E are 0.36, 0.1, 0.27, 0.1, 0.18 respectively, try to construct a Huffman code.
Assuming that the message used for communication consists of only 8 letters, the frequencies of letters appearing in the message are 0.07, 0.19, 0.02, 0.06, 0.32, 0.03, 0.21, 0.10, respectively.
① Try to design Huffman codes for these 8 letters.
② Try to design another equal-length coding scheme represented by binary.
③ For the above examples, compare the advantages and disadvantages of the two schemes.
Clue binary tree
By examining various binary linked lists, no matter what the shape of the tree, the number of empty chain domains is always more than the number of non-empty chain domains. To be precise, the binary linked list of each node of n has 2n chain domains, and there are n-1 non-empty chain domains, but there are n+1 empty chain domains.
Therefore, a method is proposed to use the original empty chain domain to store pointers to other nodes in the tree. Such pointers are called clues.
记ptr指向二叉链表中的一个结点,以下是建立线索的规则: (1)如果ptr->lchild为空,则存放指向中序遍历序列中该结点的前驱结点。这个结点称为ptr的中序前驱; (2)如果ptr->rchild为空,则存放指向中序遍历序列中该结点的后继结点。这个结点称为ptr的中序后继;