二进制搜索树最适合Alg。:输出不正确

时间:2013-11-22 00:39:25

标签: c++ find binary-search-tree bin-packing best-fit

免责声明:这是针对学校作业的

嘿,大家好。我已经在这个Bin Packing计划上待了两个星期,我还有最后一个障碍:我的二叉搜索树的查找功能给我的结果不正确。

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 

我知道我已接近完成这项任务。我知道问题是什么,但我无法修复它。拜托,你能帮助我吗?

1 个答案:

答案 0 :(得分: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;
}

因为您要传入特定节点进行擦除,并且因为树中的多个容器可以具有相同的当前剩余容量,并且因为您的“最佳bin”逻辑返回递归中的最后一个最佳匹配,所以{ {1}}函数可能erase()错误的节点,实际上很可能。 (如果你的匹配代码总是返回第一个“最佳匹配”,我不确定你在这里有问题,虽然代码很脆弱。)

如果具有相同容量的垃圾箱真的无法区分,这实际上不会成为问题。但是,代码中的每个bin都有一个唯一的erase(),因此您可以断开您分配到的bin以及实际擦除和重新插入的bin之间的连接。

由于你传入一个指向树元素的指针,你应该继续在这里进行直接指针比较。

binNumber

无论如何,擦除不在树中的节点都是非法的,因此唯一有效的终止条件是您找到了节点。

找到要擦除的节点后,以下伪代码应正确旋转树:

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