Java double LinkedList删除节点

时间:2015-11-23 12:29:00

标签: java linked-list priority-queue

所以我的想法是制作一个Double Ended Priority Queue到目前为止我有一个树状结构使用2个链接列表,我有和接口我必须坚持不做任何改动。我遇到的问题是我必须制作两个名为getMost和getLeast的方法,它们获得最多或最少的节点,然后使该节点为空。但是这两种方法很难做到。你会怎么做呢?

我尝试过使用递归,但这很难实现,因为我必须通过运行tree.root来选择树,但是将tree.root传递给递归方法总是从tree.root启动它

此外,我已经尝试过我在inspectLeast()和inspectMost()中编写的内容,但Java通过值传递而不是通过引用。有什么提示吗?

P.S不允许使用java集合或java util中的任何内容。

public class PAS43DPQ implements DPQ
{
    //this is the tree
    TreeNode tree = new TreeNode();
    //this is for the size of the array
    int size = 0;

    @Override
    public Comparable inspectLeast() {
        return tree.inspectLeast(tree.root);
    }

    @Override
    public Comparable inspectMost() {
        return tree.inspectMost(tree.root);
    }


    @Override
    public void add(Comparable c)
    {
        tree.add(c);
        size++;
    }

    @Override
    public Comparable getLeast() {
        if (tree.root != null){

        }
        return getLeast();
    }

    @Override
    public Comparable getMost(){
        Comparable most = getMost();
        return most;
    }

    @Override
    public boolean isEmpty() {
        return (size > 0)?true:false;
    }

    @Override
    public int size() {
        return this.size;
    }


    class TreeNode{
        private Comparable value;
        private TreeNode left, right, root;

        //constructors
        public TreeNode() {}

        public TreeNode(TreeNode t) {
            this.value = t.value;
            this.left = t.left;
            this.right = t.right;
            this.root = t.root;
        }

        public TreeNode(Comparable c) {
            this.value = (int) c;
        }

        public void add(Comparable input){
            if(root == null){
                root = new TreeNode(input);
                return;
            } else {
                insert(root, input);
            }
        }

        public Comparable inspectLeast(TreeNode n){
            if (n == null)
                return null;

            if (n.left == null){
                TreeNode least = n;
                return least.value;
            }
            return inspectLeast(n.left);
        }

        public Comparable inspectMost(TreeNode n){
            if (n == null)
                return null;

            if (n.right == null){
                TreeNode most = n;
                return most.value;
            }
            return inspectMost(n.right);
        }

        public Comparable getMost(TreeNode n){
            if(n.right == null)
                return n.value;

            return tree.getMost(right);
        }

        public void insert(TreeNode n, Comparable input){
            if(input.compareTo(n.value) >= 0){
                if (n.right == null) {
                    n.right = new TreeNode(input);
                    return;
                }
                else
                    insert(n.right, input);
            }

            if(input.compareTo(n.value) < 0){
                if(n.left == null) {
                    n.left = new TreeNode(input);
                    return;
                }
                else
                    insert(n.left, input);
            }
        }
    }
}

0 个答案:

没有答案