Recursive definition of a tree
A tree is a finite set of n (n>0) nodes that meet the following conditions:
⑴ has and has only one node without a precursor (Father node), the node is called the root of the tree;
⑵ Root, each of the remaining nodes has and has only one precursor;
⑶ root, each node is connected to the root by a unique path (otherwise there is a ring). This path starts with the root, and the end is on that node, and root, each node on the path is the successor of the previous node (son node);
The above definition indicates that the tree structure has no closed loops.
Classification of nodes
Nodes are generally divided into three categories.
⑴ root node: No father's knot. There is only one root node in the tree. such as node R
⑵ Branch node: Outside the root node, a node with a child is called a branch node. such as A,b,c,x,t,d,i
⑶ leaf node: a node without a child is called a leaf. such as W,h,e,f,s,m,o,n,j,u
The path of the root node to each branch node or leaf node is unique.
The only path from root r to node I is rcti.
The degree of the tree
⑴ Node Degree: The number of subtrees of a node is called the degree of the node. If the degree of node I is 3, the degree of nodal T is 2, and the degree of Node B is 1. Obviously, the degree of all leaves is 0.
⑵ the degree of the tree: the maximum degree of all nodes is called the degree (width) of the tree. The following trees have a degree of 3. If you use an array to store child node addresses, you should define the array size according to the degree of the tree
The depth of the tree
The tree is layered. The level at which the node is located is calculated from the root. The root node is on the first level, the son of the root is on the second level, and the rest of the layers are in turn. That is, a node is at level K, then the back of the node is on the k+1 layer.
In a tree, the parent node is a sibling of all the nodes in the same layer.
The largest level in the tree is called the depth of the tree, also known as height. The depth of the tree in the figure is 5.
Forest
The so-called Forest refers to the collection of several disjoint trees. Remove the root node, its original three subtrees tree TA,TB,TC collection {TA,TB,TC} is the forest, the three subtrees tree specific form is as follows:
Ordered Tree and unordered tree
The tree can be divided into ordered tree and unordered tree according to whether the same layer nodes in the tree are kept orderly.
(1) If the same layer nodes in the tree are arranged from left to right, their order is not interchangeable, such trees are called ordered trees;
(2) If the order of the same layer nodes is arbitrary, such a tree is called an unordered tree.
How to represent a tree
⑴ Tree Representations of nature:
Use nodes and edges to represent trees, for example by using the tree representation of nature. Tree representations are commonly used to analyze problems.
Advantages: Intuitive, image, disadvantage: difficult to save.
⑵ Bracket notation:
First put the root node in a pair of parentheses, and then put its subtree in the left and right order in parentheses, and the subtree is also handled by the same method: the same layer of the subtree and its root node surrounded by parentheses, the same layer of sub-tree separated by commas, and finally enclosed in parentheses. Examples can be written in the following form
(A (B (E (k,l), F), C (G), D (H (M), i,j)))
Advantages: Easy to save; Cons: Not intuitive
Traversal rules for trees
The so-called tree traversal, refers to a certain pattern of non-repeated access (or take out the information in the node, or other processing node) each node in the tree, the traversal process is essentially the tree this nonlinear structure to a certain regularity into a linear structure.
Traverse in the first root order
Post-root sequence traversal
Traverse Tree in Root order
The traversal rule for the first root order traversal is: Exit if the tree is empty, otherwise root the root of the tree, and then root through each subtrees tree of the root first. For example, the tree in the right image is traversed in the first order, and the order of formation is as follows:
Rawxdhebfcstimonju
The algorithm is:
Program preorder (V:integer);
{Access processing node V;
For i in Adj (v) Does if I is not visited then preorder (i);
}
Back Root sequence traversal tree
The traversal rule for the post-root sequence traversal is: If the tree is empty, then exit, otherwise the root accesses every subtrees tree and then accesses the root node. For example, the tree that is shown on the right shows the following root order traversal, which is formed in the order:
Whdexafbsmonijtucr
The algorithm is:
Program Postorder (V:integer);
{For i in Adj (v) does if I do not have access to then postorder (i);
Access processing node V;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Basic concepts and traversal rules for trees