1

因此,我设法使用以下代码获得了二进制左派堆的工作递归版本。

public class Node<T extends Comparable<T>>
{
    public T data;
    public Node<T> left;
    public Node<T> right;
    public int npl;

    public Node()
    {
        left = null;
        right = null;
        npl = 0;
    }
}

public class Leftist<T extends Comparable<T>>
{
    public Node<T> root;

    public Leftist()
    {
          root = null;
    }

    public void insert(T element)
    {
        if (root == null)
        {
            root = new Node();
        }
        else
        {
            Leftist temp = new Leftist();
            temp.insert(element);
            Merger(temp);
        }
    }

    public T remove()
    {

        T el = root.data;
        root = Merger(root.left, root.right);

        return el;
    }

    public Node<T> Merger(Node<T> old, Node<T> newer)
    {
        if (old == null)
            return newer;

        else if (newer == null)
            return old;
        else
        {
            if (old.data.compareTo(newer.data) < 0)
            {
                Node<T> temp = old;
                old = newer;
                old = temp;
            }
        }

        if (old.right == null)
            old.right = newer;
        else
            old.right = Merger (old.right, newer);

        if (old.left == null || old.right.npl > old.left.npl)
        {
            Node<T> temp = old.right;
            old.right = old.left;
            old.left = temp; 
        }

        if (old.right == null)
            old.npl = 0;
        else
            old.npl = old.right.npl + 1;

        return old;
    }
}

我想知道你们中的任何人是否可以帮助我弄清楚左派堆的 d 堆版本的逻辑和调整代码,这是一个具有 n 个子节点的左派堆。

任何帮助将不胜感激。

4

0 回答 0