先根遍歷,中根遍歷,後根遍歷 二叉樹並且 循環+遞歸兩種方式
public class BinaryTreeNode {
private int value;
public BinaryTreeNode leftNode;
public BinaryTreeNode rightNode;
public void setValue(int v){
this.value = v;
}
public int getValue(){
return this.value;
}
}
public class BinaryTree {
public static BinaryTreeNode treeRoot;
static{
treeRoot = new BinaryTreeNode();
treeRoot.setValue(1);
treeRoot.leftNode = new BinaryTreeNode();
treeRoot.leftNode.setValue(2);
treeRoot.rightNode = new BinaryTreeNode();
treeRoot.rightNode.setValue(3);
treeRoot.leftNode.leftNode = new BinaryTreeNode();
treeRoot.leftNode.leftNode.setValue(4);
treeRoot.leftNode.rightNode = new BinaryTreeNode();
treeRoot.leftNode.rightNode.setValue(5);
treeRoot.rightNode.leftNode = new BinaryTreeNode();
treeRoot.rightNode.leftNode.setValue(6);
treeRoot.rightNode.rightNode = new BinaryTreeNode();
treeRoot.rightNode.rightNode.setValue(7);
}
}
package Chapter2;
public class BinaryTreeNodeCode{
private BinaryTreeNode binaryTreeNode;
private int code;
public BinaryTreeNodeCode( BinaryTreeNode binaryTreeNode,int code){
this.binaryTreeNode = binaryTreeNode;
this.code = code;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public BinaryTreeNode getBinaryTreeNode() {
return binaryTreeNode;
}
}
import java.util.ArrayList;
import java.util.LinkedList;
public class BinaryTreeOprate {
public static void printTreeFirstRoot(BinaryTreeNode treeRoot){
if(treeRoot == null){
return;
}
System.out.println(treeRoot.getValue());
printTreeFirstRoot(treeRoot.leftNode);
printTreeFirstRoot(treeRoot.rightNode);
}
public static void printTreeSecoundRoot(BinaryTreeNode treeRoot){
if(treeRoot == null){
return;
}
printTreeSecoundRoot(treeRoot.leftNode);
System.out.println(treeRoot.getValue());
printTreeSecoundRoot(treeRoot.rightNode);
}
public static void printTreeThiredRoot(BinaryTreeNode treeRoot){
if(treeRoot == null){
return;
}
printTreeThiredRoot(treeRoot.leftNode);
printTreeThiredRoot(treeRoot.rightNode);
System.out.println(treeRoot.getValue());
}
public static void printTreeFirstRootFor(BinaryTreeNode treeRoot){
if(treeRoot == null){
return;
}
LinkedList<BinaryTreeNode> treeNodeStack = new LinkedList<>();
treeNodeStack.push(treeRoot);
while(!treeNodeStack.isEmpty()){
BinaryTreeNode treeNode = treeNodeStack.getLast();
treeNodeStack.removeLast();
System.out.println(treeNode.getValue());
if(treeNode.rightNode != null){
treeNodeStack.addLast(treeNode.rightNode);
}
if(treeNode.leftNode != null){
treeNodeStack.addLast(treeNode.leftNode);
}
}
}
public static void printTreeSecoundRootFor(BinaryTreeNode treeRoot){
if(treeRoot == null){
return;
}
LinkedList<BinaryTreeNode> treeNodeStack = new LinkedList<>();
treeNodeStack.addLast(treeRoot);
while(!treeNodeStack.isEmpty()){
if(treeNodeStack.getLast().leftNode != null){
treeNodeStack.addLast(treeNodeStack.getLast().leftNode);
}else{
BinaryTreeNode treeNode = treeNodeStack.getLast();
System.out.println(treeNode.getValue());
if(treeNode.rightNode != null){
treeNodeStack.addLast(treeNode.rightNode);
}
}
}
}
public static void printTreeThiredRootFor(BinaryTreeNode treeRoot) throws Exception{
if(treeRoot == null){
return;
}
LinkedList<BinaryTreeNodeCode> treeNodeStack = new LinkedList<>();
treeNodeStack.addLast(new BinaryTreeNodeCode(treeRoot, 1));
while(!treeNodeStack.isEmpty()){
if(treeNodeStack.getLast().getCode() == 1){
BinaryTreeNodeCode treeNodeCode = treeNodeStack.getLast();
treeNodeCode.setCode(2);
if(treeNodeCode.getBinaryTreeNode().rightNode != null){
treeNodeStack.addLast(new BinaryTreeNodeCode(treeNodeCode.getBinaryTreeNode().rightNode, 1));
}
if(treeNodeCode.getBinaryTreeNode().leftNode != null){
treeNodeStack.addLast(new BinaryTreeNodeCode(treeNodeCode.getBinaryTreeNode().leftNode, 1));
}
}else if(treeNodeStack.getLast().getCode() == 2){
System.out.println(treeNodeStack.getLast().getBinaryTreeNode().getValue());
treeNodeStack.removeLast();
}else{
throw new Exception("出現code != 1 && != 2");
}
}
}
public static void main(String[] args) throws Exception {
BinaryTreeNode treeRoot = BinaryTree.treeRoot;
System.out.println("======先根遍歷===遞歸=====");
printTreeFirstRoot(treeRoot);
System.out.println("======先根遍歷===循環=====");
printTreeFirstRootFor(treeRoot);
System.out.println("======中根遍歷===遞歸=====");
printTreeSecoundRoot(treeRoot);
System.out.println("======中根遍歷===循環=====");
printTreeSecoundRoot(treeRoot);
System.out.println("======後根遍歷===遞歸=====");
printTreeThiredRoot(treeRoot);
System.out.println("======後根遍歷===循環=====");
printTreeThiredRootFor(treeRoot);
}
}