Chapter 4 tree
6.1 tree Definition
Linear Structure: one-to-one. Tree Structure: one to multiple. Graphical structure: many to many.
1. Tree) is a finite set of n (n> = 0) nodes. When n = 0, it is called an empty tree. In any non-empty tree: 1) There is only one specific node called Root; (2) When n> 1, other nodes can be divided into m (m> 0) finite sets T1, T2 ,......, tm, in which each set itself is a tree, also known as the SubTree of the root ).
1) the tree in the data structure can only have one root node.
2) There is no limit on the number of Subtrees, but they must be different from each other.
2. the node of the tree contains a Data Element and several branches pointing to its subtree. The number of Subtrees owned by a node is called the Degree of node Degree ). A node with 0 degrees is called a leaf node or a terminal node. A node with a degree of less than 0 is called a non-terminal node or a branch node. Besides the root node, the branch node is also called an internal node. The tree degree is the maximum degree of each node in the tree.
3. The root of the Child tree of a node is the Child of the node. A brother is a child of the same parent. The ancestor of a node is all the nodes from the root to the branch of the node. On the contrary, any node in the subtree with a node as the root is called the descendant of the node.
4. the nodes at the same level of parent and child are cousins of each other. The maximum hierarchy of nodes in the tree is called the Depth of the tree) or height. If the subtree of a node in a fruit tree is ordered from left to right and cannot be exchanged, the tree is called an ordered tree. Otherwise, the tree is called an unordered tree.
5. Forest) is a set of trees that do not meet each other in m (m> = 0. For each node in the tree, the set of its Subtrees is the forest.
Linear Structure:
1) the first data element: NO precursor.
2) last data element: no successor.
3) intermediate element: One precursor and one successor.
Tree Structure:
1) root node: it has no parent and is unique.
2) leaf nodes: there are no children and there can be multiple leaf nodes.
3) intermediate node: one parent and multiple children.
6.2 tree storage structure
Sequential storage and chained storage.
6.2.1 parental Representation
Except for the root node, each node may not have children, but it must have only one parent.
The node structure consists of a data field and a pointer field.
Data is a data domain that stores the data information of nodes. Parent is the pointer field. The subscript of the parent nodes in the array is stored in an array ). Set the pointer field of the root node to-1.
Similarly, there are child representation and child sibling representation.
6.3 Binary Tree
6.3.1 Binary Tree Definition
1. Binary Tree) is a finite set of n (n> = 0) nodes. The set or empty set is called an empty Binary Tree ), it can also be composed of a root node and a left subtree that does not conflict with each other and is called a root node, and a binary tree that has Subtrees.
Features:
1) each node has a maximum of two Subtrees, And the binary tree does not have nodes with a degree greater than 2. There are not only two Subtrees, but at most.
2) The left and right subtree are ordered.
3) even if a node has only one subtree, it must be differentiated between the left subtree and the right subtree.
2. Special binary tree.
1) oblique tree, left oblique tree, and right oblique tree.
2) Full Binary Tree
3) Complete Binary Tree.
6.3.2 nature of Binary Trees
1. layer I of a binary tree can have up to 2 I-1 nodes.
2. A k-depth binary tree consists of at most 2k-1 nodes.
3. For any binary tree, if the number of leaf nodes is n0 and the number of nodes whose degree is 2 is n2, n0 = n2 + 1;
Proof: Total node COUNT = n0 + n1 + n2; Total node COUNT = total branch count + 1; total branch count = n1 + 2n2;
Obtain n0 = n2 + 1;
4. The depth of the Complete Binary Tree with n nodes is [log2n] + 1.
5. If the depth of a Complete Binary Tree with n nodes is [log2n] + 1, for any node I (1 <= I <= n:
1) if I = 1, the node is the root of the binary tree and has no parent. If I> 1, the parent node is node [I/2].
2) If 2i> n, node I has no left child node and I is a leaf node. Otherwise, the left child is node 2i.
3) If 2i + 1> n, node I has no right child; otherwise, its right child is node 2i + 1;
6.3.3 Binary Tree Storage Structure
1. Sequential Storage
If it is a complete binary tree, it can be stored in sequence. If it is not a complete binary tree, the missing locations are also allocated storage space, but no data is stored. Waste of space.
2. chained Storage
Each node of a binary tree has two children at most. Therefore, a data domain and two pointer domains are designed for it. Such a linked list is called a binary linked list.
Typedef struct BitNode
{
Int data;
Struct BitNode * lchild, * rchild;
} BitNode, * BitTree;
6.3.4 traverse a binary tree
Binary tree traversal: access all nodes in the binary tree in sequence from the root node so that each node is accessed only once.
1. Forward traversal: root node, left subtree, right subtree.
Void PreOrderTraverse (BitTree T)
{
If (T = NULL)
Return;
Printf ("% c", T-> data);/* displays node data, which can be changed to other node operations. */
PreOrderTraverse (T-> lchild );
PreOrderTraverse (T-> rchild );
}
2. Middle-order traversal: Left subtree, root node, and right subtree.
Void InOrderTraverse (BitTree T)
{
If (T = NULL)
Return;
InOrderTraverse (T-> lchild );
Printf ("% c", T-> data);/* displays node data, which can be changed to other node operations. */
InOrderTraverse (T-> rchild );
}
3. Post-order traversal: Left subtree, right subtree, and root node
Void PostOrderTraverse (BitTree T)
{
If (T = NULL)
Return;
PostOrderTraverse (T-> lchild );
PostOrderTraverse (T-> rchild );
Printf ("% c", T-> data);/* displays node data, which can be changed to other node operations. */
}
The first node that traverses in the forward order is the root node. The root node that traverses in the middle will appear in the middle. The left side is the node of the Left subtree, and the right side is the node of the right subtree. The last node in the descending order is the root node.
A binary tree can be identified by pre-order traversal, middle-order traversal, post-order traversal, and Middle-order traversal. However, a binary tree cannot be uniquely identified by pre-order traversal and post-order traversal.
6.4 create a binary tree
1. Procedure
1) convert a binary tree into an Extended Binary Tree. An Extended Binary Tree is used to represent an empty child of a non-empty node in #, and then becomes an Extended Binary Tree. When the node encounters #, it is known that the node is empty.
2) You can traverse the Extended Binary Tree in the forward order and create different codes ).
For example, AB # D # C ##.
3) implemented algorithm pre-sequential traversal sequence)
Void CreatBitTree (BitTree * T)
{
Char ch;
Scanf ("% c", & ch); // enter AB # D # C ##
If (ch = '#')
* T = NULL;
Else
{
* T = (BitTree) malloc (sizeof (BitNode ));
If (! * T)
Exit (OVERFLOW );
(* T)-> data = ch;/* generate the root node */
CreateBitTree (& (* T)-> lchild);/* construct the left subtree */
CreateBitTree (& (* T)-> rchild);/* construct the right subtree */
}
}
6.5 clue Binary Tree
If the number of nodes in a binary tree is n, there are 2n pointer fields, but n + 1 contains NULL pointer fields. These idle locations are used to store the frontend and successor of the node. The corresponding binary tree is called a clue binary tree.
If the lchild of a node is empty, point it to the node's precursor and the ltag is 1. Otherwise, it points to the left child. ltag = 0;
If the rchild of a node is empty, point it to the successor of the node, and rtag = 1. Otherwise, point to the right child, rtag = 0;
This article is from "Li Haichuan" blog, please be sure to keep this source http://lihaichuan.blog.51cto.com/498079/1285890