提问人:cllsse 提问时间:10/28/2023 更新时间:10/28/2023 访问量:49
我在计算霍夫曼特里的WPL时遇到了问题
i have a problem while calculate WPL of huffmantree
问:
pStack initStack() {
pStack s = (pStack)malloc(sizeof(Stack));
s->top = -1;
s->stelem[0] = NULL;
return s;
}
int isEmpty(pStack stack) {
if (stack->top == -1) {
return 1;
}
else {
return 0;
}
}
int isFull(pStack stack) {
if (stack->top == maxsize - 1) {
return 1;
}
else {
return 0;
}
}
int Push(pStack* s, pTree a) {
if (isFull(*s)) {
return -1;//异常返回-1
}
else {
(*s)->stelem[++((*s)->top)] = a;
return 1;
}
}
int Pop(pStack* s, pTree* a) {
if (isEmpty(*s)) {
return -1;
}
else {
*a = (*s)->stelem[((*s)->top)--];
return 1;
}
}
void preorder(pTree* t) {
int h=0;//深度
pStack s = initStack();
pTree p = (*t);
while (p != NULL||s->top!=-1) {
while (p != NULL) {
//访问根节点
if (p->elem != NULL) {
printf("%c ", p->elem->data);
printf("%d ", p->elem->count);
WPL = WPL + (s->top + 1) * (p->elem->count);
}
Push(&s, p);
p = p->leftChild;
//遍历左子树
}
//遍历右子树
if (s->top != -1) {
Pop(&s, &p);//pop会导致top改变,遍历右子树计算wpl出问题
//the problem is here
p = p->rightChild;
}
}
}
堆栈的预序遍历访问正确的子树会导致 s->top--,这会导致 wpl 的计算出现问题。 希望有个大佬来帮我!
堆栈的预序遍历访问正确的子树会导致 s->top--,这会导致 wpl 的计算出现问题。 如何保证节点高度不受影响?
答: 暂无答案
评论