package com.exuan.btreetraverse;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class BTreeTraverse {
public static void main(String[] args)
{
}
/*-------------------------------------------------------------
* traverse the binary tree without using the recursion method
* start
*/
/* -----------------------------------------------------
* start 'depth-first' traverse
* we need to use a stack in the 'depth-first' traverse
*/
//pre-order traverse
private static void preOrderTraverse(TreeNode root)
{
Stack s = new Stack();
TreeNode node = root;
while(node != null || !s.empty())
{
while(node != null)
{
visit(node);//visit the root
s.push(node);
node = node.left;//visit the left child
}
if(!s.empty())
{
node = s.pop();
node = node.right;//visit the right child
}
}
}
//in-order traverse
private static void inOrderTraverse(TreeNode root)
{
Stack s = new Stack();
TreeNode node = root;
while(node != null || !s.empty())
{
while(node != null)
{
s.push(node);
node = node.left;//visit the left child
}
if(!s.empty())
{
node = s.pop();
visit(node);//visit the root
node = node.right;//visit the right child
}
}
}
//post-order traverse
private static void postOrderTraverse(TreeNode root)
{
Stack s = new Stack();
TreeNode node = root;
/*we need a temporary variable to store the previous visited node,
* if we have visit the left child and the right child
* then the pre must be the right child, new we can visit the root
* */
TreeNode pre = null;
while(node != null || !s.empty())
{
if(node != null)
{
s.push(node);
node = node.left;//visit the left child
}
else
{
node = s.peek();
if(node.right != null && pre != node.right)
{
node = node.right;//visit the right child
}
else
{
/*the node doesn't have a right child or
* the right child has been visited(means the pre == node.right),
* now we can visit it
*/
node = s.peek();
pre = s.peek();
visit(node);//visit the root
s.pop();
node = null;
}
}
}
}
/* end 'depth-first' traverse
* -----------------------------------------------------
*/
/* -----------------------------------------------------
* start 'breadth-first' traverse
* 'breadth-first' traverse is 'level order' traverse
* we need to use a queue in the 'depth-first' traverse
*/
private static void BFTraverse(TreeNode root)
{
Queue q = new LinkedList();
TreeNode node = root;
if(node != null)
{
q.add(node);
while(!q.isEmpty())
{
node = q.peek();
visit(node);
if(node.left != null)
{
q.add(node.left);
}
if(node.right != null)
{
q.add(node.right);
}
q.remove();
}
}
}
/* end 'breadth-first' traverse
* -----------------------------------------------------
*/
/*end
* traverse the binary tree without using the recursion method
* -------------------------------------------------------------
*/
/*-------------------------------------------------------------
* traverse the binary tree using the recursion method
* start
*/
//pre-order traverse
private static void preOrderTraverseR(TreeNode root)
{
Stack s = new Stack();
TreeNode node = root;
visit(node);
preOrderTraverseR(node.left);
preOrderTraverseR(node.right);
}
//in-order traverse
private static void inOrderTraverseR(TreeNode root)
{
Stack s = new Stack();
TreeNode node = root;
preOrderTraverseR(node.left);
visit(node);
preOrderTraverseR(node.right);
}
//post-order traverse
private static void postOrderTraverseR(TreeNode root)
{
Stack s = new Stack();
TreeNode node = root;
preOrderTraverseR(node.left);
preOrderTraverseR(node.right);
visit(node);
}
/*end
* traverse the binary tree using the recursion method
* -------------------------------------------------------------
*/
private static void visit(TreeNode node)
{
System.out.print(node.data + ",");
}
}
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
}