Two the definition of a fork tree : A finite set of n (n>=0) nodes, either an empty set (called an empty binary tree) or a two-fork tree of a root node and two Saozi right subtree, respectively called the root node, which are disjoint from each other.
two fork Tree features :
- 0<= degree <=2;
- The left and right sub-trees are sequential and cannot be reversed;
- No matter how many subtrees trees you have, you have to distinguish between the Zuozi and the right sub-tree.
Five basic types of binary trees:
- Empty two-fork tree;
- There is only one root node;
- The root node is only Zuozi;
- The root node is only the right subtree;
- The root node has both Zuozi and right sub-trees.
For example, 3 nodes of the two-fork tree form are:
Here are some special two-fork trees.
Oblique tree: All nodes are only Zuozi two-tree is called Left oblique tree, all nodes are only the right subtree of the two-tree called the right oblique tree.
full two fork tree: All branch nodes have Saozi right subtree, and all leaf nodes are on the same layer, so the two-fork tree is called full two-tree.
Full two-fork tree features:
- Leaf nodes can only be present in the bottom layer;
- The degree of non-leaf junction must be 2;
- In the same depth of the two-fork tree, the number of nodes with a full two-tree is the largest, and the leaf tree is the most.
Complete binary tree:
A two-prong tree with n nodes is numbered by the sequence, and if the node numbered I (1<=i<=n) is the same as the node numbered I in the full two-tree with the same depth, the binary tree is called a complete binary tree.
The relationship between a complete binary tree and a full two-fork tree: A full-two-fork tree is a special case of a complete binary tree.
Features of the complete binary tree:
- Leaf nodes can only be present in the bottom two layers;
- The lowest leaf must be concentrated in the left continuous position;
- The second-to-last level, if there are leaf nodes, must be in the right side of the continuous position;
- If the degree of the node is 1, then the node is only left dial hand tree, that is, there is no only right subtree;
- A tree of two trees of the same node. The depth of the complete binary tree is minimal (note here, probably many friends will say why not the depth of the two fork tree is the smallest, in fact, it is not the number of points of the two-fork tree can constitute a full two fork tree, so called the full binary tree depth of the smallest, and full of two fork tree is actually a complete binary tree)
The nature of the binary tree:
Property One: There is a maximum of "2 i-1" nodes on the first layer of the binary tree.
Property Two: Two-fork tree with a depth of K has a maximum of "2 K-th-1" nodes.
Property Three: To any one binary tree T, if its terminal node tree is n0, the degree of 2 node is N2, then n0=n2+1. (Explanation: Number of branches = total number of nodes with -1= degree of 1 nodes + degrees of 2 nodes; The total number of nodes = 0 of the leaf node + degree of 1 of the Node + degree 2 of the node, according to the above two equations, you can get the "degree of 0 leaf node = degree 2 of the node number +1" conclusion. )
Property Four: The depth of a complete binary tree with n nodes is "no greater than the maximum integer +1" of log with 2 (n), which is not well understood, as in the words:
Explanations of this nature are as follows:
Nature Five: (here is longer, and the formula is not good description, direct mapping bar)
Two the storage structure of the fork tree:
two binary tree sequential storage structure: one-dimensional arrays
is to use a one-dimensional array to store the nodes in the binary tree, and the location of the nodes and the subscript corresponding to the array; Note that the position of the node is set by the method of full binary tree numbering, so that if there is an empty node, the place is replaced with ^. However, this approach can be extreme, such as:
two-tree chain storage structure: Two-linked list
The binary tree has a maximum of two subtrees per node, so the nodes of the linked list can be designed as a data field and two pointer fields. As follows:
Data is a field, and Lchild and Rchild are pointer fields, each holding a pointer to the left and right sub-tree. Examples are as follows:
Improved version: Add a pointer field to point to the parent, called the triple-linked list.
two traversal of the fork tree:
Principle: Refers to starting from the root node, in order to access the binary tree in a sequence of all nodes, so that each node has and is only accessed once.
Binary Tree Traversal method: (used here is the idea of recursion)
Pre-order traversal:
If the binary tree is empty, return directly; otherwise, the root node is accessed, then the left subtree is traversed, and the last access is traversed to the right sub-tree. Example:
Middle sequence Traversal:
If the binary tree is empty, return directly; otherwise, from the root node, first access to traverse the left subtree, then access the root node, and finally access to traverse the right subtree. Example:
Post-post traversal:
If the binary tree is empty, return directly; otherwise, from the root node, first access to traverse the left subtree, and then access to traverse the right subtree, and finally access the root node. Example:
sequence Traversal:
If the tree is empty, return directly, otherwise from the root node of the first layer of the tree to access, from top to bottom layer by step, in the same layer, from left to right in order to access nodes. Example:
Traversal algorithm:
The following is the "Big talk data structure" inside the two-fork tree of the pre-sequence traversal code, in fact, the sequence traversal and post-order traversal is similar to just the location of the change;
Explanation of the hierarchical traversal:
The definition of hierarchical traversal can infer that when a layer of nodes is accessed, the left child and the right child of each node are sequentially accessed by their access order, so that the first encountered nodes are accessed first, which is in agreement with the operation principle of the queue. Therefore, in a hierarchical traversal, you can set up a queue structure, traverse from the root node of the two fork tree start, first put the root node pointer into the queue, and then remove an element from the head, each take an element, perform the following two operations:
(1) Access to the node referred to by the element;
(2) If the left and right child node of the node of the element is not empty, the left child pointer and the right child pointer of the node pointed to by the element are queued.
The above analysis can clear the general realization of the hierarchical traversal, the specific will not go on.
push to traverse:
Note Analysis Test instructions, it is best to draw an analysis diagram.
Push to two properties of traversal:
- A binary tree can be uniquely identified by the pre-sequence traversal sequence and the middle sequence traversal sequence known.
- A binary tree can be uniquely identified by a sequence of sequential traversal sequences and middle sequence traversal sequences.
- Note that the pre-sequence traversal sequence and the post-order traversal sequence are known to be unable to determine a binary tree.
two the establishment of a fork tree:
The null pointer of each node in the two-fork tree is drawn to a virtual node, which is set to a value of "#", and the processed two-tree is called an extended binary tree of the original binary tree.
It is concluded that the extended binary tree can be used to build a two-fork tree according to any method of pre-order traversal. (The idea of recursion is also used here.)
Clue two fork tree:
Using a binary tree of the existing two-fork linked list storage structure of the null pointer field to indicate the precursor and successor, used to point to the direct precursor node and the direct successor of the pointer is called the thread (thread), the clue of the two-tree is called the Clue two fork tree.
explain the structure of the Clue two fork tree:
A two-fork tree with n nodes if the binary linked list is used to store the structure, the N+1 pointer field must be NULL.
If the left Child pointer Field (lchild) of a node is empty, it is used to indicate the storage address of the direct precursor node of the node in some kind of traversal sequence, and if the right child pointer (rchild) of a node is empty, it is used to indicate the storage address of the direct successor node of the node in some kind of traversal sequence. A non-null pointer field, Still holds a pointer to the left and right child of the node. In this way, you get a clue two fork tree.
This is illustrated below:
Here's a lesson for the teacher to give an example:
From the above analysis:
If the use of the two-fork tree needs to traverse or find nodes often need some kind of traversal sequence in the precursor and subsequent relationships, then the use of the clue two-linked list of the storage structure is a very good choice.
traversal of the tree:
From the root node, all nodes in the tree are accessed in a sequential order, so that each node is accessed once and accessed only once.
A tree usually has a pre-order (root) traversal, a post order (root) traversal, and a sequence (time) traversal of three ways.
(1) Pre-order (root) traversal
If the tree is empty, the empty operation returns;
A. Access to the root node;
B. Each subtrees tree that traverses the root node in sequential traversal mode from left to right.
(2) post-sequential (root) traversal
If the tree is empty, the empty operation returns;
A. Each subtrees tree traversing the root node in sequential traversal mode from left to right;
B. Access the root node.
(3) sequence (time) traversal
Starting from the first layer of the tree (that is, the root node), the top-down step-by-step traversal, in the same layer, from left to right in order to access nodes.
the tree is converted to a two-fork tree:
⑴: Add a line between all neighboring brothers in the tree.
⑵: To each node in the tree, keep it only with the first
A connection between a child's knot, and delete it from other children.
Connections between sub-nodes.
⑶ level adjustment: With the root node as the axis, the tree clockwise
Turn 45 degrees to make it clear.
Examples are as follows:
Forest converted to two-fork tree:
⑴ each tree in the forest into a binary tree according to the above method, at this time the two-fork tree has no right subtree;
⑵ from the second binary tree, in turn the root node of the second fork tree as the former two fork root node of the right child to deal with, when all the two-tree in accordance with the law to get together after the tree is a binary tree is converted from the forest two tree.
There are two ways to traverse a forest:
(1) Pre-sequence traversal: If the forest is not empty, access to the root node of the first tree in the forest; the Serring of the first tree in the forest; the pre-order traverses the forest (except the first tree) of the remaining trees. Sequential traversal of each tree in the forest from left to right in turn.
(2) Middle sequence traversal: If the forest is not empty, then the middle sequence traverses the Serring of the first tree in the forest; accesses the root node of the first tree in the forest; the middle sequence traverses the forest (except the first tree) of the remaining trees. Sequentially traversing each tree in the forest from left to right.
two fork tree converted to tree or forest
The criteria for determining whether a binary tree can be converted to a tree or a forest: two the root of a fork tree contains a right subtree, a forest, or a tree.
⑴: If a node x is the left child of its parents Y, then the right child of node X, right child 、......, are connected with node Y with the line;
⑵: Delete all the parent nodes in the original binary tree and the connection of the right child node;
⑶ level adjustment: Collation by the ⑴, ⑵ two steps to get the tree or forest, the root node as the axis, the tree counterclockwise rotation 45 degrees, so that the level is clear.
Binary tree conversion to tree example:
Binary tree conversion to forest example:
Copyright NOTICE: This article for Bo Master original article, if you need to reprint please specify the source and attached link, thank you.
Java data structure-two fork tree and its traversal