二叉树递归和非递归遍历 (五)

2014-11-24 10:46:21 · 作者: · 浏览: 19
-----


// 后序遍历(递归实现)-------------------------------------------------------------
/*
left right current
step1:初始化栈
按照step2初始化栈
step2:入栈
入栈顺序:current,right,left
如果current的中左右节点都入栈了,将current标识为true;
将入栈的左右节点标识初始化为false
step3:出栈
出栈节点设置为current,
如果current标识为ture,访问之;如果为false,做step2步骤
step4:重复step2&step3
*/

public void postOrder( Node root){
//special case
if(root==null)
return;

Node current=root;

//initial stack
Stack s=new Stack();
pushPostOrderNode(current,s);

//pop and visit
while( !s.empty() ){
current=s.pop();
if(current.bPushed)
visit(current);
else
pushPostOrderNode(current,s);
}//end while
}//end postOrder()

// 左右子树尚未入栈,则依次将 根节点,右节点,左节点 入栈
private void pushPostOrderNode(Node current, Stack s){
if(current==null)
return;
if(!s.empty()){
leftChild=current.leftChild;
rightChild=curret.rightChild;

s.push(current); //push current
if(rightChild!=null){ //push rightChild
s.push(rightChild);
rightChild.bPushed=false;
}//end if
if(leftChild!=null){ //push leftChild
s.push(leftChild);
rightChild.bPushed=false;
}//end if

//set flag, ture present current's left and right has both pushed
current.bPushed=true;
}//end if
}//end if

}//end pushPostOrderNode()3 深度优先搜索和层次遍历(广度优先搜索)

[java] view plaincopyprint // -------------------------------------------------------------
/*
二叉树的深度优先搜索即先序遍历,用栈实现(非递归实现)
*/

// -------------------------------------------------------------
/*
层次搜索,即广度优先搜索
step1:specail case
root==null;
step2:initial current
current=root;
step3:initial queue
step4:remove node in queue, and visit the current node
step5:add node in queue
step6:loop step4 and step5
base case: queue is empty
*/

public void gfs(Noode root){
//step1:specail case
if(root==null)
return ;

//step2:initial current
Node current=root;

//step3:initial queue
Queue q=new Queue();
q.add(current);

//step6:loop step4 and step5
//base case: queue is empty
while( !q.empty() ){
//step4:remove node in queue
current=q.remove();
visit(current);

Node leftChild=current.leftChild;
Node rightChild=curret.rightChild;

//step5:add node in queue
if(leftChild!=null)
q.add(leftChild);
if(rightChild!=null)
q.add(rightChild);
}//end while
}//gfs()

// -------------------------------------------------------------
/*
二叉树的深度优先搜索即先序遍历,用栈实现(非递归实现)
*/

// -------------------------------------------------------------
/*
层次搜索,即广度优先搜索
step1:specail case
root==null;
step2:initial current
current=root;
step3:initial queue
step4:remove node in queue, and visit the current node
step5:add node in queue
step6:loop step4 and step5
base case: queue is empty
*/

public void gfs(Noode root){
//step1:specail case
if(root==null)
return ;

//step2:initial current
Node current=root;

//step3:initial queue
Queue q=new Queue();
q.add(current);

//step6:loop step4 and step5
//base case: queue is empty
while( !q.empty() ){
//step4:remove node in queue
current=q.remove();
visit(current);

Node leftChild=current.leftChild;
Node rightChild=curret.rightChild;

//step5:add node in queue
if(leftChild!=null)
q.add(leftChild);
if(rightChild!=null)
q.add(rightChil