二叉树各种遍历方式实现

今天总结了一下二叉树的遍历的算法,结合网上的这种版本,实现了如下的二叉树的遍历的各种方法,尤其是后续遍历的几种非递归算法,我觉得很好,这里与大家分享。

先是结点类:

public class Node{
	private char key;
	private Node left;
	private Node right;
	
	//带三个参数的构造函数,构造非叶节点
	public Node(char key, Node left, Node right) {
		super();
		this.key = key;
		this.left = left;
		this.right = right;
	}
	//带一个参数的构造函数,构造叶节点,左右子树都为空
	public Node(char key) {
		super();
		this.key = key;
		this.left = null;
		this.right = null;
	}
	public char getKey() {
		return key;
	}
	public void setKey(char key) {
		this.key = key;
	}
	public Node getLeft() {
		return left;
	}
	public void setLeft(Node left) {
		this.left = left;
	}
	public Node getRight() {
		return right;
	}
	public void setRight(Node right) {
		this.right = right;
	}
}
具体实现如下:

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

public class BinaryTree {
	protected Node root;

	public BinaryTree(Node root) {
		this.root = root;
	}

	public Node getRoot() {
		return root;
	}

	/** 构造树 */
	public static Node init() {
		Node a = new Node('A');
		Node b = new Node('B', null, a);
		Node c = new Node('C');
		Node d = new Node('D', b, c);
		Node e = new Node('E');
		Node f = new Node('F', e, null);
		Node g = new Node('G', null, f);
		Node h = new Node('H', d, g);
		return h;// root
	}

	/** 访问节点 */
	public static void visit(Node p) {
		System.out.print(p.getKey() + " ");
	}

	/*
	 * 递归实现的三种算法都很简单,调节三个语句的顺序即可
	 */
	/** 递归实现前序遍历 */
	protected static void preorder(Node p) {
		if (p != null) {
			visit(p);
			preorder(p.getLeft());
			preorder(p.getRight());
		}
	}

	/** 递归实现中序遍历 */
	protected static void inorder(Node p) {
		if (p != null) {
			inorder(p.getLeft());
			visit(p);
			inorder(p.getRight());
		}
	}

	/** 递归实现后序遍历 */
	protected static void postorder(Node p) {
		if (p != null) {
			postorder(p.getLeft());
			postorder(p.getRight());
			visit(p);
		}
	}

	/** 非递归实现前序遍历 */
	protected static void iterativePreorder(Node p) {
		Stack<Node> stack = new Stack<Node>();
		if (p != null) {
			stack.push(p);
			while (!stack.empty()) {
				p = stack.pop();
				visit(p);
				if (p.getRight() != null)
					stack.push(p.getRight());
				if (p.getLeft() != null)
					stack.push(p.getLeft());
			}
		}
	}

	/** 
	 * 非递归实现前序遍历2 
	 * 这种实现可以实现非递归前序和中序的统一,修改visit的位置即可
	 */
	protected static void iterativePreorder2(Node p) {
		Stack<Node> stack = new Stack<Node>();
		Node node = p;
		while (node != null || stack.size() > 0) {
			while (node != null) {//压入所有的左节点,压入前访问它
				visit(node);
				stack.push(node);
				node = node.getLeft();
			}
			if (stack.size() > 0) {//
				node = stack.pop();
				node = node.getRight();
			}
		}
	}
	
	/** 
	 * 非递归实现后序遍历 1
	 * */
	protected static void iterativePostorder1(Node p) {
		Node q = p;
		Stack<Node> stack = new Stack<Node>();
		while (p != null) {
			// 左子树入栈
			while (p.getLeft() != null) {
				stack.push(p);
				p = p.getLeft();
			}
			// 当前节点无右子或右子已经输出
			while (p != null && (p.getRight() == null || p.getRight() == q)) {
				visit(p);
				q = p;// 记录上一个已输出节点
				if (stack.empty())
					return;
				p = stack.pop();
			}
			// 处理右子
			stack.push(p);
			p = p.getRight();
		}
	}

	/** 非递归实现后序遍历 双栈法 */
	protected static void iterativePostorder2(Node p) {
		Stack<Node> lstack = new Stack<Node>();
		Stack<Node> rstack = new Stack<Node>();
		Node node = p, right;
		do {
			while (node != null) {
				right = node.getRight();
				lstack.push(node);
				rstack.push(right);
				node = node.getLeft();
			}
			node = lstack.pop();
			right = rstack.pop();
			if (right == null) {
				visit(node);
			} else {
				lstack.push(node);
				rstack.push(null);
			}
			node = right;
		} while (lstack.size() > 0 || rstack.size() > 0);
	}

	/** 非递归实现后序遍历 单栈法*/
	//和方法一的实现思想一致
	protected static void iterativePostorder3(Node p) {
		Stack<Node> stack = new Stack<Node>();
		Node node = p, prev = p;
		while (node != null || stack.size() > 0) {
			while (node != null) {
				stack.push(node);
				node = node.getLeft();
			}
			if (stack.size() > 0) {
				Node temp = stack.peek().getRight();//peek()是取栈顶对象
				if (temp == null || temp == prev) {
					node = stack.pop();
					visit(node);
					prev = node;
					node = null;
				} else {
					node = temp;
				}
			}

		}
	}

	/** 非递归实现后序遍历4 双栈法*/
	//将后访问的先入栈,stack栈辅助将后序遍历按逆序入栈temp,然后temp中元素出栈并访问
	protected static void iterativePostorder4(Node p) {
		Stack<Node> stack = new Stack<Node>();
		Stack<Node> temp = new Stack<Node>();
		Node node = p;
		while (node != null || stack.size() > 0) {
			while (node != null) {
				temp.push(node);
				stack.push(node);
				node = node.getRight();
			}
			if (stack.size() > 0) {
				node = stack.pop();
				node = node.getLeft();
			}
		}
		while (temp.size() > 0) {
			node = temp.pop();
			visit(node);
		}
	}

	/** 非递归实现中序遍历 */
	protected static void iterativeInorder(Node p) {
		Stack<Node> stack = new Stack<Node>();
		while (p != null) {
			while (p != null) {
				if (p.getRight() != null)
					stack.push(p.getRight());// 当前节点右子入栈
				stack.push(p);// 当前节点入栈
				p = p.getLeft();
			}
			p = stack.pop();
			while (!stack.empty() && p.getRight() == null) {
				visit(p);
				p = stack.pop();
			}
			visit(p);
			if (!stack.empty())
				p = stack.pop();
			else
				p = null;
		}
	}

	/** 非递归实现中序遍历2 */
	protected static void iterativeInorder2(Node p) {
		Stack<Node> stack = new Stack<Node>();
		Node node = p;
		while (node != null || stack.size() > 0) {
			while (node != null) {
				stack.push(node);
				node = node.getLeft();
			}
			if (stack.size() > 0) {
				node = stack.pop();
				visit(node);
				node = node.getRight();
			}
		}
	}
	
	//*非递归实现层序遍历*/
	protected static void iterativeLevel(Node p){
		//LinkedBlockingDeque是FIFO队列的实现类
		Queue<Node> queue = new LinkedBlockingQueue<Node>();
		
		Node node = p;
		Node q = null;
		if( node != null ){
			queue.add(node);
			while(queue.size() > 0){
				q = queue.remove();
				visit(q);
				if(q.getLeft() != null){
					queue.add(q.getLeft());
				}
				if(q.getRight() != null){
					queue.add(q.getRight());
				}
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BinaryTree tree = new BinaryTree(init());
		System.out.print(" Pre-Order:");
		preorder(tree.getRoot());
		System.out.println();
		System.out.print("  In-Order:");
		inorder(tree.getRoot());
		System.out.println();
		System.out.print("Post-Order:");
		postorder(tree.getRoot());
		System.out.println();
		System.out.print(" Pre-Order:");
		iterativePreorder(tree.getRoot());
		System.out.println();
		System.out.print("Pre-Order2:");
		iterativePreorder2(tree.getRoot());
		System.out.println();
		System.out.print("  In-Order:");
		iterativeInorder(tree.getRoot());
		System.out.println();
		System.out.print(" In-Order2:");
		iterativeInorder2(tree.getRoot());
		System.out.println();
		System.out.print(" Post-Order1:");
		iterativePostorder1(tree.getRoot());
		System.out.println();
		System.out.print("Post-Order2:");
		iterativePostorder2(tree.getRoot());
		System.out.println();
		System.out.print("Post-Order3:");
		iterativePostorder3(tree.getRoot());
		System.out.println();
		System.out.print("Post-Order4:");
		iterativePostorder4(tree.getRoot());
		System.out.println();

		System.out.print("iterativeLevel:");
		iterativeLevel(tree.getRoot());
		System.out.println();
	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值