查找最小优先级队列java中最旧的元素

时间:2016-11-28 03:45:18

标签: java element priority-queue

我应该返回优先级min队列中最旧的元素以及所述元素。我必须使用节点,数组是可选的。这是我到目前为止,但我在第20行有一个空指针错误,我不知道如何解决它。请帮忙

    public class MinHeap {
    public static int timeStamp = 0;
    public static int ts = 0;
    public static int maxTime = 0;
    public static  Node root;
    public MinHeap(){
        this.root = null;
    }

    public static void insert(int id, int ts){
        timeStamp++;
        Node newNode = new Node(id);
        if(root==null){
            root = newNode;
        }
        Node current = root;
        Node parent = null;
        while(true){
            parent = current;
            if(id < current.data){
                current = current.left;
                if(current==null){
                    parent.left = newNode;
                }
            }else{
                current = current.right;
                if(current==null){
                    parent.right = newNode;
                }
            }
        }

    }

    public static Node delete(int x, Node n){
        timeStamp++;
        if(n==null)
            return n;
        if(x == n.data){
            if(n.left == null && n.right == null){
                return null;
            }else if(n.left == null){
                n.right = delete(x, n.right);
                return n;
            }else if(n.right == null){
                n.left = delete(x, n.left);
                return n;
            }else{
                Node tempNode = findMin(n.right);
                n.right = delete(tempNode.data, n.right);
                n.data = tempNode.data;
                return n;
            }
        }
        if(x < n.data){
            n.left = delete(x, n.left);
            return n;
        }else{
            n.right = delete(x, n.right);
            return n;
        }

    }

    public static void display(Node root){
        if(root!=null){
            display(root.left);
            System.out.print(" " + root.data);
            display(root.right);
        }
    }

    public static Node findMin(Node n){
        if(n == null){
        return null;
       }
       if(n.left == null){
           return n;
       }
       return findMin(n.left);      
     }

    public static int maxAge(){
        int currentAge = 0;
        currentAge = timeStamp - ts;
        if(currentAge > maxTime)
           maxTime = currentAge;
        return maxTime;
    }

    public static void main(String [] arg){

        MinHeap min = new MinHeap();
        min.insert(1, ts = 0);
        min.insert(2, ts = 1);
        min.insert(3, ts = 2);
        min.insert(4, ts = 4);
        min.delete(1, root);
        min.delete(2, root);
        min.delete(3, root);
        min.delete(4, root);
        min.maxAge();
    }
}

class Node{
    int data;
    Node left;
    Node right;
    public Node(int data){
        this.data = data;
        left = null;
        right = null;
    }
}

1 个答案:

答案 0 :(得分:0)

你的插页坏了。您在找到插入点后忘记中断了,并且您没有检查密钥已经在树中的情况。

while (true) {
    parent = current;
    if (id < current.data) {
        current = current.left;
        if (current == null) {
            parent.left = newNode;
            // Break after insert
            break;
        }
    } else if (id > current.data) {
        current = current.right;
        if (current == null) {
            parent.right = newNode;
            // Break after insert
            break;
        }
    } else {
        // Key exists. 
        break;
    }
}