使用<>非法开始表达

时间:2017-03-12 20:13:34

标签: java binary-search-tree compiler-warnings

每当我使用-Xlint运行代码时,我都会继续这样做:

Tester.java:11: error: illegal start of type
            DoubleThreadedBST<> BST = new DoubleThreadedBST()<>;
                              ^
Tester.java:11: error: illegal start of expression
            DoubleThreadedBST<> BST = new DoubleThreadedBST()<>;
                                                              ^
Tester.java:11: error: illegal start of expression
            DoubleThreadedBST<> BST = new DoubleThreadedBST()<>;

代码用于创建双线程二进制搜索树。我想实际测试我的代码,但我一直在收到这些错误

我的代码: DoubleThreadedBST 代码是selfexplanitory(也有评论)

public class DoubleThreadedBST<T extends Comparable<? super T>>
{
private DTNode<T> root; // the root node of the tree
public static boolean dLeft;
public static boolean dRight;
/*
TODO: You must complete each of the methods in this class to create
your own double threaded BST. You may add any additional methods
or data fields which you might need to accomplish your task. You
must NOT change the signatures of any methods given to you with this
class.
*/

public DoubleThreadedBST()
{
    root = null;
}


public DoubleThreadedBST(DoubleThreadedBST<T> other)
{
    root = other.getRoot();
}

public DoubleThreadedBST<T> clone()
{
    DoubleThreadedBST<T> other = new DoubleThreadedBST<T>();
    other.setRoot(this.root);
    return other;
}

public DTNode<T> getRoot()
{
    return root;
}

public void setRoot(DTNode<T> r){
    root = r;
}

public boolean insert(T element){
    DTNode<T> newNode = new DTNode<T>(element);
    if(root != null){
        if(contains(element) == false){
            if(root.hasLeftT() != true){
                root.setLeft(newNode);
                root.setHasLeftT(true);
                root.setLeftBit(1);
                newNode.setRight(root);
                newNode.setHasRightT(true);
                newNode.setRightBit(0);
                return true;
            }else if (root.hasRightT() != true){
                root.setRight(newNode);
                root.setHasRightT(true);
                root.setRightBit(1);
                newNode.setLeft(root);
                newNode.setHasLeftT(true);
                newNode.setLeftBit(0);
                return true;
            }else{
                DTNode<T> trav = root.getLeft();
                while(true){
                    if(trav.getData().compareTo(newNode.getData()) == 1){
                        if(trav.getLeftBit() == 0){
                            newNode.setLeft (trav.getLeft());
                            trav.setLeft(newNode);
                            newNode.setLeftBit (trav.getLeftBit());
                            trav.setLeftBit(1);
                            newNode.setRight(trav);
                            break;
                        }else{
                            trav = trav.getLeft();
                        }
                    }else{
                        if(trav.getRightBit() == 0){
                            newNode.setRight(trav.getRight());
                            trav.setRight(newNode);
                            newNode.setRightBit(trav.getRightBit());
                            trav.setRightBit(1);
                            newNode.setLeft(trav);
                            break;
                        }else{
                            trav = trav.getRight();
                        }
                    }
                }
            }

        }else{
            return false;
        }
    }else{
        root = newNode;
        root.setLeft(null);
        root.setRight(null);
        root.setHasLeftT(false);
        root.setHasRightT(false);
        return true;
    }

    /*
    The element passed in as a parameter should be
    inserted into this tree. Duplicates are not allowed.
    Left and right threads in the corresponding branch 
    must be updated accordingly, as necessary. 
    If the insert was successfull, the method should 
    return true. If the operation was unsuccessfull, 
    the method should return false.

    NB: Do not throw an exception.
    */

    return true;

}
/*
public boolean delete(T element){
    if(contains(element){
        DTNode<T> trav = root;
        while (trav.getLeft()!= null){
            trav = trav.getLeft();
        }
        while(trav != null){
            if(trav.getRight().getData() == element){
                DTNode<T> temp = trav.getRight();
                trav.setRight(keeper.getRight());
                while (temp 1= null){

                }
            }
            if(trav.hasRightT()){
                trav = trav.getRight();
            }else{
                trav = trav.getLeft();
            }
        }
    }
    */
    /*
    The element passed in as a parameter should be
    deleted from this tree. If the delete was successfull,
    the method should return true. If the operation was
    unsuccessfull, the method should return false. Eg, if
    the requested element is not found, return false.

    You have to implement the mirror case of delete by merging 
    as discussed in the textbook. That is, for a deleted node,
    its right child should replace it in the tree and not its
    left child as in the textbook examples. Relevant left and
    right threads must be updated accordingly.

    NB: Do not throw an exception.

    return false;
}
*/
public boolean contains(T element){
    if(root != null){
        DTNode<T> trav = root;
        while (trav.getLeft()!= null){
            trav = trav.getLeft();
        }
        while(trav != null){
            if(trav.getData() == element){
                return true;
            }
            if(trav.hasRightT()){
                trav = trav.getRight();
            }else{
                trav = trav.getLeft();
            }
        }
    }
    return false;
}

public String inorderAscending(){
    String tree = "";
    if(root != null){
        DTNode<T> trav = root;
        while (trav.getLeft()!= null){
            trav = trav.getLeft();
        }
        while(trav != null){
            tree += trav.getData();
            if(trav.getRight() != null){
                tree += ",";
            }
            if(trav.hasRightT()){
                trav = trav.getRight();
            }else{
                trav = trav.getLeft();
            }
        }
    }
    /*
    This method must return a string representation
    of the elements in the tree inorder, left to right. 
    This function must not be recursive. Instead, right 
    threads must be utilised to perform a depth-first 
    inorder traversal.

    If the tree looks like:

       B
      / \
     A   D
        / \
       C   E

    Then the following string must be returned:

    A,B,C,D,E

    Note that there are no spaces in the string, and
    the elements are comma-separated.
    */

    return tree;
}

public String inorderDescending(){
    String tree = "";
    if(root != null){
        DTNode<T> trav = root;
        while (trav.getRight()!= null){
            trav = trav.getRight();
        }
        while(trav != null){
            tree += trav.getData();
            if(trav.getLeft() != null){
                tree += ",";
            }
            if(trav.hasLeftT()){
                trav = trav.getLeft();
            }else{
                trav = trav.getRight();
            }
        }
    }
    /*
    This method must return a string representation
    of the elements in the tree inorder, right to left. 
    This function must not be recursive. Instead, left 
    threads must be utilised to perform a depth-first 
    inorder traversal.

    If the tree looks like:

       B
      / \
     A   D
        / \
       C   E

    Then the following string must be returned:

    E,D,C,B,A

    Note that there are no spaces in the string, and the elements are comma-separated.
    */

    return tree;
}

public int countRightThreads(){
    int count = 0;
    if(root != null){
        DTNode<T> trav = root;

        while (trav.getRight() != null){
            trav = trav.getRight();
            count ++;
        }
        while(trav != null){
            if(trav.hasLeftT()){
                trav = trav.getLeft();
            }else{
                count++;
                trav = trav.getRight();
            }
        }
    }
    /*
    This method should return the total number of right threads
    in the tree.
    */

    return count;
}

public int countLeftThreads(){
    int count = 0;
    if(root != null){
        DTNode<T> trav = root;

        while (trav.getLeft()!= null){
            trav = trav.getLeft();
            count++;
        }
        while(trav != null){
            if(trav.hasLeftT()){
                trav = trav.getLeft();
                count ++;
            }else{
                trav = trav.getRight();
            }
        }
    }
    /*
    This method should return the total number of left threads
    in the tree.
    */

    return count;
}

public int getNumberOfNodes()
{

    int count = 0;
    if(root != null){
        DTNode<T> trav = root;

        while (trav.getLeft()!= null){
            trav = trav.getLeft();
            count++;
        }
        while(trav != null){
            if(trav.hasLeftT()){
                trav = trav.getLeft();
                count ++;
            }else{
                trav = trav.getRight();
                count++;
            }
        }
    }
    /*
    This method should count and return the number of nodes 
    currently in the tree.
    */

    return count;
}

public int getHeight()
{
    /*
    This method should return the height of the tree. The height 
    of an empty tree is 0; the height of a tree with nothing but
    the root is 1.
    */

    return 0;
}
}

测试仪 仅供测试

    public class Tester 
{
    public static void main(String[] args) throws Exception
    {
    /*
    TODO: Write your code to test your implementation here.

    This file will be overwritten for marking purposes
    */

    DoubleThreadedBST<> BST = new DoubleThreadedBST()<>;

    BST.insert(1);
    BST.insert(2);
    BST.insert(3);
    BST.insert(5);
    BST.insert(4);
    System.out.println(BST.inorderAscending());
    System.out.println(BST.inorderDescending());
}
}

DTNode 有一个左边和右边,而我的...位显示他们左右是否指向父顺序或子节点。

public class DTNode<T extends Comparable<? super T>>
{
/*
TODO: You must implement a node class which would be appropriate to use with your trees.
Methods and variables can be added.
Names of the given variables must not be altered. 
*/


//setters

public DTNode(){
}

public DTNode(T elem){
    data = elem;
}

public void setLeft(DTNode<T> n){
    left = n;
}

public void setRight(DTNode<T> n){
    right = n;
}

public void setData(T elem){
    data = elem;
}

public void setHasLeftT(boolean t){
    hasLeftThread = t;
}

public void setHasRightT(boolean t){
    hasRightThread = t;
}

public void setLeftBit(int b){
    leftBit = b;
}
public void setRightBit(int b){
    rightBit= b;
}

//getters
public DTNode<T> getLeft(){
    return left;
}
public DTNode<T> getRight(){
    return right;
}

public T getData(){
    return data;
}

public boolean hasLeftT(){
    return hasLeftThread;
}

public boolean hasRightT(){
    return hasRightThread;
}

public int getLeftBit(){
    return leftBit;
}

public int getRightBit(){
    return rightBit;
}


protected T data;
protected DTNode<T> left, right; // left child and right child
protected boolean hasLeftThread, hasRightThread; // flags that indicate whether the left and the right pointers are threads
protected int leftBit, rightBit;

}

1 个答案:

答案 0 :(得分:1)

引用声明不接受菱形运算符,这意味着您必须发送泛型参数。请补充一下:

DoubleThreadedBST<> BST = new DoubleThreadedBST()<>;

使用:

DoubleThreadedBST<Integer> BST = new DoubleThreadedBST<>();