Algorithms and data structures (5) tree 1

The basic concept of the tree

  1. Definition tree
    tree is n> = 0 nodes finite set T;
    1. And only a particular node becomes the root of the tree
    2. n> 1, is divided into disjoint subsets, each subset is itself a tree (not intersecting subsets called a tree);
  2. The basic term of the tree
    1. Node: A data element contains, and links to other branches;
    2. Of node: contains subtree tree;
    3. Of the tree: the tree nodes of the maximum value point;
    4. Leaf node: = 0 degrees;
    5. Non-leaf node: is not equal to 0 degrees;
    6. Child nodes: sibling node, by definition;
    7. Path level: Start from the root node, all nodes reaches experienced p; p is called a node-level route;
    8. The depth of the tree: the maximum number of nodes in the tree hierarchy point;

Binary Tree

  1. Definition: less than or equal to 2;
  2. Left and right sub-tree;
  3. Full binary tree: each is full, asFull Binary Tree
  4. 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:

  1. I-th layer up to 2 ^ (i-1) th node
  2. K number of the total depth of up to 2 ^ k -1 nodes;
  3. 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; Here Insert Picture Description
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

Here Insert Picture Description
Each node configuration procedure:
Here Insert Picture Description

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:
Here Insert Picture Description

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:
Here Insert Picture Description
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 Here Insert Picture Description
each node has 3 opportunity to visit

Preorder non-recursive algorithm

The core idea **: ** Stack storage node means, when thrown in sequence;
step

  1. Encountered node, push; Traverse the left tree;
  2. The left end of the tree, popped and access;
  3. 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, Here Insert Picture Description
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);
		}
	}
}
Published 17 original articles · won praise 0 · Views 368

Guess you like

Origin blog.csdn.net/qq_32193775/article/details/104031481