[Note: Relevant Concepts come from classic textbooks, Baidu encyclopedia, And Wikipedia]
Tree
Tree chartIt is a data structure that consists of n (n> = 1) finite nodes and a set of hierarchies. It has the following features:
- Each node has zero or multiple subnodes;
- A node without a parent node is called a root node;
- Each non-root node has only one parent node;
- In addition to the root node, each subnode can be divided into multiple non-Intersecting Subtrees;
:
Related Concepts:
- Node degree: The number of Subtrees contained in a node is called the degree of the node;
- Tree degree: The degree of the largest node in a tree is called the degree of the tree;
- Leaf nodeOrTerminal node: Zero-degree node;
- Non-terminal nodeOrBranch node: A node with a minimum degree;
- Father's DayOrParent node: If a node contains a subnode, this node is called the parent node of its subnode;
- Child NodeOrSubnode: The root node of the subtree contained in a node is called the child node of the node;
- Sibling Node: A node with the same parent node is called a sibling node;
- NodeLevel: Starting from the root definition, the root layer is layer 1st, and the root sub-node is layer 2nd, and so on;
- TreeHeightOrDepth: The maximum hierarchy of nodes in the tree;
- Cousin Node: The parent node is a cousin of each other at the same layer;
- Node ancestor: All nodes from the root node to the branch of the node;
- Descendant: Any node in the subtree rooted in a node is called the child of the node.
- Forest: The set of trees that are separated by M (M> = 0) is called forest;
Types of trees
- Unordered tree: There is no sequential relationship between subnodes of any node in the tree. This tree is called an unordered tree or a free tree. [information propagation diagram]
- Ordered Tree: The subnodes of any node in the tree have an ordered relationship. This tree is called an ordered tree. [family genealogy]
- Binary Tree: a tree with a maximum of two Subtrees is called a binary tree;
- Full Binary Tree: assume that the depth of a binary tree is D (D> 1 ). Except layer D, the number of nodes in other layers has reached the maximum value, and all nodes in layer D are arranged from left to right consecutively. Such a binary tree is called a Complete Binary Tree; [The first to last layer is not twice the second to last layer. N nodes are missing]
- Full Binary Tree: For the Complete Binary Tree above, if all the nodes in layer D are removed, the rest constitutes a full binary tree (at this time, the depth of the full binary tree is D-1 ); [The first to last layer is twice the second to last layer]
- Hoffmann tree: the shortest binary tree with the right path is called the Hoffmann tree or the optimal binary tree. [all people who have learned the information theory know the Hoffmann code, old Nb]
- B tree [data size of each layer is ordered]
Binary Tree
Each node of a binary tree has at most two Subtrees (nodes with no degree of presence greater than 2). the Subtrees of a binary tree have left and right points, and the order cannot be reversed. The I-th layer of a binary tree has at most nodes. The binary tree with a depth of K has at most [calculate by the Sum Formula of the proportional series] nodes. For any binary tree T, if the number of terminal nodes is 2, then.
There are three major differences between trees and binary trees:
- The number of nodes in a tree must be at least 1, and the number of nodes in a binary tree can be 0;
- The maximum number of nodes in the tree is unlimited, while the maximum number of Binary Tree nodes is 2]
- Tree nodes have no left or right, while Tree nodes have left or right.
Access Binary Tree
L, D, and r represent traversing the left subtree, the access root node, and the right subtree, respectively. There are 6 possibilities for sorting and combination. The following three traversal methods are common based on the root location. First, followed by the root node. Note: To a certain extent, Wikipedia instructs humans and fish instead of fish, and cannot confuse them.
Forward (first) sequential Traversal
The first (Root) Order to traverse a binary tree is DLR. [Note: both are l first and R later .] ----- The following three pictures are from the Courseware
Explanation: each node is traversed in the order of DLR. The same below.
Sequential Traversal
The order for Traversing binary trees in the middle (Root) Order is LDR. [Note: both are l first and R later .]
Post-order traversal
The order for Traversing binary trees in the post (Root) Order is LRD. [Note: both are l first and R later .]
Example
The example is from the Internet and courseware. The analysis is written by the blogger. If any error occurs, please correct it.
Example 1:
Sequential traversal (root left and right DLR): abdec
In-order traversal (left root right LDR): dbeac
Post-order traversal (left and right root LRD): debca
Example 2:
First-order traversal (root left and right DLR): abcdefghk [simplest. Write down the root, and then write down the root of the Left subtree. If the left subtree still has a subtree, continue searching until the leaf node! Because the root is left and right, write the root first, that is, write down what you encounter! You only need to pay attention to writing left at the same layer. If there is a subtree on the left, continue to traverse the subtree .]
In-order traversal (left root right LDR): bdcaehgkf [difficult to write, easy to make mistakes. Start searching from the left subtree, and there is a subtree In the subtree. Continue searching for its left subtree until it encounters a leaf node (d) or no left subtree (B. In this example, B does not have a left subtree, so first write, then traverse the right subtree, And the right subtree also traverses from its left subtree, so it is BCD; relative to the root node, the left subtree is traversed completely, so it is bdca. Next, traverse the right subtree of the root node. Following the steps above, E (which has no left subtree) H (leaf node, it has no left subtree and is G's L) g (itself is root d) K (relative to node g, it is R) g (G is F's L) F]
Post-sequential traversal (left and right root LRD): dcbhkgfea [first left subtree, then right subtree, and finally root node; if there is no left subtree, find the right subtree of this node, find the leaf node in the right subtree In the LRD order until it finds the leaf node (concept above) and starts to trace and write it down, which is equivalent to a stack!]
Example 3: how to find the descending order based on the Ascending Order:
It is known that the forward traversal is gdafemhz, and the middle traversal is adefghmz. Draw this binary tree.
① Traverse features in the forward order, we know that the root node must be at the first position, so it is g;
② Traverse features in the middle order. The adef on the left of the root node G must be the left subtree of the root node, and the hmz on the right of the G must be the right subtree of the root node;
③ Repeat the preceding steps based on the features in the forward and middle order. Recursively locate the child root node;
We can plot this binary tree:
As shown in the figure, the order of post-order traversal is aefdhzmg.
How to find the forward order in the descending order:
The central sequence and the post sequence of a binary tree are bdceafhg and decbhgfa respectively. Draw this binary tree.
Analysis:
① Traverse features in descending order, and the root node must be at the end of the descending order sequence (that is, );
② Traverse features in the middle order, and the root node must be in the middle, and the left part must be the descendant of the Left subtree (bdce ), the right part must be the descendant of the right subtree (fhg );
③ Recursively locate the child root node.
We can plot this binary tree:
Note:
In the left subtree, the order is bdce, and the subsequent order is decb. This indicates that B is the left subtree of A, and C is the right subtree of B (from bdce) root Node (from DCE and DEC );
In the right subtree, the order is fhg, and the subsequent order is HGF, indicating that F is the root node of the right subtree of A, and H is the left subtree of G.
C ++ implementation
Anti-crawler: no more.
This article by @ the_third_wave (blog address: http://blog.csdn.net/zhanh1218) original. Otherwise, it will be updated from time to time. please correct me if any error occurs.
If you find this blog post incomplete, that is why I want to prevent crawlers from releasing them in half. Please refer to the original author's blog.
If this blog post is helpful to you, you are not recommended to repost it for a good network environment. We recommend that you add it to your favorites! If you must reprint the content, please include the suffix and the address of this article.
Tree and binary tree summary and Algorithm Implementation