二叉树遍历

        二叉树的递归与非递归算法。

        完整代码:

package cn.ccn.print;

import java.util.Stack;

// 节点的数据结构
class Node{
	public int value;
	public Node left;
	public Node right;
	public Node(int value) {
		super();
		this.value = value;
	}
}

public class PrintTree {
	// 递归前序遍历树
	public void preOrder(Node head){
		if(head == null){
			return;
		}
		System.out.print(head.value + "  ");
		preOrder(head.left);
		preOrder(head.right);
	}
	
	// 递归中序遍历
	public void inOrder(Node head){
		if(head == null){
			return;
		}
		inOrder(head.left);
		System.out.print(head.value + "  ");
		inOrder(head.right);
	}
	
	// 递归后续遍历
	public void posOrder(Node head){
		if(head == null){
			return;
		}
		posOrder(head.left);
		posOrder(head.right);
		System.out.print(head.value + "  ");
	}
	
	// 非递归方法
	// 非递归前序遍历
	public void preOrderUuRecur(Node head){
		if(head != null){
			Stack<Node> stack = new Stack<Node>();
			stack.push(head);
			while(!stack.isEmpty()){
				Node temp = stack.pop();
				System.out.print(temp.value + "  ");
				if(temp.right != null){
					stack.add(temp.right);
				}
				if(temp.left != null){
					stack.add(temp.left);
				}
			}
		}
	}
	
	// 非递归中序遍历
	public void inOrderUuRecur(Node head){
		if(head != null){
			Stack<Node> stack = new Stack<Node>();
			while(!stack.isEmpty() || head!=null){
				if(head != null){
					stack.add(head);
					head = head.left;
				}else{
					head = stack.pop();
					System.out.print(head.value + "  ");
					head = head.right;
				}
			}
		}
	}
	
	// 用两个栈实现非递归后序遍历
	public void posOrderUnRecur(Node head){
		if(head != null){
			Stack<Node> stack1 = new Stack<Node>();
			Stack<Node> stack2 = new Stack<Node>();
			stack1.add(head);
			while(!stack1.isEmpty()){
				head = stack1.pop();
				stack2.add(head);
				if(head.left != null){
					stack1.add(head.left);
				}
				if(head.right != null){
					stack1.add(head.right);
				}
			}
			while(!stack2.isEmpty()){
				System.out.print(stack2.pop().value + "  ");
			}
		}
	}
	
	// 用一个栈实现非递归后序遍历
	public void posOrderUnRecurOneStack(Node head){
		if(head != null){
			Stack<Node> stack = new Stack<Node>();
			stack.add(head);
			Node cur = null; // 用来表示当前栈顶元素
			while(!stack.isEmpty()){
				cur = stack.peek();
				if(cur.left!=null && head!=cur.left && head!=cur.right){ 
					stack.add(cur.left);
				}else if(cur.right!=null && cur.right!=head){
					stack.add(cur.right);
				}else{
					System.out.print(stack.pop().value + "  ");
					head = cur;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		PrintTree printTree = new PrintTree();
		Node head = new Node(1);
		Node n1 = new Node(2);
		Node n2 = new Node(3);
		Node n3 = new Node(4);
		Node n4 = new Node(5);
		Node n5 = new Node(6);
		Node n6 = new Node(7);
		head.left = n1;
		head.right = n2;
		n1.left = n3;
		n1.right = n4;
		n2.left = n5;
		n2.right = n6;
		System.out.println("前序遍历:");
		printTree.preOrder(head);
		System.out.println();
		System.out.println("中序遍历:");
		printTree.inOrder(head);
		System.out.println();
		System.out.println("后序遍历:");
		printTree.posOrder(head);
		System.out.println();
		System.out.println("=====非递归=========");
		System.out.println("前序遍历:");
		printTree.preOrderUuRecur(head);
		System.out.println();
		System.out.println("中序遍历:");
		printTree.inOrderUuRecur(head);
		System.out.println();
		System.out.println("后序遍历:");
		printTree.posOrderUnRecur(head);
		System.out.println();
		System.out.println("后序遍历:");
		printTree.posOrderUnRecurOneStack(head);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值