实现二叉搜索树插入

时间:2012-12-07 20:02:24

标签: java binary-search-tree

我正在尝试为二叉搜索树编写代码,我正在处理的第一个方法是add(insert)方法。 root似乎正确插入,但是在添加第二个节点时我得到空指针异常。我将在我的代码中用注释指出确切的问题点。

如果你能看到如何解决这些问题,或者让我知道我的整体逻辑是否存在缺陷,那将会非常有帮助.--我会提到这是针对学校的,所以我不打算做一个真的令人印象深刻的模型...我的大多数布局选择只是反映了我们在课堂上工作的方式。此外,方法名称由教师选择,应保持不变。随意编辑格式,有点麻烦。

BINARY TREE CLASS

public class BinarySearchTree 
{   
private static Node root;

    public BinarySearchTree()
    {
        root = null;
    }

    public static void Add (Node newNode)
    {
        Node k = root;
        if (root == null)//-----------------IF TREE IS EMPTY -----------------
        {
            root = newNode;
        }

        else // -------TREE IS NOT EMPTY --------

        {
        if (newNode.value > k.value) //-------NEW NODE IS LARGER THAN ROOT---------
        {   
            boolean searching = true;

            while(searching) // SEARCH UNTIL K HAS A LARGER VALUE
            {   //***CODE FAILS HERE****
                if(k.value > newNode.value || k == null) 
                {
                    searching = false;
                }
                else {k = k.rightChild; }
            }

            if ( k == null) { k = newNode;} 
            else if (k.leftChild == null){ k.leftChild = newNode;} 
            else 
            {
                Node temp = k.leftChild;
                k.leftChild = newNode;
                newNode = k.leftChild;

                if(temp.value > newNode.value )
                {
                    newNode.rightChild = temp;
                }
                else
                {
                    newNode.leftChild = temp;
                }
            }

        }

        if (newNode.value < k.value) //-----IF NEW NODE IS SMALLER THAN ROOT---
        {   
            boolean searching = true;

            while(searching) // ----SEARCH UNTIL K HAS SMALLER VALUE
            {// **** CODE WILL PROBABLY FAIL HERE TOO ***
                if(k.value < newNode.value || k == null) {searching = false;}
                else {k = k.leftChild;} 
            }

            if ( k == null) { k = newNode;} 
            else if (k.rightChild == null){ k.rightChild = newNode;} 
            else 
            {
                Node temp = k.rightChild;
                k.rightChild = newNode;
                newNode = k.rightChild;

                if(temp.value > newNode.value )
                {
                    newNode.rightChild = temp;
                }
                else
                {
                    newNode.leftChild = temp;
                }
            }
        }       
           }} // sorry having formatting issues
}

NODE CLASS

public class Node 
{
    int value;
    Node leftChild;
    Node rightChild;

    public Node (int VALUE)
    {
                value = VALUE;
    }
}

测试申请

public class TestIT
{

    public static void main(String[] args) 
    {

        BinarySearchTree tree1 = new BinarySearchTree();
        Node five = new Node(5);
        Node six = new Node(6);

        tree1.Add(five);
        tree1.Add(six);

        System.out.println("five value: " + five.value);
        System.out.println("five right: " + five.rightChild.value);
    }

}

1 个答案:

答案 0 :(得分:1)

从左到右检查条件语句,因此在检查k.value&gt;之前,您需要检查k是否为null。 newNode.value因为如果k为null,则它没有值。