The basic concept of the tree
- Definition tree
tree is n> = 0 nodes finite set T;- And only a particular node becomes the root of the tree
- n> 1, is divided into disjoint subsets, each subset is itself a tree (not intersecting subsets called a tree);
- The basic term of the tree
- Node: A data element contains, and links to other branches;
- Of node: contains subtree tree;
- Of the tree: the tree nodes of the maximum value point;
- Leaf node: = 0 degrees;
- Non-leaf node: is not equal to 0 degrees;
- Child nodes: sibling node, by definition;
- Path level: Start from the root node, all nodes reaches experienced p; p is called a node-level route;
- The depth of the tree: the maximum number of nodes in the tree hierarchy point;
Binary Tree
- Definition: less than or equal to 2;
- Left and right sub-tree;
- Full binary tree: each is full, as
- Complete binary tree, top to bottom, from left to right, as the order number and said the same; to understand: the full binary contrast, only about a little to the right, small left, right, and not part of a complete binary tree;
Binary tree properties:
- I-th layer up to 2 ^ (i-1) th node
- K number of the total depth of up to 2 ^ k -1 nodes;
- Important properties: degree of the node n1 1 is referred to, referred to as a 0 degree n0, referred to as degree of 2 N2;
N0 = N2 +. 1;
Proof: summary of points N = n0 + n1 + n2;
the number of edges (two number of connected nodes). 1-B = N;
B = 2 * n1 + N2; (starting from each of the edges are n1 and N2);
Third simplified formula to give n0 = n2 + 1;
Binary storage structure
Sequence Structure
Sequence structure is a waste of space, understood as an array to store the binary tree (imagine a full binary tree, from top to bottom, from left to right, followed by an index node), the node does not exist referred to as null;
FIG., A digital to array index (java array need -1); if H node does not exist, the array index 8 referred to as null;
Chain structure
Each node configuration procedure:
Binary tree traversal
Traversal methods: first order, in sequence, after, traverse the level;
Sakijo
Root To start, visit the left subtree, the right subtree - "recursion;
//伪代码如下:
public void traversal(Tree tree){
if(tree==null) return;
system.out.println(tree.data);
traversal(tree.leftNode);
traversal(tree.rightNode);
}
In order
First the left subtree, in which: root, right subtree
//伪代码如下:
public void traversal(Tree tree){
if(tree==null) return;
traversal(tree.leftNode);
system.out.println(tree.data);
traversal(tree.rightNode);
}
Understand the results:
After the order
//伪代码如下:
public void traversal(Tree tree){
if(tree==null) return;
traversal(tree.leftNode);
traversal(tree.rightNode);
system.out.println(tree.data);
}
Execution results appreciated:
this three kinds of traversal, sequential access are the same, the only difference is that when a print result (access time - when reading data): access path below
each node has 3 opportunity to visit
Preorder non-recursive algorithm
The core idea **: ** Stack storage node means, when thrown in sequence;
step
- Encountered node, push; Traverse the left tree;
- The left end of the tree, popped and access;
- Right tree traversal, push popped access;
//伪代码如下
public void traversal(Tree tree){
Stack s=Stack.creatStack(size);//得到栈
while(tree!=null || !s.isEmpty()){
while(tree!=null){
s.push(tree);
tree=tree.left;
}
if(!s.isEmpty()){
print(tree.data);
tree=s.pop();
tree = tree.right;
}
}
}
In sequence, after a position that is accessible to change;
Layer preorder
After the core idea, as the first order of the non-recursive traversal, using the stack to save, how to do it layer by layer, ie visit the left subtree, saved his brother sequence, while the output of the left subtree, save the left and right subtrees where, to save it? When comparing the queue,
as shown below:
---- save B, C queues B, C when accessing the A - B dequeued
access storage B ---- D, F queue C, D, F ---- the column C
so steps can be completed hierarchical access
//伪代码如下
public void traversal(Tree tree){
if(tree!=null){
Queue que = Queue.getQueue(size);
que.add(tree);
while(!que.isEmpty()){
Tree treeTemp = que.pop();
System.out.println(treeTemp.data);
que.add(tree.left);
que.add(tree.right);
}
}
}