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);
}
}