First, preface
Project source code and other statements, etc. see Data structure (i) linear structure chapter.
second, related concepts
Tree as a widely used one-to-many non-linear data structure, not only the point of the relationship between the information, there are hierarchical relationships, examples are shown in Figure I. Because the structure of the tree is more complex, in order to simplify the operation and storage, we generally convert the tree to two tree processing, so this paper mainly discusses the binary tree.
- Two-fork Tree
A binary tree is a tree structure with a maximum of two child nodes per node, and if the root node is moved, the remaining nodes are divided into two disjoint subtree, called Saozi right subtree, respectively. Binary tree is an ordered tree, the left and right sub-tree has a strict order, if reversed to become a different two-fork tree.
- Full two fork Tree
Full two fork tree, as the name implies that all nodes except the leaf node have two children, and the leaf node in the same layer of two fork tree, example shown in Figure Ii.
- Complete binary Tree
Complete binary tree, after removing the last layer of node is full two fork tree, and the last layer of nodes are continuously concentrated on the left, the example is shown in Figure three.
Three or two fork tree storage structure
- Sequential storage
According to the characteristics of the complete binary tree, we can calculate the number of the parent node and the left and right child nodes of any node n, so the nodes of the complete binary tree can be stored in a one-dimensional array sequentially from the top to the bottom. The non-complete binary tree storage should be transformed into a complete binary tree, empty replacement of non-existent nodes, the comparison of waste storage space, storage is shown in Figure four.
- Chain-Store
A tree-structured chain store is similar to a linear structured chain store, which defines the nodes that contain data and reference domains, and then stores the relationships between nodes by referencing the domain. According to the structure of the binary tree, nodes node contains at least data domain (the database), reference domain (left child lchild, right child rchild), in order to facilitate the child node to find the parent node, the reference domain can consider adding a parent node reference (parent), storage as shown in Figure five.
Four, tree and two-fork tree conversion
- Tree Turn two fork tree
Add a line and add a link between all the sibling nodes.
Erase line, to each node in the tree, keep only the connection between him and the first child's node, and remove the connection between it and other children's nodes.
Finishing, finishing the first two steps to get the tree, so that the structure of a clear hierarchy.
- Two-fork tree to Tree
Add line, if the left child node of a node exists, the left child knot point of the right child node, right child knot point of the right child node ... As the node's child node, connect the node with these right child nodes.
Erase line, delete all nodes in the original binary tree and their right child nodes.
Finishing, finishing the first two steps to get the tree, so that the structure of a clear hierarchy.
Five, tree traversal implementation
1 /// <summary>2 ///First Order traversal (DLR)3 /// </summary>4 /// <! [cdata[first accesses the node, then traverses the left subtree, and finally the right subtree]]>5 Static voidPreorder (node<Char>root)6 {7 if(Root = =NULL)8 {9 return;Ten } One A Print (root); - preorder (root. Lchild); - preorder (root. Rchild); the } - - /// <summary> - ///Middle Sequence Traversal (LDR) + /// </summary> - /// <! [cdata[first traverses the left subtree, then the root node, and finally traverses the right subtree]]> + Static voidInorder (node<Char>root) A { at if(Root = =NULL) - { - return; - } - - inorder (Root. Lchild); in Print (root); - inorder (Root. Rchild); to } + - /// <summary> the ///Post -post traversal (LRD) * /// </summary> $ /// <! [cdata[traverses the left subtree first, then traverses the right subtree and finally traverses the root node]]>Panax Notoginseng Static voidPostorder (node<Char>root) - { the if(Root = =NULL) + { A return; the } + - Postorder (Root. Lchild); $ Postorder (Root. Rchild); $ Print (root); - } - the /// <summary> - ///sequence TraversalWuyi /// </summary> the /// <! [cdata[from top to bottom from left to right]]> - Static voidLevelorder (node<Char>root) Wu { - if(Root = =NULL) About { $ return; - } -cseqqueue<node<Char>> sq =Newcseqqueue<node<Char>> ( -); - Sq. In (root); A while(!Sq. IsEmpty ()) + { thenode<Char> tmp =Sq. Out (); - Print (TMP); $ the if(TMP. Lchild! =NULL) the { the Sq. In (TMP. Lchild); the } - in if(TMP. Rchild! =NULL) the { the Sq. In (TMP. Rchild); About } the } the}
Data structure Collation (ii) tree