Algorithm: Binary Tree

The tree diagram is a data structure, which is a hierarchical collection composed of n (n>=1) finite nodes. It is called a "tree" because it looks like an upside-down tree, which means that it has its roots facing up and its leaves facing down.
It has the following characteristics: each node has zero or more child nodes; the node without a parent node is called the root node; each non-root node has and only one parent node; except for the root node, each child node can be divided into Multiple disjoint subtrees;
Write picture description here

Definition:
Node : Contains a data element and several branches pointing to its subtree.
The degree of the node: the number
of subtrees the node owns. Leaf node : the node with degree 0 [node without subtree]
Branch node : the node with degree other than 0
Child : the root of the node's subtree [Direct successors, there may be multiple]
Parents : the immediate predecessor of the child [ Although it is a parent, there can only be one at most ]
Brother : Children of the same parent: Children and
grandchildren of the same parent : All node
ancestors in a tree rooted at a node : From the root to all the node
levels on the branch passed by the node : the root node is the first level, its children are the second level, and so on.
Depth : the largest level of nodes in the tree.
Forest : disjoint trees的合。 The combination. For each node in the tree, the collection of its subtrees is the forest. [Forests can be converted into binary trees]
Write picture description here

Type:
Unordered tree : There is no order relationship between the child nodes of any node in the tree. This kind of tree is called an unordered tree, also called a free tree.
Ordered tree : There is order between the child nodes of any node in the tree Relationship, this kind of tree is called an ordered tree
binary tree : each node has at most two subtrees.
Write picture description here
Complete Binary Tree
Write picture description here
Full Binary Tree
Write picture description here
Huffman Tree : The binary tree with the shortest weighted path is called Huffman tree or optimal binary tree

In the data structure algorithm, the main use of binary trees is more.
The properties of the binary tree:
1. There are at most 2^(i-1) nodes on the i-th level of
the binary tree. 2. A binary tree with a depth of k has at most 2^k-1 nodes.
3. If the number of terminal nodes of the binary tree is n0, the number of nodes with degree 2 is n2. Then n0=n2+1

Traversing the binary tree:
Tree traversal is to visit the nodes in the tree in a certain order. Each node is required to visit once and only once. Suppose that the access root node is denoted by D, and the left and right subtrees are denoted by L and R.
Write picture description here

First-order traversal:
1. If the binary tree is empty, return. Otherwise:
2. Visit the root node
3. First traverse the left subtree (L)
4. First traverse the right subtree (R)
Write picture description here
Traversal result: ABDEGCF

Middle-order traversal:
1. If the binary tree is empty, return; otherwise:
2. Middle-order traverse the left subtree (L)
3. Visit the root node (D)
4. Middle-order traverse the right subtree (R)
Write picture description here
traversal result: DBGEAFC

Follow-up traversal:
1. If the binary tree is empty, return; otherwise:
2. Follow-up traverse the left subtree (L)
3. Follow-up traverse the right subtree (R)
4. Visit the root node (D)
Write picture description here
Traversal result: DGEBFCA

For the creation process of the binary tree, you can refer to the blog post: click for reference

Guess you like

Origin blog.csdn.net/new_Aiden/article/details/50968125