用递归和非递归方式实现二叉树先序、中序和后序遍历
2017-11-10 21:18
721 查看
import java.util.Stack;
//分别用递归和非递归方式实现二叉树先序、中序和后序遍历
public class TreeTravel{
//二叉树节点的定义
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int data)
{
this.value=data;
}
}
//----------一 递归的二叉树前序、中序、后序遍历
public static void RecurPreOrder(Node head)
{
if(head==null)
{
return ;
}
System.out.print(head.value+" ");
RecurPreOrder(head.left);
RecurPreOrder(head.right);
}
public static void RecurInOrder(Node head)
{
if(head==null)
{
return ;
}
RecurInOrder(head.left);
System.out.print(head.value+" ");
RecurInOrder(head.right);
}
public static void RecurposOrder(Node head)
{
if(head==null)
{
return ;
}
RecurposOrder(head.left);
RecurposOrder(head.right);
System.out.print(head.value+" ");
}
//-----------------------------------------------
//----------二 非递归的二叉树前序、中序、后序遍历(用栈来保存信息)
public static void NoRecurPreOrder(Node head)
{
if(head==null)
{
return ;
}
Stack<Node>stack=new Stack<Node>();
stack.push(head);
while(!stack.isEmpty())
{
head=stack.pop();
System.out.print(head.value+" ");
if(head.right!=null)
{
stack.push(head.right);
}
if(head.left!=null)
{
stack.push(head.left);
}
}
System.out.println();
}
//中序
public static void NoRecurInOrder(Node head)
{
if(head==null)
{
return ;
}
Stack<Node>stack=new Stack<Node>();
Node cur=head;
//左子树的操作
while(cur.left!=null)
{
stack.push(cur.left);
cur=cur.left;
}
while(!stack.isEmpty())
{
cur=stack.pop();
System.out.print(cur.value+" ");
if(cur.right!=null)
{
System.out.print(cur.right.value+" ");
}
}
System.out.print(head.value+" "); //打印根节点
while(head.right!=null)
{
stack.push(head.right); //压入右子树的节点
head=head.right;
}
while(!stack.isEmpty())
{
if(stack.size()>1)
{
stack.pop(); //弹出最后一个节点
}
cur=stack.pop();
if(cur.left!=null)
{
System.out.print(cur.left.value+" ");
}
System.out.print(cur.value+" ");
if(cur.right!=null)
{
System.out.print(cur.right.value+" ");
}
}
System.out.println();
}
//后序
public static void NoRecurposOrder(Node head)
{
if(head==null)
{
return ;
}
Stack<Node> stack=new Stack<Node>();
//左子树
Node cur=head;
while(cur.left!=null)
{
stack.push(cur.left);
if(cur.left.right!=null)
{
stack.push(cur.left.right);
}
cur=cur.left;
}
while(!stack.isEmpty())
{
cur=stack.pop();
System.out.print(cur.value+" ");
}
Node p=head;
//右子树
while(p.right!=null)
{
stack.push(p.right);
p=p.right;
}
//打印右子树
while(!stack.isEmpty())
{
if(stack.size()>1)
{
stack.pop();
}
p=stack.pop();
if(p.left!=null)
{
System.out.print(p.left.value+" ");
}
if(p.right!=null)
{
System.out.print(p.right.value+" ");
}
System.out.print(p.value+" ");
}
System.out.print(head.value+" ");
}
//-----------------------------------------------
public static void main(String[]args)
{
Node node=new Node(1);
node.left=new Node(2);
node.right=new Node(3);
node.left.left=new Node(4);
node.left.right=new Node(5);
node.right.left=new Node(6);
node.right.right=new Node(7);
NoRecurPreOrder(node);
NoRecurInOrder(node);
NoRecurposOrder(node);
}
}
左神的代码:
import java.util.Stack;
public class Problem_01_PreInPosTraversal {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static void preOrderRecur(Node head) {
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
public static void inOrderRecur(Node head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.value + " ");
inOrderRecur(head.right);
}
public static void posOrderRecur(Node head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.value + " ");
}
public static void preOrderUnRecur(Node head) {
System.out.print("pre-order: ");
if (head != null) {
Stack<Node> stack = new Stack<Node>();
stack.add(head);
while (!stack.isEmpty()) {
head = stack.pop();
System.out.print(head.value + " ");
if (head.right != null) {
stack.push(head.right);
}
if (head.left != null) {
stack.push(head.left);
}
}
}
System.out.println();
}
public static void inOrderUnRecur(Node head) {
System.out.print("in-order: ");
if (head != null) {
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
System.out.println();
}
public static void posOrderUnRecur1(Node head) {
System.out.print("pos-order: ");
if (head != null) {
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.push(head);
while (!s1.isEmpty()) {
head = s1.pop();
s2.push(head);
if (head.left != null) {
s1.push(head.left);
}
if (head.right != null) {
s1.push(head.right);
}
}
while (!s2.isEmpty()) {
System.out.print(s2.pop().value + " ");
}
}
System.out.println();
}
public static void posOrderUnRecur2(Node h) {
System.out.print("pos-order: ");
if (h != null) {
Stack<Node> stack = new Stack<Node>();
stack.push(h);
Node c = null;
while (!stack.isEmpty()) {
c = stack.peek();
if (c.left != null && h != c.left && h != c.right) {
stack.push(c.left);
} else if (c.right != null && h != c.right) {
stack.push(c.right);
} else {
System.out.print(stack.pop().value + " ");
h = c;
}
}
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.left.left.left = new Node(1);
head.right.left = new Node(7);
head.right.left.left = new Node(6);
head.right.right = new Node(10);
head.right.right.left = new Node(9);
head.right.right.right = new Node(11);
// recursive
System.out.println("==============recursive==============");
System.out.print("pre-order: ");
preOrderRecur(head);
System.out.println();
System.out.print("in-order: ");
inOrderRecur(head);
System.out.println();
System.out.print("pos-order: ");
posOrderRecur(head);
System.out.println();
// unrecursive
System.out.println("============unrecursive=============");
preOrderUnRecur(head);
inOrderUnRecur(head);
posOrderUnRecur1(head);
posOrderUnRecur2(head);
}
}
相关文章推荐
- 实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式
- 探讨:C++实现链式二叉树(用非递归方式先序,中序,后序遍历二叉树)
- 分别用递归和非递归方式实现二叉树的先序、中序和后序遍历
- 从递归的异同分析二叉树先序、中序、后序遍历共同流程并实现非递归方式的三种遍历。
- 二叉树的遍历 前序 中序 后序 分别实现递归和非递归遍历方式
- 用递归方式实现二叉树先序、中序、后序遍历
- c++实现二叉树的非递归创建以及非递归先序、中序、后序遍历
- 数据结构(六)——二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- 二叉树构建,先序,中序,后序遍历(以及非递归实现),广度优先遍历
- 二叉树先序、中序、后序遍历的非递归实现,纯c代码
- ZT 二叉树先序,中序,后序遍历非递归实现
- 二叉树(前序,中序,后序,层序)遍历递归与循环的python实现
- 二叉树先序,中序,后序遍历非递归实现
- 二叉树的前序、中序、后序遍历非递归实现
- 二叉树(15)----由中序遍历和后序遍历重建二叉树,递归方式
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- 二叉树的先序、中序、后序遍历的递归和非递归实现
- 二叉树 前序、中序、后序、层次遍历及非递归实现 查找、统计个数、比较、求深度的递归实现
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- Java实现二叉树的递归构建及先序、中序、后序、层序遍历(递归和循环)