Binary Trees, Trees and Forests
Examination Content
Definitions of Binary Trees, Trees, and Forests
Tree:
A tree (Tree) is a finite set of n (n>=0) nodes, it is either an empty tree (n= 0); or a non-empty tree, for a non-empty tree T:
- There is one and only one node called the root;
- The rest of the nodes except the root node can be divided into m (m>0) mutually disjoint finite sets T1 , T2 , ..., each of which is a tree itself
and is called the subtree of the root.
Binary tree:
Binary Tree (Binary Tree) is a collection of n (n>=0) nodes, it is either an empty tree (n= 0); or a non-empty tree, for a non-empty tree T:
- There is one and only one node called the root;
- The remaining nodes except the root node are divided into two mutually disjoint subsets T1 and T2, which are called the left subtree and right
subtree of T respectively, and T1 and T2 are both binary trees.
The difference between a binary tree and a tree:
- Each node of the binary tree has at most two subtrees (that is, there are no nodes with a degree greater than 2 in the binary tree);
- The subtrees of a binary tree are divided into left and right, and their order cannot be reversed arbitrarily.
Implementation of binary tree (including sequential storage structure and chain storage structure), traversal of binary tree;
Sequential storage structure:
Chain storage structure:
Traversal of a binary tree:
- preorder traversal
If the binary tree is empty, do no operation; otherwise
(1) visit the root node;
(2) traverse the left subtree in preorder;
(3) traverse the right subtree in preorder.
- Inorder traversal
If the binary tree is empty, do a null operation; otherwise
(1) traverse the left subtree in order;
(2) visit the root node;
(3) traverse the right subtree in order;
- post order traversal
If the binary tree is empty, then no operation; otherwise
(1) post-order traversal of the left subtree;
(2) post-order traversal of the right subtree;
(3) visit the root node
Applications and extensions under the binary tree structure, such as binary search tree, 2-3-4 tree, Huffman coding and heap
binary search tree
2-3-4 tree
Huffman coding
The Huffman tree, also known as the optimal tree, is a tree with the shortest weighted path length.
heap
The definition of balanced binary tree, the definition of balance factor and the rotation operation of balanced binary tree
balanced binary tree
balance factor
Balance the rotation of a binary tree:
The storage structure of trees and forests, the traversal of trees and forests, and the conversion of forests and binary trees;
Storage structures for trees and forests
- parent notation
In this representation method, the nodes of the tree are stored in a group of continuous storage units
, and besides the data field data, each node also has a parent field attached to indicate the position of its parent node.This storage structure takes advantage of the property that each node (except the root) has only one parent. Under this storage structure,
it is very convenient to find the parents of the node, and it is also easy to find the root of the tree, but it is necessary to traverse the entire structure to find the children of the node
- child representation
Since each node in the tree may have multiple subtrees, multiple linked lists can be used, that is, each node has multiple pointer fields, and each
pointer points to the root node of a subtree
- child brother law
Also known as binary tree representation, or binary linked list representation, that is, a binary linked list is used as a tree storage structure. The two chain domains of a node in the linked list point to the first child node and the next sibling node of the node respectively, named firstchild domain and nextsibling domain respectively
The advantage of this storage structure is that it is exactly the same as the binary linked list representation of the binary tree, so the general tree structure can be converted into a binary tree for processing, and the operation on the tree can be realized by using the algorithm of the binary tree. Therefore, the child-brother representation is a commonly used tree storage representation method.
Forest to Binary Tree Conversion:
- Convert forest to binary tree
If F={T1, T2,..., several} is a forest, it can be transformed into a binary tree B= (root, LB, RB) according to the following rules.
(1) If F is empty, that is, m= o,', then B is an empty tree;
(2) If F is not empty, that is, m"'F-0, then the root of B is the first tree in the forest Root ROOT(T1 ); The left subtree
LB of B is a binary tree converted from the subtree forest F{T11, T12, ..., T1m} of the root node in Wu; its right subtree RB is converted from the forest F'
= {T2, T3 , …,several} Converted binary tree
- Convert binary tree to forest
If B= (root, LB, RB) is a binary tree, it can be transformed into a forest F={T1, T2, ..., Tm} according to the following rules: (1)
If B is empty, then F is empty;
(2 ) if B is not empty, then the root ROOT(T1) of the first tree in F is the root of the binary tree B; the subtree of the root node in T1 > tree forest F1 is converted from the left subtree LB of B Forest; the forest F'={T2 , T3 , ...} composed of trees except T1 in F is a forest converted from the right subtree RB of B.
Tree and Forest Traversal
When representing a tree in terms of sons and brothers of a binary tree
The first-root traversal and post-root traversal of the tree can be implemented by borrowing the algorithm of the pre-order traversal and in-order traversal of the binary tree:
The first root traversal of the tree == the preorder traversal of the binary tree
The back root traversal of the tree == the inorder traversal of the binary tree