BST的插入方法

时间:2019-05-07 09:01:42

标签: java insert binary-search-tree

我能够为二进制搜索树实现布尔插入方法。它获取通用数据并将密钥插入BST。我的代码不允许重复,并且如果BST中已经存在密钥,则返回false。如果插入成功,则返回true。

问题在于,我认为它不会增加BST中存在的元素数量,只能达到2个元素。

我尝试调试它,看看为什么我的输出和期望值偏离了一个。

这是我的构造函数和方法

public class BSTree<T extends Comparable<? super T>> implements Iterable {

    private int nelems;
    private BSTNode root;
    private BSTNode curr;
    private BSTNode keyHere;

    protected class BSTNode {

        T key;
        LinkedList<T> dataList;
        BSTNode left;
        BSTNode right;


        public BSTNode(BSTNode left, BSTNode right, LinkedList<T> dataList, T key) {
            //TODO
            this.left = left;
            this.right = right;
            this.key = key;
            setDataList(dataList);
        }

        public BSTNode(BSTNode left, BSTNode right, T key) {
            //TODO
            this.left = left;
            this.right = right;
            this.key = key;
            this.dataList = new LinkedList<T>();

        }

        public T getKey() {
            //TODO
            return this.key;
        }

        public BSTNode getLeft() {
            //TODO
            return this.left;
        }

        public BSTNode getRight() {
            //TODO
            return this.right;
        }

        public LinkedList<T> getDataList() {
            //TODO
            return this.dataList;
        }

        public void setleft(BSTNode newleft) {
            //TODO
            this.left = newleft;
        }

        public void setright(BSTNode newright) {
            //TODO
            this.right = newright;
        }

        public void setDataList(LinkedList<T> newData) {
            //TODO
            this.dataList = newData;
        }

        public void addNewInfo(T data) {
            //TODO
            if (data != null)
            {
                getDataList().addLast(data);
            }

        }

        public boolean removeInfo(T data) {


            return dataList.remove(data);

        }
    }

    public BSTree() {
        //TODO
        root = null;
        nelems = 0;
        curr = null;
        keyHere = null;

    }

这是我的插入代码

public boolean insert(T key) {


        if (key == null)
        {
            throw new NullPointerException();
        }

        if (findKey(key)) // this method returns true if key is in BST
        {
            return false;
        }

        if (getSize() == 0)
        {
            LinkedList<T> linked = new LinkedList<T>();
            BSTNode node = new BSTNode(null, null, linked,key);
            root = node;
            curr = root;
            nelems = 1;
        }
        else
         {
            if (curr.getKey().compareTo(key) < 0)
            {
                if(curr.getRight() != null)
                {
                    curr = curr.getRight();
                    insert(key);
                }
                else
                {
                    LinkedList<T> linked = new LinkedList<T>();
                    BSTNode node = new BSTNode(null,null,linked,key);
                    curr.setright(node);
                    nelems += 1 ;
                    curr = root;
                }

            }
            else
            {
                if (curr.getLeft() != null)
                {
                    curr = curr.getLeft();
                    insert(key);
                }
                else
                {
                    LinkedList<T> linked = new LinkedList<T>();
                    BSTNode node = new BSTNode(null,null,linked,key);
                    curr.setleft(node);
                    curr = root;
                    nelems += 1;
                }

            }
         }

        return true;

    }
public boolean findKey(T key) {

        if (key == null)
        {
            throw new NullPointerException();
        }

        if (getSize() == 0)
        {
            return false;
        }
        else if(curr.getKey().compareTo(key)!= 0)
        {
            if(curr.getKey().compareTo(key) < 0)
            {
                if (curr.getRight() != null)
                {
                    curr = curr.getRight();
                    findKey(key);
                }
                else
                {
                    this.curr = root;
                    return false;
                }
            }
            else
            {
                if (curr.getLeft() != null)
                {
                    curr = curr.getLeft();
                    findKey(key);
                }
                else
                {
                    this.curr = root;
                    return false;
                }
            }
        }
        else
        {
            this.keyHere = curr;
            this.curr = root;
            return true;
        }
        return true;

    }

当我将值插入树时,它会跟踪前2个元素,但是在添加第三个元素后,由于某种原因,它会停止递增元素。 这是我的测试人员

public class BSTreeTester{
    BSTree<Integer> tree1;
    public void setUp() throws Exception {
        tree1 = new BSTree<Integer>();
    }
    public void testinsert() {
        tree1.insert(new Integer(100));
        assertEquals("The size of the tree should be 1", 1, tree1.getSize());
        assertEquals("The root of the tree should be 100", 100,
                (int)tree1.getRoot().getKey());

        tree1.insert(new Integer(200));
        assertEquals("The size of the tree shoul dbe 2", 2, tree1.getSize());
        assertEquals("The root of the tree should still be 100", 100,
                (int)tree1.getRoot().getKey());
        tree1.insert(new Integer(300));
        assertEquals("The size of the tree should be 3", 3, tree1.getSize());
        assertEquals("The root of the tree should still be 100", 100,
                (int)tree1.getRoot().getKey());

}

插入第三个新整数时,期望值应为3,但给我2。

0 个答案:

没有答案