Node Heap将额外的空节点打印到数组中

时间:2016-04-21 08:45:06

标签: java null heap nodes

我正在为二进制堆的链接节点表示编写代码,但我遇到了一些问题。在下面的代码中,当我使用NodeHeap类中的toArray方法在主类中打印数组时,我得到了这个输出:

[david 7, Ocsar 6, Kevin 3, Kim 4, Andrew 2, null, null]

问题是,为什么我在数组的末尾得到这两个空值,我该如何摆脱它们?我怀疑它可能与我的toArray方法或我的add方法有关,但我不确定究竟是什么导致了错误。

import java.util.*;
public class NodeHeap<V extends Comparable<V>> implements Heap<V> {

    Node<V> root;
    int nodeCount;

    public NodeHeap() {
        root = null;
        nodeCount = 0;

    }

    public void resetHeap() {
        root = null;
        nodeCount = 0;
    }

    /**
     * Use a queue. Start from root and enque the parent, and then deque the
     * parent. When dequeing the parent, enque its right and left children,
     * repeat until queue is empty.
     */
    public V[] toArray(V[] array) {
        V[] outputArr = (V[]) java.lang.reflect.Array.newInstance(array.getClass().getComponentType(), nodeCount);
        ArrayList<Node> queue = new ArrayList<Node>();
        int index = 0;
        Node<V> temp = null;
        System.out.println("NodeCount in NodeHeap: " + nodeCount);

        if( root == null) {
            return null;
        }
        queue.add(root);


        while (!queue.isEmpty()) {
            temp = queue.remove(0);
            outputArr[index] = temp.data;


            if (temp.leftChild != null) {
                queue.add(temp.leftChild);
            } 
            if (temp.rightChild != null) {
                queue.add(temp.rightChild); 
            }
            index++;

        }
        return outputArr;
    }

    public void fromArrary(V[] array) {
        resetHeap();

        for (int i = 0; i < array.length; i++) {
            add(array[i]);
        }

    }

    /**
     * Need to implement.
     */
    public V[] getSortedContents(V[] array) {

        return null;
    }

    /**
     * Need to figure out why it adds two more nulls to the end of the 
     * output array.
     */
    public void add(V value) {
        Node<V> temp = root;

        if (nodeCount == 0) {
            root = new Node<V>(null, value);
            nodeCount++;
        } else {
            String direction = (Integer.toBinaryString(nodeCount + 1));
            direction = direction.substring(1);

            for (int i = 0; i < direction.length(); i++) {
                if (direction.charAt(i) == '1') {

                    if (temp.rightChild == null) {
                        temp.rightChild = new Node<V>(temp, value);
                        siftUp(temp.rightChild);
                    } else {
                        temp = temp.rightChild;
                    }
                } else {
                    if (temp.leftChild == null) {
                        temp.leftChild = new Node<V>(temp, value);
                        siftUp(temp.leftChild);
                    } else {
                        temp = temp.leftChild;
                    }
                }
                nodeCount++;
            }
        }
    }

    /**
     * Still needs to test.
     */
    public V remove() {
        Node<V> temp = root;
        V elementRemoved = root.data;

        String direction = (Integer.toBinaryString(nodeCount));
        direction = direction.substring(1);

        if (root == null) {
            return null;
        } else if (nodeCount == 1) {
            root = null;
            nodeCount--;
            return elementRemoved;
        }

        for (int i = 0; i < direction.length(); i++) {

            if (direction.charAt(i) == '1') {
                temp = temp.rightChild;
            } else {
                temp = temp.leftChild;
            }
        }

        swap(root, temp);
        temp = null;
        nodeCount--;
        siftDown(root);
        return elementRemoved;

    }

    /**
     * seems to be working.
     */
    public void siftUp(Node<V> last) {

        if (last == root) {
            return;
        }

        if (last.data.compareTo(last.parent.data) > 0) {
            swap(last.parent, last);
            siftUp(last.parent);
        } /**
             * while(last != root && (last.data.compareTo(last.parent.data) >
             * 0)) { swap(last.parent, last); last = last.parent; }
             **/

    }

    /**
     * Need to fix
     * 
     */
    public void siftDown(Node<V> min) {
        boolean notDone = true;
        Node<V> temp;

        while (notDone) {
            if (!min.isLeaf()) {
                temp = min;
                if (min.data.compareTo(min.leftChild.data) < 0 || min.data.compareTo(min.rightChild.data) < 0) {
                    if (min.leftChild.data.compareTo(min.rightChild.data) >= 0) {
                        swap(temp, min.leftChild);
                    } else {
                        swap(temp, min.rightChild);
                    }
                }
            } else {
                notDone = false;
            }
        }
    }

    /**
     * Need to implement
     */
    public void heapify() {     

    }
    public void swap(Node<V> ele1, Node<V> ele2) {
        Node<V> temp = new Node();
        temp.data = ele1.data;
        ele1.data = ele2.data;
        ele2.data = temp.data;
    }

    public boolean isEmpty() {
        return nodeCount == 0;
    }
    /**
     * Need to implement
     */
    public String toString() {
        Node<V> temp = root;

    return  null;
    }

    public static class Node<V> implements Comparable<V> {
        Node<V> leftChild;
        Node<V> rightChild;
        Node<V> parent;
        V priority;
        V data;

        /**
         * Empty constructor. Not sure what this is used in.
         * need to find out.
         */
        Node() {

        }

        Node(Node<V> parent, V data) {
            this.parent = parent;
            this.data = data;
        }

        public boolean isLeaf() {
            return (leftChild == null && rightChild == null);
        }


    }
    }

    public class Main {

    /**
     * @param <V>
     * @param args
     */
    public static <V> void main(String[] args) {

        NodeHeap<Customer> test = new NodeHeap<Customer>();
        Customer[] test2 = { new Customer("david", 7), new Customer("Ocsar", 6), new Customer("Andrew", 2), new Customer("Kim", 4), new Customer("Kevin", 3)};
        System.out.println("NodeCount: " + test.nodeCount);
        //test.swap(test.root, test.root.leftChild);
        System.out.println(test.root.rightChild.data.name);
        test.toArray(test2);
        //test.siftDown(test.root.leftChild);
        //test.remove();

        System.out.println(Arrays.toString(test.toArray(test2)));
    }
}

0 个答案:

没有答案