package tree;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Tree {
/*
* 创建树,前序
* */
public static Note createTree(Queue<Character> que) {
char s = que.poll();
if ( s == ',') return null;
Note t = new Note();
t.data = s;
t.left = createTree(que);
t.right = createTree(que);
return t;
}
/*
* 创建树,中序
* */
public static Note createTree2(Queue<Character> que) {
char c = que.poll();
if (c == ',') return null;
Note t = new Note();
t.left = createTree2(que);
t.data = c;
t.right = createTree2(que);
return t;
}
/*
* 创建树,后续
* */
public static Note createTree3(Queue<Character> que) {
char c = que.poll();
if (c == ',') return null;
Note t = new Note();
t.left = createTree3(que);
t.right = createTree3(que);
t.data = c;
return t;
}
/*
* 前序遍历,递归
* */
public static void before (Note t) {
if (t == null) return;
System.out.print(t.data);
before(t.left);
before(t.right);
}
/*
* 前序遍历,非递归,方法一
* */
public static void before2(Note t) {
Stack<Note> s = new Stack<>();
s.push(t);
System.out.print(t.data);
t = t.left;
while(!s.isEmpty() || t != null) {
if (t != null) {
s.push(t);
System.out.print(t.data);
t = t.left;
} else {
t = s.pop();
t = t.right;
}
}
}
/*
* 前序遍历,非递归,方法二
* */
public static void before3(Note t) {
Stack<Note> s = new Stack<>();
s.push(t);
System.out.print(t.data);
t = t.left;
while (!s.isEmpty()) {
while(t != null) {
s.push(t);
System.out.print(t.data);
t = t.left;
}
t = s.pop();
t = t.right;
}
}
/*
* 中序遍历,递归
* */
public static void center (Note t) {
if (t == null) return;
center(t.left);
System.out.print(t.data);
center(t.right);
}
/*
* 中序遍历,非递归,方法一
* */
public static void center2(Note t) {
Stack<Note> s = new Stack<>();
s.push(t);
t = t.left;
while (!s.isEmpty() || t != null) {
if (t != null) {
s.push(t);
t = t.left;
} else {
t = s.pop();
System.out.print(t.data);
t = t.right;
}
}
}
/*
* 中序遍历,非递归,方法二
* */
public static void center3(Note t) {
Stack<Note> s = new Stack<>();
s.push(t);
t = t.left;
while(!s.isEmpty()) {
while(t != null) {
s.push(t);
t = t.left;
}
t = s.pop();
System.out.print(t.data);
t = t.right;
}
}
/*
* 后续遍历,递归
* */
public static void after (Note t) {
if (t == null) return;
after(t.left);
after(t.right);
System.out.print(t.data);
}
/*
* 后续遍历,非递归,方法一
* */
public static void after2 (Note t) {
Stack<Note> s = new Stack<>();
s.push(t);
s.push(t);
t = t.left;
while (!s.isEmpty()) {
if (t != null) {
s.push(t);
s.push(t);
t = t.left;
} else {
t = s.pop();
if (!s.isEmpty() && t == s.peek()) { //&&是惰性求值,如果前边的就能确定结果将不会进行后面的表达式运行算(此处用到这个特性的原因是当栈为空时使用peek会报错)
// 第一次取得此节点要先遍历其右子树,不能输出
t = t.right;
} else { //第二次输出
System.out.print(t.data);
t = null;
}
}
}
}
/*
* 后序遍历,非递归,方法二
* */
public static void after3 (Note t) {
Stack<Note> s = new Stack<>();
s.push(t);
s.push(t);
t = t.left;
while (!s.isEmpty()) {
while (t != null) {
s.push(t);
s.push(t);
t = t.left;
}
t = s.pop();
if (!s.isEmpty() && t == s.peek()) {
t = t.right;
} else {
System.out.print(t.data);
t = null;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
Queue<Character> que = new LinkedList<>(); //因为Java中没有一次读取一个字符的机制所以我把整个字符串整个读进来然后一个字符一个字符的添加到队列,在使用的时候依次拿出
for (int i = 0;i < s.length(); i ++) {
que.add(s.charAt(i));
}
Note tree = createTree(que);
before(tree);
System.out.println();
before2(tree);
System.out.println();
before3(tree);
System.out.println();
center(tree);
System.out.println();
center2(tree);
System.out.println();
center3(tree);
System.out.println();
after(tree);
System.out.println();
after2(tree);
System.out.println();
after3(tree);
System.out.println();
}
static class Note {
public char data;
public Note left = null;
public Note right = null;
}
}
Create a binary tree and traverse --Java
Guess you like
Origin blog.csdn.net/qq_40561126/article/details/104367352
Ranking