Is it possible to make efficient pointer-based binary heap implementations?

前端 未结 6 1866
无人共我
无人共我 2020-12-14 12:22

Is it even possible to implement a binary heap using pointers rather than an array? I have searched around the internet (including SO) and no answer can be found.

T

6条回答
  •  离开以前
    2020-12-14 13:00

    I have searched around the internet (including SO) and no answer can be found.

    Funny, because I found an answer on SO within moments of googling it. (Same Google search led me here.)

    Basically:

    • The node should have pointers to its parent, left child, and right child.
    • You need to keep pointers to:
      • the root of the tree (root) (duh)
      • the last node inserted (lastNode)
      • the leftmost node of the lowest level (leftmostNode)
      • the rightmost node of the next-to-lowest level (rightmostNode)

    Now, let the node to be inserted be nodeToInsert. Insertion algorithm in pseudocode:

    void insertNode(Data data) {
        Node* parentNode, nodeToInsert = new Node(data);
        if(root == NULL) { // empty tree
            parent = NULL;
            root = nodeToInsert;
            leftmostNode = root;
            rightmostNode = NULL;
        } else if(lastNode.parent == rightmostNode && lastNode.isRightChild()) { 
            // level full
            parentNode = leftmostNode;
            leftmostNode = nodeToInsert;
            parentNode->leftChild = nodeToInsert;
            rightmostNode = lastNode;
        } else if (lastNode.isLeftChild()) {
            parentNode = lastNode->parent;
            parentNode->rightChild = nodeToInsert;
        } else if(lastNode.isRightChild()) {
            parentNode = lastNode->parent->parent->rightChild;
            parentNode->leftChild = nodeToInsert;
        }
        nodeToInsert->parent = parentNode;
        lastNode = nodeToInsert;
        heapifyUp(nodeToInsert);
    }
    

    Pseudocode for deletion:

    Data deleteNode() {
        Data result = root->data;
        if(root == NULL) throw new EmptyHeapException();
        if(lastNode == root) { // the root is the only node
            free(root);
            root = NULL;
        } else {
            Node* newRoot = lastNode;
            if(lastNode == leftmostNode) {
                newRoot->parent->leftChild = NULL;
                lastNode = rightmostNode;
                rightmostNode = rightmostNode->parent;  
            } else if(lastNode.isRightChild()) {
                newRoot->parent->rightChild = NULL;
                lastNode = newRoot->parent->leftChild;
            } else if(lastNode.isLeftChild()) {
                newRoot->parent->leftChild = NULL;
                lastNode = newRoot->parent->parent->leftChild->rightChild;
            }
            newRoot->leftChild  = root->leftChild;
            newRoot->rightChild = root->rightChild;
            newRoot->parent = NULL;
            free(root);
            root = newRoot;
            heapifyDown(root);
        }
        return result;
    }
    

    heapifyUp() and heapifyDown() shouldn’t be too hard, though of course you’ll have to make sure those functions don’t make leftmostNode, rightmostNode, or lastNode point at the wrong place.

    TL;DR Just use a goddamn array.

提交回复
热议问题