簡體   English   中英

將節點插入BST

[英]Inserting a node into a BST

我的將節點插入BST的代碼無法正常工作。 當我嘗試顯示時,它僅顯示樹的最后兩個節點,即,它以某種方式覆蓋了樹中的節點。 我做錯了什么? 提前致謝!

public Node Insert(int value)
    {
        Node newNode = new Node();
        newNode.data = value;
        newNode.left = null;
        newNode.right = null;

        if(root == null){
            root = newNode;
            return root;
        }
        else{
            while(root != null){
                if(root.data < value){
                    if(root.right != null){
                        root = root.right;
                    }
                    else{
                        root.right = newNode;
                        break;
                    }
                }
                else{
                    if(root.left != null){
                        root = root.left;
                    }
                    else{
                        root.left = newNode;
                        break;
                    }

                }
            }
            return root;
        }

    }
    public void inOrder(){
        inOrder(this.root);
    }
    private void inOrder(Node root){
        if(root != null){
            inOrder(root.left);
            System.out.println(root.data);
            inOrder(root.right);
        }
    }

根指向樹的頂部節點。 在您改變其價值的同時。 您必須使用注釋變量(例如'n')。

“ n”的初始值為“ root”。 而在片刻內,您僅使用“ n”。

if(root == null){
        root = newNode;
        return root;
    }
    else{
        Node n = root;
        while(n != null){
        ...
        }
        return root;
        ...

您必須返回'root',而不是'n',因為root不會改變。

請參閱代碼中的注釋。 希望能幫助到你。

public Node Insert(int value)
{
    Node newNode = new Node();
    newNode.data = value;
    newNode.left = null;
    newNode.right = null;

    if(root == null){
        root = newNode;
        return root;
    }
    else{
        while(root != null){
            if(root.data < value){
                if(root.right != null){
                    root = root.right;
                    // root should point to the head of the tree.
                    /* Here you are changing the variable root to point to some other memory location and the reference to the head of the tree is lost. You should store the reference to the head of the tree in a temporary variable to return later  */
                }
                else{
                    root.right = newNode;
                    break;
                }
            }
            else{
                if(root.left != null){
                    root = root.left;
                   //root should point to the head of the tree.
                    /* Here you are changing the variable root to point to some other memory location and the reference to the head of the tree is lost. You should store the reference to the head of the tree in a temporary variable to return later  */
                }
                else{
                    root.left = newNode;
                    break;
                }

            }
        }
        // return temporary variable which points to the head of the tree here
        return root;
    }

}
public void inOrder(){
    inOrder(this.root);
}
private void inOrder(Node root){
    if(root != null){
        inOrder(root.left);
        System.out.println(root.data);
        inOrder(root.right);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM