为什么我的程序不会进入我的while循环?

时间:2014-11-30 22:19:12

标签: java while-loop binary-search-tree

我正在开发一个程序,它将获取周期元素的名称和编号,将其存储在树中,然后以不同的顺序打印出来。

当我尝试运行我的主类时,它会询问名称,但是它不会进入while循环。

这是我的主要课程。

public class BinarySearchTree 
{

    public static void main(String[] args) 
    {
        Scanner conIn = new Scanner(System.in);

        String name;
        int atomicNum;

        BSTInterface<PeriodicElement> elements = new BSTree<PeriodicElement>();
        PeriodicElement element;
        int numElements;

        String skip;

        System.out.print("Element name (press Enter to end): ");
        name = conIn.nextLine();
        while (!name.equals(""));
        {
            System.out.print("Atomic Number: ");
            atomicNum = conIn.nextInt();
            skip = conIn.nextLine();

            element = new PeriodicElement(name, atomicNum);
            elements.add(element);

            System.out.print("Element name (press ENTER TO END): ");
            name = conIn.nextLine();
        }
        System.out.println();
        System.out.println("Periodic Elements");

        numElements = elements.reset(BSTree.INORDER);
        for (int count = 1; count <= numElements; count++)
        {
            System.out.println(elements.getNext(BSTree.INORDER));
        }

    }

}

这是我的二进制搜索树

public class BSTree <T extends Comparable<T>>
                implements BSTInterface<T>
{
    protected BSTNode<T> root;
    boolean found;

    protected LinkedUnbndQueue<T> inOrderQueue;
    protected LinkedUnbndQueue<T> preOrderQueue;
    protected LinkedUnbndQueue<T> postOrderQueue;

    public BSTree()
    {
        root = null;
    }

    public boolean isEmpty()
    {
        return (root == null);
    }

    private int recSize(BSTNode<T> tree)
    {
        if(tree == null)
        {
            return 0;
        }
        else
        {
            return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
        }
    }

    public int size()
    {
        int count = 0;
        {
            if(root != null)
            {
                LinkedStack<BSTNode<T>> hold = new LinkedStack<BSTNode<T>>();
                BSTNode<T> currNode;
                hold.push(root);
                while(!hold.isEmpty())
                {
                    currNode = hold.top();
                    hold.pop();
                    count++;
                    if(currNode.getLeft() != null)
                    {
                        hold.push(currNode.getLeft());
                    }
                    if(currNode.getRight() != null)
                    {
                        hold.push(currNode.getRight());
                    }
                }
            }
            //System.out.println(count);
            return count;
        }
    }

    public boolean recContains(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            return false;
        }
        else if(element.compareTo(tree.getInfo()) < 0)
        {
            return recContains(element, tree.getLeft());
        }
        else if(element.compareTo(tree.getInfo()) > 0)
        {
            return recContains(element, tree.getRight());
        }
        else
        {
            return true;
        }
    }

    public boolean contains (T element)
    {
        //System.out.println("Tree contains: " + recContains(element, root));
        return recContains(element, root);
    }

    public T recGet(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            return null;
        }
        else if(element.compareTo(tree.getInfo()) < 0)
        {
            return recGet(element, tree.getLeft());
        }
        else if(element.compareTo(tree.getInfo()) > 0)
        {
            return recGet(element, tree.getRight());
        }
        else
        {
            return tree.getInfo();
        }
    }

    public T get(T element)
    {
        //System.out.println(recGet(element, root));
        return recGet(element, root);
    }

    public void add(T element)
    {
        root = recAdd(element, root);
    }

    private BSTNode<T> recAdd(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            tree = new BSTNode<T>(element);
        }
        else if(element.compareTo(tree.getInfo()) <= 0)
        {
            tree.setLeft(recAdd(element, tree.getLeft()));
        }
        else
        {
            tree.setRight(recAdd(element, tree.getRight()));
        }
        return tree;
    }

    public boolean remove(T element)
    {
        root = recRemove(element, root);
        return found;
    }

    private BSTNode<T> recRemove(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            found = false;
        }
        else if (element.compareTo(tree.getInfo()) < 0)
        {
            tree.setLeft(recRemove(element, tree.getLeft()));
        }
        else if (element.compareTo(tree.getInfo()) > 0)
        {
            tree.setRight(recRemove(element, tree.getRight()));
        }
        else
        {
            tree = removeNode(tree);
            found = true;
        }
        return tree;
    }

    private BSTNode<T> removeNode(BSTNode<T> tree)
    {
        T data;

        if(tree.getLeft() == null)
        {
            return tree.getRight();
        }
        else if(tree.getRight() == null)
        {
            return tree.getLeft();
        }
        else
        {
            data = getPredecessor(tree.getLeft());
            tree.setInfo(data);
            tree.setLeft(recRemove(data, tree.getLeft()));
            return tree;
        }
    }

    private T getPredecessor(BSTNode<T> tree)
    {
        while (tree.getRight() != null)
        {
            tree = tree.getRight();
        }
        return tree.getInfo();
    }

    public int reset(int orderType)
    {
        int numNodes = size();

        if(orderType == INORDER)
        {
            inOrderQueue = new LinkedUnbndQueue<T>(numNodes);
        }
        else
        {
            if(orderType == PREORDER)
            {
                preOrderQueue = new LinkedUnbndQueue<T>(numNodes);
                preOrder(root);
            }
            if(orderType == POSTORDER)
            {
                postOrderQueue = new LinkedUnbndQueue<T>(numNodes);
                postOrder(root);
            }
        }
        return numNodes;
    }

    public T getNext(int orderType)
    {
        if(orderType == INORDER)
        {
            return inOrderQueue.dequeue();
        }
        else
        {
            if(orderType == PREORDER)
            {
                return preOrderQueue.dequeue();
            }
            else
            {
                if(orderType == POSTORDER)
                {
                    return postOrderQueue.dequeue();
                }
                else
                {
                    return null;
                }
            }
        }
    }

    private void inOrder(BSTNode<T> tree)
    {
        if(tree != null)
        {
            inOrder(tree.getLeft());
            inOrderQueue.enqueue(tree.getInfo());
            inOrder(tree.getRight());
        }
    }

    private void preOrder(BSTNode<T> tree)
    {
        if(tree != null)
        {
            preOrderQueue.enqueue(tree.getInfo());
            preOrder(tree.getLeft());
            preOrder(tree.getRight());
        }
    }

    private void postOrder(BSTNode<T> tree)
    {
        if(tree != null)
        {
            postOrder(tree.getLeft());
            postOrder(tree.getRight());
            postOrderQueue.enqueue(tree.getInfo());
        }
    }
}

这是二进制搜索树节点类

public class BSTNode <T extends Comparable<T>>
{
    protected T info;
    protected BSTNode<T> left;
    protected BSTNode<T> right;

    public BSTNode(T info)
    {
        this.info = info;
        left = null;
        right = null;
    }

    public void setInfo(T info)
    {
        this.info = info;
    }

    public T getInfo()
    {
        return info;
    }

    public void setLeft(BSTNode<T> link)
    {
        left = link;
    }

    public void setRight(BSTNode<T> link)
    {
        right = link;
    }

    public BSTNode<T> getLeft()
    {
        return left;
    }

    public BSTNode<T> getRight()
    {
        return right;
    }
}

2 个答案:

答案 0 :(得分:3)

删除终止while语句

的分号
while (!name.equals(""));
                        ^

答案 1 :(得分:0)

在while语句之后删除半冒号,因为半冒号使它结束,而{是一个开头,如果某事是真的或不是(!),它应该以你明显知道的结束}结束。