二叉树遍历
Node
public class Node {
public int value;
public Node left;
public Node right;
public Node ( int data) {
this . value = data;
}
}
recur
package com. wxl. DichotomizationTree. thinking;
import com. wxl. DichotomizationTree. Node. Node;
public class Recur {
public void preOrderRecur ( Node head) {
if ( head == null ) {
return ;
}
System. out. print ( head. value + " " ) ;
preOrderRecur ( head. left) ;
preOrderRecur ( head. right) ;
}
public void inOrderRecur ( Node head) {
if ( head == null ) {
return ;
}
inOrderRecur ( head. left) ;
System. out. print ( head. value + " " ) ;
inOrderRecur ( head. right) ;
}
public void postOrderRecur ( Node head) {
if ( head == null ) {
return ;
}
postOrderRecur ( head. left) ;
postOrderRecur ( head. right) ;
System. out. print ( head. value + " " ) ;
}
public static void main ( String[ ] args) {
Recur recur = new Recur ( ) ;
Node head = createTree ( ) ;
System. out. println ( "先序遍历" ) ;
recur. preOrderRecur ( head) ;
System. out. println ( ) ;
System. out. println ( "中序遍历" ) ;
recur. inOrderRecur ( head) ;
System. out. println ( ) ;
System. out. println ( "后序遍历" ) ;
recur. postOrderRecur ( head) ;
}
private static Node createTree ( ) {
Node head = new Node ( 0 ) ;
Node leftHead = head;
Node rightHead = head;
head. left = new Node ( 1 ) ;
head. right = new Node ( 2 ) ;
leftHead = leftHead. left;
leftHead. left = new Node ( 3 ) ;
leftHead. right = new Node ( 4 ) ;
rightHead = rightHead. right;
rightHead. left = new Node ( 5 ) ;
rightHead. right = new Node ( 6 ) ;
return head;
}
}
result
先序遍历
0 1 3 4 2 5 6
中序遍历
3 1 4 0 5 2 6
后序遍历
3 4 1 5 6 2 0
unRecur
package com. wxl. DichotomizationTree. thinking;
import com. wxl. DichotomizationTree. Node. Node;
import org. omg. CORBA. NO_IMPLEMENT;
import java. nio. charset. StandardCharsets;
import java. util. Stack;
public class unRecur {
public void preOrderUnRecur ( Node head) {
System. out. println ( "pre-Order" ) ;
if ( head == null) {
return ;
}
Stack< Node> stack = new Stack < Node> ( ) ;
stack. push ( head) ;
while ( ! stack. isEmpty ( ) ) {
head = stack. pop ( ) ;
System. out. print ( head. value + " " ) ;
if ( head. right != null) {
stack. push ( head. right) ;
}
if ( head. left != null) {
stack. push ( head. left) ;
}
}
System. out. println ( ) ;
}
public void inOrderUnRecur ( Node head) {
System. out. println ( "in-Order" ) ;
Node cur = head;
Stack< Node> stack = new Stack < Node> ( ) ;
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 void postOrderUnRecur ( Node head) {
System. out. println ( "post-order:" ) ;
if ( head == null) return ;
Stack< Node> s1 = new Stack < Node> ( ) ;
Stack< Node> s2 = new Stack < Node> ( ) ;
s1. push ( head) ;
while ( ! s1. isEmpty ( ) ) {
head = s1. pop ( ) ;
s2. push ( head) ;
if ( head. left != null) {
s1. push ( head. left) ;
}
if ( head. right != null) {
s1. push ( head. right) ;
}
}
while ( ! s2. isEmpty ( ) ) {
System. out. print ( s2. pop ( ) . value + " " ) ;
}
System. out. println ( ) ;
}
public static void main ( String[ ] args) {
unRecur unRecur = new unRecur ( ) ;
Node head = createTree ( ) ;
System. out. println ( "先序遍历" ) ;
unRecur. preOrderUnRecur ( head) ;
System. out. println ( "中序遍历" ) ;
unRecur. inOrderUnRecur ( head) ;
System. out. println ( "后续序遍历" ) ;
unRecur. postOrderUnRecur ( head) ;
System. out. println ( "后续序遍历==一个栈解决" ) ;
unRecur. postOrderUnRecurN1 ( head) ;
}
private void postOrderUnRecurN1 ( Node head) {
System. out. println ( "post-order:" ) ;
if ( head == null) return ;
Stack< Node> stack = new Stack < Node> ( ) ;
stack. push ( head) ;
Node c = null;
while ( ! stack. isEmpty ( ) ) {
c = stack. peek ( ) ;
if ( c. left != null && head != c. left && head != c. right) {
stack. push ( c. left) ;
} else if ( c. right != null && head != c. right) {
stack. push ( c. right) ;
} else {
System. out. print ( stack. pop ( ) . value+ " " ) ;
head = c;
}
}
System. out. println ( ) ;
}
private static Node createTree ( ) {
Node head = new Node ( 0 ) ;
Node leftHead = head;
Node rightHead = head;
head. left = new Node ( 1 ) ;
head. right = new Node ( 2 ) ;
leftHead = leftHead. left;
leftHead. left = new Node ( 3 ) ;
leftHead. right = new Node ( 4 ) ;
rightHead = rightHead. right;
rightHead. left = new Node ( 5 ) ;
rightHead. right = new Node ( 6 ) ;
return head;
}
}
result
先序遍历pre- Order
0 1 3 4 2 5 6
中序遍历
in- Order
3 1 4 0 5 2 6
后续序遍历
post- order:
3 4 1 5 6 2 0
后续序遍历== 一个栈解决
post- order:
3 4 1 5 6 2 0