总览
实现二叉树及其遍历方式
public class BinarayTree {
public Node<String> root;
class Node<T>{
public T data;
public Node<T> leftChild;
public Node<T> rightChild;
public Node(T data,Node<T> leftChild,Node<T> rightChild){
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
}
public BinarayTree(String data){
this.root = new Node<>(data,null,null);
}
public void createBinarayTree(){
Node<String> nodeB=new Node<String>("B",null,null);
Node<String> nodeC=new Node<String>("C",null,null);
Node<String> nodeD=new Node<String>("D",null,null);
Node<String> nodeE=new Node<String>("E",null,null);
Node<String> nodeF=new Node<String>("F",null,null);
Node<String> nodeG=new Node<String>("G",null,null);
Node<String> nodeH=new Node<String>("H",null,null);
Node<String> nodeJ=new Node<String>("J",null,null);
Node<String> nodeI=new Node<String>("I",null,null);
root.leftChild=nodeB;
root.rightChild=nodeC;
nodeB.leftChild=nodeD;
nodeC.leftChild=nodeE;
nodeC.rightChild=nodeF;
nodeD.leftChild=nodeG;
nodeD.rightChild=nodeH;
nodeE.rightChild=nodeJ;
nodeH.leftChild=nodeI;
}
/**
* 中序访问树的所有节点
*/
public void midOrderTraverse(Node root){//逻辑
if (root == null) {
return;
}
midOrderTraverse(root.leftChild);
System.out.println("midOrderTraverse = "+root.data);
midOrderTraverse(root.rightChild);
}
/**
* 前序访问树的所有节点 Arrays.sort();
*/
public void preOrderTraverse(Node root){
if(root==null){
return;
}
System.out.println("pre:"+root.data);
preOrderTraverse(root.leftChild);
preOrderTraverse(root.rightChild);
}
/**
* 后序访问树的所有节点
*/
public void postOrderTraverse(Node root){
if(root==null){
return;
}
postOrderTraverse(root.leftChild);
postOrderTraverse(root.rightChild);
System.out.println("post:"+root.data);
}
}