Java中二叉樹的創建

很少說廢話,若是對二叉樹不夠了解的,能夠百度。node

在Java中創建二叉樹須要三個類,Node表示結點,Tree表示整棵樹,TreeMap表示對樹的操做測試

Node類的代碼以下所示:this

/**
 *
 * @author 小跳哥哥
 * 表示樹中一個普通的結點
 */
public class Node {

    private int iData;//結點的關鍵字
    private String others;//結點中的其餘數據,能夠是任意類型
    private Node leftChild;//結點的左孩子
    private Node rightChild;//結點的右孩子
    
    public Node() {
        
    }

    public Node(int iData, String others) {
        super();
        this.iData = iData;
        this.others = others;
    }

    public int getiData() {
        return iData;
    }

    public void setiData(int iData) {
        this.iData = iData;
    }

    public String getOthers() {
        return others;
    }

    public void setOthers(String others) {
        this.others = others;
    }

    public Node getLeftChild() {
        return leftChild;
    }

    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }

    public Node getRightChild() {
        return rightChild;
    }

    public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
    }
    
}遞歸

 

Tree類的代碼以下所示:get

public class Tree {

    private Node root;//樹的根結點

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
    
    //創建樹
    public void insert(Node node){
         Node current=root;
         int value=current.getiData();
         int key=node.getiData();
         while(true){
             //插入到根結點的左邊
             if(key<=value){
                 if(current.getLeftChild()==null){
                     current.setLeftChild(node);
                     break;
                 }
                 current=current.getLeftChild();
             }else{
                 if(current.getRightChild()==null){
                     current.setRightChild(node);
                     break;
                 }
                 current=current.getRightChild();
             }
            
             value=current.getiData();
         }
    }
    
    //前序遍歷 DLR
    public void preOrder(Node node){
        if(node==null){//記得加判斷條件,用遞歸遍歷樹很簡單
            return;
        }
        System.out.println("key:"+node.getiData()+" value:"+node.getOthers());
        preOrder(node.getLeftChild());
        preOrder(node.getRightChild());
        
    }
    
    //中序遍歷 LDR
    public void inOrder(Node node){
        if(node==null){
            return;
        }
        inOrder(node.getLeftChild());
        System.out.println("key:"+node.getiData()+" value:"+node.getOthers());
        inOrder(node.getRightChild());
    }
    
    //後序遍歷 LRD
    public void lastOrder(Node node){
        if(node==null){
            return;
        }
        lastOrder(node.getLeftChild());
        lastOrder(node.getRightChild());
        System.out.println("key:"+node.getiData()+" value:"+node.getOthers());
    }
    
}

ast

測試類以下所示:class

public class TreeApp {

    public static void main(String[] args) {
        Tree tree=new Tree();
        Node root=new Node(12, "A");
        tree.setRoot(root);
        
        //左孩子
        tree.insert(new Node(9,"B"));
        tree.insert(new Node(11,"C"));
        tree.insert(new Node(11,"D"));
        
        //右孩子
        tree.insert(new Node(15,"E"));
        tree.insert(new Node(13,"F"));
        tree.insert(new Node(20,"G"));
        
        System.out.println("先序遍歷:");
        tree.preOrder(root);
        
        System.out.println("中序遍歷:");
        tree.inOrder(root);
        
        System.out.println("後序遍歷:");
        tree.lastOrder(root);
        
    }
}

百度

就這樣創建起最基本的樹結構啦!二叉樹

相關文章
相關標籤/搜索