二叉树先序、中序,后序遍历的非递归实现

public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}

先序

根--左---右

使用栈,先装下根节点(头节点),出栈并输出,并且把出栈的节点的 右节点---左节点,依次入栈。循环

public static void preOrderUnRecur(Node head){
		Stack<Node> stack = new Stack<Node>();
		if(head!=null){
			System.out.print("pre-order: ");
			stack.add(head);
			while(!stack.isEmpty()){
				head = stack.pop();
				System.out.print(head.value+" ");
				if(head.right!=null){
					stack.add(head.right);
				}
				if(head.left!=null){
					stack.add(head.left);
				}
			}
			System.out.println();
		}
	}

中序

--根---右

依旧使用栈

中序遍历是先打印左节点

                    5

                2

             1

          3

这是一颗只有左节点的二叉树

先序应该先左节点,再中节点,再右节点。只有有一个节点有左节点,那么依次装入栈中出栈时去看看,右边节点有没有了,

有的话依旧,装完他的左节点,然后,出栈(加打印)

public static void inOrderUnRecur(Node head){
		if(head!=null){
			System.out.print("in-order: ");
			Stack<Node> stack = new Stack<Node>();
			while(!stack.isEmpty()||head!=null){
				
				while(head!=null){
					stack.add(head);
					head = head.left;
				}
				
				head= stack.pop();
				System.out.print(head.value+" ");
				head = head.right;
				
			}
			System.out.println();
		}
	}

后序

--右--根

后序遍历有好几种做法,这里介绍最简单的一种

刚刚介绍了前序遍历,他的原理是直接输入跟节点,然后装入栈中 右节点,再装入左节点,再输入,根据栈的先进后出

那么应该左节点先出来,遍历完左节点再去遍历右节点。

我们的后序遍历。使用原来的前序遍历,但是并不输出节点,而是装入B栈中。

按原来的模式,

根节点装入B栈。(这里不输出了而是装入另一个栈中)

但是这里应该先左节点进A栈,

右节点再装入A栈,


这样一来装二叉树的B栈装入顺序应该是

根--右---左(看代码理解,我讲述的可能不是太清楚)

那么直接让他们出栈,

即是左---右---根

public static void inOrderUnRecur(Node head){
		if(head!=null){
			System.out.print("in-order: ");
			Stack<Node> stack = new Stack<Node>();
			while(!stack.isEmpty()||head!=null){
				
				while(head!=null){
					stack.add(head);
					head = head.left;
				}
				
				head= stack.pop();
				System.out.print(head.value+" ");
				head = head.right;
				
			}
			System.out.println();
		}
	}


全部代码,,,附加一颗这样的二叉树

                                5

                      3                    8

                  2    4                7    10

               1                        6


没有连线,单应该能看懂。

package basic_class_04;
import java.util.*;


public class Main {

	public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}
	public static void preOrderUnRecur(Node head){
		Stack<Node> stack = new Stack<Node>();
		if(head!=null){
			System.out.print("pre-order: ");
			stack.add(head);
			while(!stack.isEmpty()){
				head = stack.pop();
				System.out.print(head.value+" ");
				if(head.right!=null){
					stack.add(head.right);
				}
				if(head.left!=null){
					stack.add(head.left);
				}
			}
			System.out.println();
		}
	}
	public static void inOrderUnRecur(Node head){
		if(head!=null){
			System.out.print("in-order: ");
			Stack<Node> stack = new Stack<Node>();
			while(!stack.isEmpty()||head!=null){
				
				while(head!=null){
					stack.add(head);
					head = head.left;
				}
				
				head= stack.pop();
				System.out.print(head.value+" ");
				head = head.right;
				
			}
			System.out.println();
		}
	}
	public static void posOrderUnRecur(Node head){
		Stack<Node> stack = new Stack<Node>();
		Stack<Node> stack2 = new Stack<Node>();
		if(head!=null){
			System.out.print("pre-order: ");
			stack.add(head);
			while(!stack.isEmpty()){
				head = stack.pop();
				stack2.add(head);
				if(head.left!=null){
					stack.add(head.left);
				}
				if(head.right!=null){
					stack.add(head.right);
				}
			}
			while(!stack2.isEmpty()){
				System.out.print(stack2.pop().value+" ");
			}
			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);
		
		//----------先序------------//
		preOrderUnRecur(head);
		//----------中序------------//
		inOrderUnRecur(head);
		//----------后序------------//
		posOrderUnRecur(head);
	}
	
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SUNbrightness

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值