二叉树相关问题3-二叉树的遍历(非递归)

    /**
	 * 层次遍历  按行打印
	 * @param root
	 */
	public static  void printTree(Node root) {
		if (root==null) {
			return;
		}
		Queue<Node> queue = new LinkedList<>();
		queue.add(root);
		int nodeNum=1;//当前层的数量
		int nextNum=0;//下一层的数量
		while (!queue.isEmpty()) {
			Node t = queue.peek();
			System.out.print(queue.poll().value + " ");
			nodeNum--;
			if (t.left != null) {
				queue.add(t.left);
				nextNum++;
			}
			if (t.right != null) {
				queue.add(t.right);
				nextNum++;
			}
			// 如果当前输出结点是最右结点,那么换行
			if (nodeNum == 0) {
				System.out.println();
				nodeNum= nextNum;
				nextNum=0;
			}
		}
	}
	/**
	 * 层次遍历  按行打印
	 * @param root
	 */
	public static  void printTree1(Node root) {
		if (root==null) {
			return;
		}
		Node last= root;
		Node nlast= root;

		Queue<Node> queue = new LinkedList<>();
		queue.add(root);
		while (!queue.isEmpty()) {
			Node t = queue.peek();
			System.out.print(queue.poll().value + " ");
			if (t.left != null) {
				queue.add(t.left);
				nlast = t.left;
			}
			if (t.right != null) {
				queue.add(t.right);
				nlast = t.right;
			}
			// 如果当前输出结点是最右结点,那么换行
			if (last == t) {
				System.out.println();
				last = nlast;
			}
		} 
	}
	/**
	 * 先序遍历
	 * @param node
	 */
	public static void printPre(Node node) {
		if (node==null) {
			return;
		}
		Stack<Node> stack=new Stack<>();
		while (node!=null||!stack.isEmpty()) {
			if (node!=null) {
				stack.add(node);
				System.out.print(node.value+" ");
				node=node.left;
			}else {
				node=stack.pop().right;
			}
		}
	}
	/**
	 * 先序遍历
	 * @param node
	 */
	public static void printPre1(Node node) {
		if (node==null) {
			return;
		}
		Stack<Node> stack=new Stack<>();
		stack.push(node);
		while (!stack.isEmpty()) {
			node=stack.pop();
			System.out.print(node.value+" ");
			if (node.right!=null) {
				stack.push(node.right);
			} 
			if (node.left!=null) {
				stack.push(node.left);
			} 
		}
	}

	/**
	 * 中序遍历
	 * @param node
	 */
	public static void printMid1(Node node) {
		if (node==null) {
			return;
		}
		Node n=node;
		Stack<Node> stack=new Stack<>();
		stack.add(n);
		while (stack.size()!=0) {
			if (n.left!=null) {
				stack.push(n.left);
				n=n.left;
			}else {
				n = stack.pop();
				System.out.print(n.value+"  ");
				if (n.right!=null) {
					stack.push(n.right);
					n=n.right;
				}
			}
		}
	}
	/**
	 * 中序遍历
	 * @param node
	 */
	public static void printMid(Node node) {
		if (node==null) {
			return;
		}
		Stack<Node> stack=new Stack<>();
		while (node!=null||stack.size()!=0) {
			if (node!=null) {
				stack.push(node);
				node=node.left;
			}else {
				Node pop = stack.pop();
				System.out.print(pop.value+"  ");
				node=pop.right;
			}
		}
	}

	/**
	 * 后续遍历                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
	 * @param node
	 */
	public static void printPost(Node node) {
		if (node==null) {
			return;
		}
		Stack<Node> stack=new Stack<>();
		stack.push(node);
		Node tmp=null;
		while (stack.size()!=0) {
			tmp=stack.peek();
			if (tmp.left!=null&&node!=tmp.left&&node!=tmp.right) {
				stack.push(tmp.left);
			}else if (tmp.right!=null&&node!=tmp.right) {
				stack.push(tmp.right);
			}else {
				System.out.print(stack.pop().value+"  ");
				node=tmp;
			}
		}

	}
	public static void printPost1(Node node) {
		if (node==null) {
			return;
		}
		Stack<Node> stack1=new Stack<>();
		Stack<Node> stack2=new Stack<>();
		stack1.push(node);
		while (!stack1.isEmpty()) {
			node=stack1.pop();
			stack2.push(node);
			if (node.left!=null) {
				stack1.push(node.left);

			}
			if (node.right!=null) {
				stack1.push(node.right);

			}
		}
		while (!stack2.isEmpty()) {
			System.out.print(stack2.pop().value+"  ");
		}
	}

 

转载于:https://my.oschina.net/woniuyi/blog/3030973

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值