1

免責事項:これは学校の課題用です

ねえ、みんな。この Bin Packing プログラムで 2 週間苦労してきましたが、最後のハードルが 1 つあります。Binary Search Tree の検索機能で間違った結果が返されることです。

BinarySearchTree.cpp

#include "BinarySearchTree.h"

void BinarySearchTree::insert(int capacity, int binNumber)
{
    // Insert the Pair into the tree. Overwrite existing
    // pair, if any, with same key.
    // find place to insert
    BinaryTreeNode *p = root,
                         *pp = NULL;
    while (p != NULL)
    {// examine p->capacity
        pp = p;
        // move p to a child
        if (capacity <= p->capacity)
            p = p->leftChild;
        else
            p = p->rightChild;
    }

    // get a node for the Pair and attach to pp
    BinaryTreeNode *newNode = new BinaryTreeNode (capacity, binNumber);
    if (root != NULL) // the tree is not empty
        if (capacity <= pp->capacity)
            pp->leftChild = newNode;
        else
            pp->rightChild = newNode;
    else
        root = newNode; // insertion into empty tree
    treeSize++;
}

void BinarySearchTree::erase(BinaryTreeNode *n)
{
    // Delete the pair, if any, whose key equals n.

    // search for node with key theKey
    BinaryTreeNode *p = root,
                         *pp = NULL;
    while (p != NULL && p->capacity != n->capacity)
    {// move to a child of p
        pp = p;
        if (n->capacity < p->capacity)
            p = p->leftChild;
        else
            p = p->rightChild;
    }
    if (p == NULL)
        return; // no pair with key theKey

    // restructure tree
    // handle case when p has two children
    if (p->leftChild != NULL && p->rightChild != NULL)
    {// two children
        // convert to zero or one child case
        // find largest element in left subtree of p
        BinaryTreeNode *s = p->leftChild,
                *ps = p;  // parent of s
        while (s->rightChild != NULL)
        {// move to larger element
            ps = s;
            s = s->rightChild;
        }

        // move largest from s to p, can't do a simple move
        // p->capacity= s->capacity as key is const
        BinaryTreeNode *q = new BinaryTreeNode (s->capacity,s->binNumber, p->leftChild, p->rightChild, p->parent);
        if (pp == NULL)
            root = q;
        else if (p == pp->leftChild)
            pp->leftChild = q;
        else
            pp->rightChild = q;
        if (ps == p) pp = q;
        else pp = ps;
        delete p;
        p = s;
    }

    // p has at most one child
    // save child pointer in c
    BinaryTreeNode *c;
    if (p->leftChild != NULL)
        c = p->leftChild;
    else
        c = p->rightChild;

    // delete p
    if (p == root)
        root = c;
    else
    {// is p left or right child of pp?
        if (p == pp->leftChild)
            pp->leftChild = c;
        else pp->rightChild = c;
    }
    treeSize--;
    delete p;
}

BinaryTreeNode* BinarySearchTree::find(const int objectSize) const
{
    // Return pointer to pair with smallest key >= objectSize.
    // Return NULL if no element has key >= objectSize.
    BinaryTreeNode *currentNode = root,
                   *bestElement = NULL; // element with smallest key
                                     // >= theKey found so far

    // search the tree
    while (currentNode != NULL) {
        // is currentNode->capacity a candidate?
        if (currentNode->capacity >= objectSize)
        {
            // smaller keys in left subtree only
            bestElement = currentNode;
            currentNode = currentNode->leftChild;

        }
        else if (currentNode->capacity < objectSize)
        {
            // no, currentNode->capacity is too small
            // try right subtree

            currentNode = currentNode->rightChild;
        }
    }
    return bestElement;
}

BinaryTreeNode.h

struct BinaryTreeNode
{
    public:
        BinaryTreeNode *leftChild;
        BinaryTreeNode *rightChild;
        BinaryTreeNode *parent;
        int capacity;
        int binNumber;

        BinaryTreeNode() {leftChild = rightChild = parent = NULL;}
        BinaryTreeNode(const int& c, const int& b):capacity(c), binNumber(b)
        {
            leftChild = rightChild = parent = NULL;
        }
        BinaryTreeNode(const int& c, const int& b, BinaryTreeNode* l, BinaryTreeNode* r, BinaryTreeNode* p):capacity(c), binNumber(b)
        {
            leftChild = l;
            rightChild = r;
            parent = p;
        }
};

BinPacking.cpp

void BinPacking::bestFitPack(int *objectSize, int numberOfObjects, int binCapacity)
{// Output best-fit packing into bins of size binCapacity.
 // objectSize[1:numberOfObjects] are the object sizes.
   int n = numberOfObjects;
   int binsUsed = 0;
   BinarySearchTree theTree;  // tree of bin capacities
   BinaryTreeNode theBin;

   // pack objects one by one
   for (int i = 1; i <= n; i++)
   {// pack object i
      // find best bin
       BinaryTreeNode *bestBin = theTree.find(objectSize[i]);
      if (bestBin == NULL)
      {// no bin large enough, start a new bin
         theBin.capacity = binCapacity;
         theBin.binNumber = ++binsUsed;
      }
      else
      {// remove best bin from theTree
         theBin = *bestBin;
         theTree.erase(bestBin);
      }

      cout << "Pack object " << i << " in bin " << theBin.binNumber << endl;

      // insert bin in tree unless bin is full
      theBin.capacity -= objectSize[i];
      if (theBin.capacity > 0)
         theTree.insert(theBin.capacity, theBin.binNumber);
   }
}

メインへのユーザー入力 (表示されていません)

# of objects = 12
Bin capacity = 6

Sizes of objects:
object 1  = 2 
object 2  = 5
object 3  = 5
object 4  = 1
object 5  = 1
object 6  = 3
object 7  = 4
object 8  = 6
object 9  = 2
object 10 = 5
object 11 = 6
object 12 = 1

期待される出力

Pack object 1 in bin 1 
Pack object 2 in bin 2 
Pack object 3 in bin 3 
Pack object 4 in bin 2 
Pack object 5 in bin 3 
Pack object 6 in bin 1 
Pack object 7 in bin 4 
Pack object 8 in bin 5 
Pack object 9 in bin 4 
Pack object 10 in bin 6 
Pack object 11 in bin 7 
Pack object 12 in bin 1 

電流出力

Pack object 1 in bin 1 
Pack object 2 in bin 2 
Pack object 3 in bin 3 
Pack object 4 in bin 3 
Pack object 5 in bin 3 
Pack object 6 in bin 1 
Pack object 7 in bin 4 
Pack object 8 in bin 5 
Pack object 9 in bin 4 
Pack object 10 in bin 6 
Pack object 11 in bin 7 
Pack object 12 in bin 6 

私は、この任務がもうすぐ終わることを知っています。問題が何であるかはわかっていますが、それを修正できませんでした。お願いします、手伝ってくれませんか?

4

1 に答える 1

0

エラーは、コードの次の場所にあるようですerase():

while (p != NULL && p->capacity != n->capacity)
{// move to a child of p
    pp = p;
    if (n->capacity < p->capacity)
        p = p->leftChild;
    else
        p = p->rightChild;
}

消去する特定のノードを渡しているため、ツリー内の複数のビンが同じ現在の残りの容量を持つ可能性があるため、および「最適なビン」ロジックが再帰で最後に最適な一致を返すため、erase()関数がerase()間違っている可能性がありますノードであり、実際にはそうなる可能性があります。(マッチ コードが常に最初の「ベスト マッチ」を返した場合、コードは脆弱ですが、ここで問題が発生するかどうかはわかりません。)

同じ容量のビンが本当に区別できない場合、これは実際には問題になりません。ただし、コード内の各ビンには一意binNumberの があるため、割り当てたと言ったビンと、実際に消去して再挿入したビンとの間に切断が生じます。

ツリーの要素へのポインタを渡しているので、先に進んで直接ポインタ比較を行う必要があります。

while (p != n)
{// move to a child of p
    pp = p;
    if (p->capacity >= N->capacity)
        p = p->leftChild;
    else
        p = p->rightChild;
}

いずれにせよ、ツリーにないノードを消去することは違法であるため、唯一の有効な終了条件は、ノードを見つけたことです。

消去するノードが見つかったら、次の疑似コードでツリーを正しく回転させる必要があります。

if (pp->capacity >= n->capacity)
    parent_to_me = &pp->leftChild;
else
    parent_to_me = &pp->rightChild;

if (!node->leftChild && !node->rightChild)
{
    // point to nothing
    *parent_to_me = NULL;
} else if (node->leftChild)
{
    if (!node->rightChild)
    {
        // left child, but no right child:
        // promote left child to self
        *parent_to_me = node->leftChild;
    } else
    {
        // left and right children:  Make right child the
        // right child of the right-most left sub-child.
        // Replace myself with left child.
        Node *rmlsc = node->leftChild;

        while (rmlsc->rightChild)
            rmlsc = rmlsc->rightChild;

        rmlsc->rightChild = node->rightChild;

        *parent_to_me = node->leftChild;
    }
} else
    *parent_to_me = node->rightChild;
于 2013-11-22T02:57:32.917 に答える