12 storage structure of a fork tree 1.1 sequential storage structure
The sequential storage structure of binary tree is to store the nodes in the binary tree with one-dimensional array, and the storage location of the nodes, and also the subscript of the array to embody the logical relationship between the nodes, such as the relationship between parents and children.
Storage of a complete binary tree:
Generic binary Tree storage: Although the sequence number does not reflect a logical relationship, it can be numbered by a full binary tree, except that the nonexistent node is set to "^".
Extreme case, right leaning tree, which is not recommended to use
1.2 Two fork List
The binary tree has a maximum of two children per node, so it is natural to design a data field and two pointer fields for it, which we call a linked list of two forks.
Where data is the field, Lchild and Rchild are pointer fields, each holding a pointer to the left child and right child.
2 Traversal binary Tree 2.1 Two fork tree traversal principle
The traversal of binary trees (traversing binary tree) refers to all nodes in a binary tree that are accessed from the root node in a sequential order, so that each node is accessed once and accessed only once.
2.2 Two fork Tree Traversal method 2.2.1 Pre-sequence traversal
The rule is that if the binary tree is empty, then an empty operation is returned, otherwise the root node is accessed first, then the left subtree is traversed before the sequence, and then the right subtree is traversed.
2.2.2 Middle Sequence traversal
The rule is that if the tree is empty, then an empty operation is returned, otherwise starting from the root node (note that the root nodes are not first accessed), the middle sequence traverses the left dial hand tree of the root, then the access root , and the final middle sequence traverses the right subtree .
2.2.3 Post-sequential traversal
The rule is that if the tree is empty, then an empty operation is returned, otherwise the left-to-right leaves the back node in a way that iterates through the subtree and then accesses the root node.
2.2.4 Sequence Traversal
The rule is that if the tree is empty, then an empty operation is returned, otherwise it is accessed from the first layer of the tree, which is the root node, and is traversed from top to bottom, and in the same layer, the nodes are accessed from left to right sequentially.
3, clue two fork Tree
Previous two-fork tree:
A pointer to a precursor and a successor is called a Clue, and a two-linked list of clues is called a clue list, and the corresponding two-fork tree is called the Clue Two fork tree (treaded binary trees).
Using the middle sequence traversal: HDIBEAFCG
The black line indicates the precursor, and the brown wire indicates the successor.
The process of traversing a two-fork tree in some order to turn it into a clue two fork tree is a clue.
Lchild |
Ltag |
Data |
Rtag |
Rchild |
which
Ltag is 0 o'clock the left child pointing to the node, which is the precursor of the 1 o'clock point;
Rtag is 0 o'clock to the right child pointing to the node, and for 1 o'clock points to the successor of the node.
4, tree, forest and two-fork tree conversion 1, the tree converted to two fork tree
1) Add line. A line between all the brothers ' knots.
2) go to the line. For each node in the tree, keep only its connection to the first child node, and remove the connection to the other child's nodes.
3) Level adjustment. Take the tree as the root node as the axis, the whole tree clockwise rotation of a certain angle, so that the structure of a clear hierarchy. Note that the first child is the left child of a two-fork tree, and the child that the brother converts is the right child of the knot.
2. Conversion of forest to two-fork tree
1) Convert each tree to a binary tree
2) The first binary tree does not move, starting from the second tree, followed by the root node of the second fork tree as the root node of the previous tree, the right child, with a line connection.
3. Binary Tree conversion to tree
Binary tree-to-tree is the inverse process of tree transforming binary tree.
4. Binary Tree conversion to forest
1) starting from the root node, if the right child in, then the right child node connection to delete, and then see the separation of the two fork tree, if the right child, then delete ... Until all the right kids are connected, get a two-fork tree.
2) then convert each tree into a tree after separating the two trees.
Big talk data structure (15)--Two fork Tree theory knowledge (2)