【java數據結構】java實現二叉樹(一)歡迎你們參與學習和指正

簡單二叉樹定義:一個節點下面最多擁有兩個子節點,而且兩個子節點分爲左值和右值,左值比父節點要小,右值比父節點要大,下面,咱們來利用java實現一棵以下圖中的二叉樹:java


你們能夠根據個人描述分析一下這棵二叉樹測試

下面就來寫代碼實現這棵二叉樹:this

首先是要創建一個節點類Node:spa

package Tree;

/**
 * 節點類
 * @author javadaodechengxuyuan
 *
 */
public class Node {
    private long value;
    private Node leftNode;//節點下面的左節點
    private Node RightNode;//節點下面的右節點
    
    //構造器
    public Node(long value){
        this.value=value;
    }

    public long getValue() {
        return value;
    }
    public void setValue(long value) {
        this.value = value;
    }
    public Node getLeftNode() {
        return leftNode;
    }
    public void setLeftNode(Node leftNode) {
        this.leftNode = leftNode;
    }
    public Node getRightNode() {
        return RightNode;
    }
    public void setRightNode(Node rightNode) {
        RightNode = rightNode;
    }
}


這是二叉樹類,就是這個類用來操做節點類的:code

package Tree;
/**
 * @author javadaodechengxuyuan
 * 二叉樹:每一個節點有最多兩個分叉,
 * 分別做爲父節點的左值和右值,遵循左小右大的規則進行分叉
 */
public class Tree {
    private Node root;
    private Node current;
    private Node parent;
    /**
     * @author javadaodechengxuyuan
     * 爲一顆二叉樹添加節點
     */
    public void insert(long value){//爲二叉樹插入新的節點
        //建立新的節點
        Node newNode=new Node(value);
        
        //建立完後就該考慮把這個節點放在哪裏了,下面這些代碼就是用來判斷將這個節點放在哪裏的
        if(root==null){
            this.root=newNode;//若是root爲空,那麼第一次調用添加時應給root初始化
        }else{
            this.current=root;//初始化current
            while(true){//進入死循環,一直等到給newNode找到合適的位置時進行終止死循環
                if(this.current.getValue()>value){//比root小,放在左側
                    this.parent=this.current;//讓parent一直保留本次的current
                    this.current=this.current.getLeftNode();
                    if(this.current==null){//若是當前的左值爲空,那麼就終止循環並賦值給這個左值
                        this.parent.setLeftNode(newNode);//將這個新節點放在這個位置
                        return;//最終找到合適位置,死循環終止
                    }
                }else{//比root大,放在右側
                    this.parent=this.current;//讓parent一直保留本次的current
                    this.current=this.current.getRightNode();//將當前的節點從新賦值給下一次須要比較的節點
                    if(this.current==null){//若是當前的右值爲空,那麼就終止循環並賦值給這個左值
                        this.parent.setRightNode(newNode);//將這個新節點放在這個位置
                        return;//最終找到合適位置,死循環終止
                    }
                }
            }
        }    
    }

    public Node getRoot() {
        return root;
    }

    public void setRoot(Node root) {
        this.root = root;
    }
}


這是測試類:get

package Tree;
/**
 * 測試類
 * @author javadaodechengxuyuan
 *
 */
public class Test {
    public static void main(String args[]){
        Tree t=new Tree();
        t.insert(10);//根節點
        t.insert(20);
        t.insert(15);
        t.insert(9);
        t.insert(35);
        System.out.print(t.getRoot().getValue()+"、");//第0層:根節點
        System.out.print(t.getRoot().getLeftNode().getValue()+"、");//第一層左值
        System.out.print(t.getRoot().getRightNode().getValue()+"、");//第一層右值
        System.out.print(t.getRoot().getRightNode().getLeftNode().getValue()+"、");//第二層左值
        System.out.print(t.getRoot().getRightNode().getRightNode().getValue());//第二層右值
        //輸出結果應爲:十、九、20、1五、35
    }
}

輸出結果應該爲:class

十、九、20、1五、35

這只是簡單的插入功能,下一節我會寫如何查找二叉樹的節點以及刪除節點、還有如何遍歷一棵二叉樹二叉樹

謝謝您的閱讀!
循環

相關文章
相關標籤/搜索