package jieduaner;
import jdk.nashorn.internal.ir.BinaryNode;
import java.util.Stack;
public class ErChaTreeFind {
/**
A
B E
C
D
F G
* @param args
*/
public static void main(String[] args) {
ErChaNode erChaNodeA = new ErChaNode("A");
ErChaNode erChaNodeB = new ErChaNode("B");
ErChaNode erChaNodeC = new ErChaNode("C");
ErChaNode erChaNodeD = new ErChaNode("D");
ErChaNode erChaNodeE = new ErChaNode("E");
ErChaNode erChaNodeF = new ErChaNode("F");
ErChaNode erChaNodeG = new ErChaNode("G");
erChaNodeA.leftNode = erChaNodeB;
erChaNodeA.rightNode = erChaNodeE;
erChaNodeB.leftNode = erChaNodeC;
erChaNodeB.parentNode = erChaNodeA;
erChaNodeC.rightNode = erChaNodeD;
erChaNodeC.parentNode = erChaNodeB;
erChaNodeD.leftNode = erChaNodeF;
erChaNodeD.rightNode = erChaNodeG;
erChaNodeD.parentNode = erChaNodeC;
erChaNodeF.parentNode = erChaNodeD;
erChaNodeG.parentNode = erChaNodeD;
erChaNodeE.parentNode = erChaNodeA;
ErChaTreeFind erChaTreeFind = new ErChaTreeFind();
//前序遍历
erChaTreeFind.preOut(erChaNodeA);
System.out.println("");
//中序遍历
erChaTreeFind.centerOut(erChaNodeA);
System.out.println("");
//后序遍历
erChaTreeFind.endOut(erChaNodeA);
System.out.println("");
//前序遍历
erChaTreeFind.preOutFor(erChaNodeA);
System.out.println("");
//中序遍历
erChaTreeFind.centerOutFor(erChaNodeA);
System.out.println("");
//后序遍历
erChaTreeFind.endOutFor(erChaNodeA);
System.out.println("");
}
//后序遍历左右中
private void endOutFor(ErChaNode erChaNode) {
Stack<ErChaNode> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
int i = 1;
while(erChaNode != null || !stack1.empty())
{
while (erChaNode != null)
{
stack1.push(erChaNode);
stack2.push(0);
erChaNode = erChaNode.leftNode;
}
while(!stack1.empty() && stack2.peek() == i)
{
stack2.pop();
System.out.print(stack1.pop().value);
}
if(!stack1.empty())
{
stack2.pop();
stack2.push(1);
erChaNode = stack1.peek();
erChaNode = erChaNode.rightNode;
}
}
}
//前序非递归 中前后
public void preOutFor(ErChaNode erChaNode){
//stack 记录 parentroot 节点
Stack<ErChaNode> erChaNodes = new Stack<>();
while(erChaNode != null || (erChaNodes.size()!=0)){
while (erChaNode != null) {
System.out.print(erChaNode.value);
erChaNodes.push(erChaNode);
erChaNode = erChaNode.leftNode;
}
if(erChaNodes.size()!=0){
ErChaNode erChaNodeRoot = erChaNodes.pop();
erChaNode = erChaNodeRoot.rightNode;
}
}
}
//中序非递归 前中后
public void centerOutFor(ErChaNode erChaNode){
//stack 记录 parentroot 节点
Stack<ErChaNode> erChaNodes = new Stack<>();
while(erChaNode != null || !erChaNodes.isEmpty()){
while(erChaNode != null){
erChaNodes.push(erChaNode);
erChaNode = erChaNode.leftNode;
}
if(!erChaNodes.empty()){
ErChaNode centerErChaNode = erChaNodes.pop();
System.out.print(centerErChaNode.value);
erChaNode = centerErChaNode.rightNode;
}
}
}
//前序遍历 中左右
public void preOut(ErChaNode erChaNode){
if(erChaNode == null){
return;
}
System.out.print(erChaNode.value);
preOut(erChaNode.leftNode);
preOut(erChaNode.rightNode);
}
//中序遍历 左中右
public void centerOut(ErChaNode erChaNode){
if(erChaNode == null){
return;
}
centerOut(erChaNode.leftNode);
System.out.print(erChaNode.value);
centerOut(erChaNode.rightNode);
}
//后序遍历 左右中
public void endOut(ErChaNode erChaNode){
if(erChaNode == null){
return;
}
endOut(erChaNode.leftNode);
endOut(erChaNode.rightNode);
System.out.print(erChaNode.value);
}
}
class ErChaNode{
public ErChaNode parentNode;
public ErChaNode leftNode;
public ErChaNode rightNode;
public String value;
public ErChaNode(String value){
this.value = value;
}
}
输出结果: