0

我有一个具有以下定义的类,

class BinomialNode
    {
        public int key; // The key value
        public int x_point; // x co-ordinate for drawing
        public int y_point; // y co-ordinate for drawing
        public int degree;  // number of siblings/children for current node
        public BinomialNode parent;
        public BinomialNode child;
        public BinomialNode sibling;
        ...
    }

我们在大学里学习二项式堆,我已经在代码中实现了合并和插入算法。至少,当我暂停 Visual Studio 并查看“Locals”时(通过将鼠标悬停在变量上),我可以看到预期的数据。

作为一个实验,我在标准的“二项式节点”中添加了 2 个额外的变量。它们是 x_point 和 y_point。现在在程序执行期间我看到了这个, 在此处输入图像描述

请注意我在上面指出的区域。它应该代表同一个节点,但正如我们所见,x_point 的值是不同的。(在其他情况下,y_point 不同)

有谁知道为什么会这样?据我了解,如果它代表同一个节点,则数据应该是相同的。但它不是——这意味着它不是同一个节点。这怎么可能?如果我忽略我的“额外” x_point 和 y_point 变量,代码运行完美!事实上,我什至都不知道这是个问题。

从我的代码片段中看不到它,但 x_point 和 y_point 是我在类定义之外编辑的唯一值。其他的,虽然是公开的,但只能从中读取。

编辑:这是我制作的代码,

class BinomialNode
{
    public int key; // The key value
    public int x_point; // x co-ordinate for drawing
    public int y_point; // y co-ordinate for drawing
    public int degree;  // number of siblings/children for current node
    public BinomialNode parent;
    public BinomialNode child;
    public BinomialNode sibling;

    // Binomial Link takes the tree rooted at y and makes it a child of z
    private static void Binomial_Link(ref BinomialNode y,ref  BinomialNode z)
    {
        y.parent = z;
        y.sibling = z.child;
        z.child = y;
        z.degree++;
    }

    // This merges the root lists of H1 and H2 into a single linked list that is sorted
    // by degree in increasing order
    private static BinomialNode Binomial_Heap_Merge(BinomialNode H1, BinomialNode H2)
    {
        BinomialNode H = new BinomialNode();
        BinomialNode temp = H;
        if (H1 == null) // if it's the first insert
        {
            return H2;
        }
        while (H1 != null && H2 != null)
        {
            if (H1.degree < H2.degree)
            {
                // insert H1 into position
                temp.key = H1.key;
                temp.x_point = H1.x_point;
                temp.y_point = H1.y_point;
                temp.child = H1.child;
                temp.degree = H1.degree;
                temp.sibling = new BinomialNode();
                temp = temp.sibling;

                // move H1 to the next sibling
                H1 = H1.sibling;
            }
            else
            {
                // insert H2 into position
                temp.key = H2.key;
                temp.x_point = H2.x_point;
                temp.y_point = H2.y_point;
                temp.child = H2.child;
                temp.degree = H2.degree;
                temp.sibling = new BinomialNode();
                temp = temp.sibling;

                // move H2 to next sibling
                H2 = H2.sibling;
            }
        }

        // one of them hit null, so fill in the rest
        while (H1 != null)
        {
            // insert H1 into position
            temp.key = H1.key;
            temp.x_point = H1.x_point;
            temp.y_point = H1.y_point;
            temp.child = H1.child;
            temp.degree = H1.degree;
            temp.sibling = new BinomialNode();
            temp = temp.sibling;

            // move H1 to the next sibling
            H1 = H1.sibling;
        }
        while (H2 != null)
        {
            // insert H2 into position
            temp.key = H2.key;
            temp.x_point = H2.x_point;
            temp.y_point = H2.y_point;
            temp.child = H2.child;
            temp.degree = H2.degree;
            temp.sibling = new BinomialNode();
            temp = temp.sibling;

            // move H2 to the next sibling
            H2 = H2.sibling;
        }

        // To remove the extra node added,
        temp = H;
        while (temp != null)
        {
            if (temp.sibling.key == 0 && temp.sibling.sibling == null && temp.sibling.child == null)
            {
                // found the extra, now to get rid of it!
                temp.sibling = null;
            }
            temp = temp.sibling;
        }
        return H;  // send back the merged heap
    }

    // Unites the binomial heaps H1 & H2 and returns resulting heap
    public static BinomialNode Binomial_Heap_Union(BinomialNode H1, BinomialNode H2)
    {
        BinomialNode prev_x, x, next_x;
        BinomialNode H = new BinomialNode();
        H = Binomial_Heap_Merge(H1, H2);

        // simple checks
        if (H == null)
        {
            return H;
        }
        else
        {
            prev_x = null;
            x = H;
            next_x = x.sibling;
        }

        // now, for the actual merging
        while (next_x != null)
        {
            if ((x.degree != next_x.degree) || (next_x.sibling != null && x.degree == next_x.sibling.degree))
            {
                prev_x = x;
                x = next_x;
            }
            else if (x.key <= next_x.key)
            {
                x.sibling = next_x.sibling;
                Binomial_Link(ref next_x, ref x);
            }
            else
            {
                if (prev_x == null)
                {
                    H = next_x;
                }
                else
                {
                    prev_x.sibling = x.sibling;
                }
                Binomial_Link(ref x, ref next_x);
                x = next_x;
            }
            next_x = x.sibling;
        }

        // now, to return the merged heap
        return H; 
    }

    // inserting a key into a heap
    public static void Binomial_Heap_Insert(ref BinomialNode H, int x)
    {
        BinomialNode H_temp = new BinomialNode();
        H_temp.key = x;
        H_temp.parent = null;
        H_temp.degree = 0;
        H_temp.sibling = null;
        H_temp.child = null;
        H = Binomial_Heap_Union(H, H_temp);
    }
}

我使用表单窗口从用户那里获取数据以填充堆。输入在这里,

 private void button1_Click(object sender, EventArgs e)
        {
            BinomialNode.Binomial_Heap_Insert(ref B_HEAP1, Int32.Parse(numericUpDown1.Value.ToString()));

            // drawing the heap
            pictureBox1.Refresh();
            int x = 0;
            DrawNodeValue(pictureBox1, ref B_HEAP1, ref x, 0);
        }

我希望代码不是太糟糕?

4

1 回答 1

1

您正在创建一个新节点,然后将所有值复制过来。那是你想做的吗?如果您希望使用相同的节点,请使用相同的节点。

代替:

Node H = new Node();
Node temp = H;
if(node1 > node2)
  temp.values = node1.values
else
  temp.values = node2.values

只需使用实际对象...

Node temp;
if(node1 > node2)
  temp = node1;
else
  temp = node2;

我不确定这些值在哪里分开,但这就是为什么它们实际上不是同一个节点。

于 2012-02-24T17:01:22.890 回答