【发布时间】:2013-12-06 17:45:59
【问题描述】:
免责声明:这是学校作业
大家好。我已经在这个 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);
}
}
用户输入到 main(未显示)
# 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
我知道我即将完成这项任务。我知道问题是什么,但我无法解决它。拜托,你能帮帮我吗?
【问题讨论】:
-
你没有建立“最佳元素”的逻辑。当它完全匹配时,您只会返回一个非空节点。当对象大小与初始 bin 大小不匹配时,您的代码将如何运行?当你决定把东西装进垃圾箱时,你会怎么做?它的大小会改变,因此它在树中的位置也需要改变。
-
我实际上刚刚意识到我有一个旧版本的代码。我刚刚修改了它。输出也已更新。
-
好的,您的搜索现在看起来不错。但是,一旦你将一个对象分配给一个 bin,你会怎么做? bin 的剩余容量减少,因此 bin 在搜索树中的位置需要改变。你还没有向我们展示你在
find()一个节点之后做了什么。 -
好的,我刚刚添加了我的最佳拟合算法,它显示了找到节点后会发生什么。
-
这看起来很有希望。
erase和insert是做什么的?
标签: c++ find binary-search-tree bin-packing best-fit