gdb程序调试入门

#include
#include
#include
using namespace std;
typedef struct BiTnode{
int data;
struct BiTnode * lchild =NULL;
struct BiTnode * rchild =NULL;
int type=1;//tkeep the record for depth
int level=0;
}BiTnode;

bool sortFunc(const BiTnode* a,const BiTnode * b){

return a->data<=b->data;//increasing
}

int getvalue(BiTnode * root){
if(root->lchild==NULL) return root->data*root->level;//叶子结点
else return getvalue(root->lchild)+getvalue(root->rchild);
}

void update(BiTnode * node){
if(nodeNULL) return;
else {
if(node->type
1) node->level+=1;
else {
update(node->lchild);
update(node->rchild);
}
}
}
int main(){

//read in the different data and output the wpl
int index=5;
//cin>>index;
int input[index]={1,2,2,5,9};
int result=0;
//使用vector来实现对元素的添加、删除与排序操作
//使用root来保存现在已经有的树根
//元素值为-100表示的是这个结点是中间结点而不是刚开始输入的叶子结点
vector<BiTnode*> store;

for(int i=0;i<index;i++){
int in=0;
//cin>>in;
BiTnode * node;
node->data=input[i];
store.push_back(node);
}
if(index<0) return 0;
if(index==1){ cout<<store[0]->data<<endl; return 0;}
//构造哈弗曼树
sort(store.begin(),store.end(),sortFunc);

while(store.size()>=2){
// 记录每一个叶子结点的高度
BiTnode * a=store[0];
BiTnode * b=store[1];
BiTnode * c;
c->data=a->data+b->data;
c->lchild=a;
c->rchild=b;
c->type=0;//这个表示是中间新生成的结点
//开始计算叶子结点的深度
update(a);
update(b);
store.erase(store.begin());
store.erase(store.begin());
//insert new node
store.insert(store.begin(),c);
sort(store.begin(),store.end(),sortFunc);
}
//数组访问越界,store 0前没有加上取址符号
BiTnode * root =store[0];
//开始遍历哈弗曼树并且获取到最后的结果 计算所有的叶子结点的value
int res=getvalue(root);
cout<<res<<endl;

return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值