|
|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
package leo.class07;
|
|
|
|
|
package leo.class07_10;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Stack;
|
|
|
|
|
@ -15,10 +15,6 @@ import java.util.Stack;
|
|
|
|
|
*/
|
|
|
|
|
public class TraversalBT {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args){
|
|
|
|
|
TreeNode head = new TreeNode(1);
|
|
|
|
|
head.left = new TreeNode(2);
|
|
|
|
|
@ -28,18 +24,19 @@ public class TraversalBT {
|
|
|
|
|
head.right.left = new TreeNode(6);
|
|
|
|
|
head.right.right = new TreeNode(7);
|
|
|
|
|
|
|
|
|
|
Recursive.pre(head);
|
|
|
|
|
Recursive.pre1(head);
|
|
|
|
|
System.out.println();
|
|
|
|
|
UnRecursive.pre(head);
|
|
|
|
|
UnRecursive.pre1(head);
|
|
|
|
|
System.out.println("--------");
|
|
|
|
|
Recursive.in(head);
|
|
|
|
|
|
|
|
|
|
Recursive.in1(head);
|
|
|
|
|
System.out.println();
|
|
|
|
|
UnRecursive.in(head);
|
|
|
|
|
UnRecursive.in1(head);
|
|
|
|
|
System.out.println("---------");
|
|
|
|
|
Recursive.pos(head);
|
|
|
|
|
System.out.println();
|
|
|
|
|
|
|
|
|
|
UnRecursive.pos(head);
|
|
|
|
|
Recursive.pos1(head);
|
|
|
|
|
System.out.println();
|
|
|
|
|
UnRecursive.pos1(head);
|
|
|
|
|
System.out.println("---------");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@ -62,6 +59,15 @@ class Recursive{
|
|
|
|
|
pre(node.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void pre1(TreeNode node) {
|
|
|
|
|
if (node == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
System.out.print(node.value+" ");
|
|
|
|
|
pre1(node.left);
|
|
|
|
|
pre1(node.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void in(TreeNode node) {
|
|
|
|
|
if (node == null) {
|
|
|
|
|
return;
|
|
|
|
|
@ -70,6 +76,14 @@ class Recursive{
|
|
|
|
|
System.out.print(node.value+" ");
|
|
|
|
|
in(node.right);
|
|
|
|
|
}
|
|
|
|
|
public static void in1(TreeNode node){
|
|
|
|
|
if (node == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
in1(node.left);
|
|
|
|
|
System.out.print(node.value+" ");
|
|
|
|
|
in1(node.right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void pos(TreeNode node) {
|
|
|
|
|
if (node == null) {
|
|
|
|
|
@ -80,6 +94,14 @@ class Recursive{
|
|
|
|
|
System.out.print(node.value+" ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void pos1(TreeNode node) {
|
|
|
|
|
if (node == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pos1(node.left);
|
|
|
|
|
pos1(node.right);
|
|
|
|
|
System.out.print(node.value + " ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -93,7 +115,7 @@ class UnRecursive{
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
System.out.print("pre-order: ");
|
|
|
|
|
System.out.println("pre-order: ");
|
|
|
|
|
Stack<TreeNode> stack = new Stack<>();
|
|
|
|
|
stack.add(head);
|
|
|
|
|
while (!stack.isEmpty()) {
|
|
|
|
|
@ -109,6 +131,26 @@ class UnRecursive{
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void pre1(TreeNode head){
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Stack<TreeNode> stack = new Stack<>();
|
|
|
|
|
stack.push(head);
|
|
|
|
|
while (!stack.isEmpty()) {
|
|
|
|
|
TreeNode cur = stack.pop();
|
|
|
|
|
System.out.print(cur.value+" ");
|
|
|
|
|
if (cur.right != null) {
|
|
|
|
|
stack.push(cur.right);
|
|
|
|
|
}
|
|
|
|
|
if (cur.left != null) {
|
|
|
|
|
stack.push(cur.left);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 中序:左头右
|
|
|
|
|
*/
|
|
|
|
|
@ -116,7 +158,7 @@ class UnRecursive{
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
System.out.print("in-order: ");
|
|
|
|
|
System.out.println("in-order: ");
|
|
|
|
|
Stack<TreeNode> stack = new Stack<>();
|
|
|
|
|
TreeNode cur = head;
|
|
|
|
|
while (cur != null || !stack.isEmpty()) {
|
|
|
|
|
@ -133,7 +175,45 @@ class UnRecursive{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void in1(TreeNode head){
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Stack<TreeNode> stack = new Stack<>();
|
|
|
|
|
TreeNode cur = head;
|
|
|
|
|
while (!stack.isEmpty() || cur != null) {
|
|
|
|
|
if (cur != null) {
|
|
|
|
|
stack.push(cur);
|
|
|
|
|
cur = cur.left;
|
|
|
|
|
} else {
|
|
|
|
|
cur = stack.pop();
|
|
|
|
|
System.out.print(cur.value + " ");
|
|
|
|
|
cur = cur.right;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void in2(TreeNode head) {
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Stack<TreeNode> stack = new Stack<>();
|
|
|
|
|
TreeNode cur = head;
|
|
|
|
|
while (!stack.isEmpty() || cur != null) {
|
|
|
|
|
if (cur != null) {
|
|
|
|
|
stack.push(cur);
|
|
|
|
|
cur = cur.left;
|
|
|
|
|
}else{
|
|
|
|
|
cur = stack.pop();
|
|
|
|
|
System.out.print(cur.value + " ");
|
|
|
|
|
cur = cur.right;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 左右头
|
|
|
|
|
@ -142,7 +222,7 @@ class UnRecursive{
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
System.out.print("pos-order: ");
|
|
|
|
|
System.out.println("pos-order: ");
|
|
|
|
|
Stack<TreeNode> s1 = new Stack<>();
|
|
|
|
|
Stack<TreeNode> s2 = new Stack<>();
|
|
|
|
|
TreeNode cur = head;
|
|
|
|
|
@ -164,6 +244,29 @@ class UnRecursive{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void pos1(TreeNode head){
|
|
|
|
|
if (head == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Stack<TreeNode> s1 = new Stack<>();
|
|
|
|
|
Stack<TreeNode> s2 = new Stack<>();
|
|
|
|
|
TreeNode cur = head;
|
|
|
|
|
s1.push(cur);
|
|
|
|
|
while (!s1.isEmpty()) {
|
|
|
|
|
cur = s1.pop();
|
|
|
|
|
s2.push(cur);
|
|
|
|
|
if (cur.left != null) {
|
|
|
|
|
s1.push(cur.left);
|
|
|
|
|
}
|
|
|
|
|
if (cur.right != null) {
|
|
|
|
|
s1.push(cur.right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
while (!s2.isEmpty()) {
|
|
|
|
|
System.out.print(s2.pop().value + " ");
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|