生活随笔
收集整理的這篇文章主要介紹了
树的更多相关算法-3
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
此代碼包含了構建哈夫曼樹的代碼,在后面幾段
?
???int?Nodes(BTNode?*b)?{?????int?num1?=?0,?num2?=?0;?????if(b?==?NULL)??return?0;?????else?if(b->lchild?==?NULL?&&?b->rchild?==?NULL)?return?1;?????else??????{?????????num1?=?Nodes(b->lchild);????????????????num2?=?Nodes(b->rchild);???????????????return?(num1?+?num2?+?1);??????????}?}??void?LeafNodes(BTNode?*b,?int?&leafnum)???????{?????????????????????????????????????????????????if(b?==?NULL)?return?;?????else?if(b->lchild?==?NULL?&&?b->rchild??==?NULL)??++leafnum;?????else?????{?????????LeafNodes(b->lchild,leafnum);?????????LeafNodes(b->rchild,leafnum);?????}????}??int?Nodes2(BTNode?*b)???????????????{?????int?num1?=?0,?num2?=?0;?????if(b?==?NULL)??return?0;?????else?if(b->lchild?==?NULL?&&?b->rchild?==?NULL)?{??return?1;}?????else??????{?????????num1?=?Nodes2(b->lchild);????????????num2?=?Nodes2(b->rchild);????????????return?(num1?+?num2?);???????????}?}??void??DNodes(BTNode?*b,?int?&dnum)????????{?????if(b?!=?NULL)?????????????????{?????????if(b->lchild?!=?NULL?&&?b->rchild?!=?NULL)??????????????????++dnum;??????????????????????????????DNodes(b->lchild,dnum);????????????????????????????????DNodes(b->rchild,dnum);???????????????????????????}?}??int?DNodes2(BTNode?*b)???????????????????????{?????int?num1?=?0,?num2?=?0,?n?=?0;?????if(b?==?NULL)?return?0;?????else?if(b->lchild?==?NULL?||?b->rchild?==?NULL)?n?=?0;?????????????else?{?n?=?1;?}??????????num1?=?DNodes2(b->lchild);?????num2?=?DNodes2(b->rchild);?????return?(num2?+?num1?+?n);????????????????????????????}??void??DNodes3(BTNode?*b,?int?&dnum)??????????{?????int?num1?=?0,?num2?=?0;?????if(b?!=?NULL)???????????????????????????????{?????????if(b->rchild?==?NULL?||?b->lchild?==?NULL)????????????{?????????????DNodes3(b->lchild,?dnum);?????????????DNodes3(b->rchild,?dnum);?????????}else?????????{??????????????++dnum;???????????????????????????????????????????????DNodes3(b->lchild,?dnum);?????????????DNodes3(b->rchild,?dnum);?????????}?????}?}????void??DelTree(BTNode?*b)???{?????if(b?!=??NULL)?????{???DelTree(b->lchild);?????????DelTree(b->rchild);???????????????????free(b);?????}?}??void?Swap(BTNode?*&b)??????????{?????BTNode?*temp;?????if(b?!=?NULL)????????????????{?????????Swap(b->lchild);?????????Swap(b->rchild);?????????temp?=?b->lchild;????????????????????b->lchild?=?b->rchild;?????????b->rchild?=?temp;?????}?}????void?Swap1(BTNode?*b,?BTNode?*&b1)??????????????{?????if(b?==?NULL)?????{?????????b1?=?NULL;?????}?????else?????{?????????b1?=?(BTNode?*)malloc(sizeof(BTNode));?????????b1->data?=?b->data;?????????Swap1(b->lchild,?b1->rchild);?????????Swap1(b->rchild,?b1->lchild);?????}?}??void?ancestor(BTNode?*b,char?r,?char?s)????{????????????????????????????????????????????BTNode?*st[MAXSIZE],??*temp,?*temp2;??????int?top?=?-1;????????????????????????????bool??flag?=?true;?????char?path[MAXSIZE];??????????if(b?!=?NULL)????????????{?????????temp?=?b;?????????do???????????????{????????????????????while(temp?!=?NULL)?????????????????????{?????????????????++top;?????????????????st[top]?=?temp;?????????????????temp?=?temp->lchild;?????????????}?????????????flag?=?true;??????????????temp2?=?NULL;?????????????while(flag?&&?top?>?-1)??????????????{?????????????????temp?=?st[top];?????????????????if(temp->rchild?==?temp2)??????????????????????????{????????????????????????????????????????if(temp->data?==?r)??????????????????????????????{?????????????????????????for(int?loop1=0;?loop1?<=?top;?++loop1)?????????????????????????????path[loop1]?=?st[loop1]->data;?????????????????????????--top;????????????????????????????????????????????????temp2?=?temp;????????????????????????????????????}else?if(temp->data?==?s)?????????????????????{?????????????????????????int?loop2?=?0;?????????????????????????while(st[loop2]->data?==?path[loop2])?????????????????????????????++loop2;?????????????????????????cout<<r<<"?與?"<<s<<"?,?最近的共同祖先是?:"<<path[--loop2]<<endl;?????????????????????????return?;?????????????????????}else?????????????????????{?????????????????????????--top;?????????????????????????temp2?=?temp;?????????????????????}?????????????????}else?????????????????{?????????????????????flag?=?false;??????????????????????????????????????temp?=?temp->rchild;?????????????????}?????????????}?????????}while(top?>?-1);?????}?}?char?path1[MAXSIZE];?int?top?=?-1;?void?AncestorPath(BTNode?*b,?char?s)??????{????????????????????????????????????????????if(b?!=?NULL)?????{?????????++top;?????????path1[top]?=?b->data;???????????????if(b->data?==?s)????????????????????{??????????????cout<<"路徑為:"<<endl;?????????????for(int?loop1?=?0;?loop1?<=?top;?++loop1)?????????????????cout<<path1[loop1]<<'?';?????????????cout<<endl;?????????????return;?????????}?????????AncestorPath(b->lchild,s);?????????AncestorPath(b->rchild,s);?????????--top;??????????????????????}?}???void?AncestorPath2(BTNode?*b,?char?s)?????{?????BTNode?*st[MAXSIZE],?*temp,?*temp2;?????int?top?=?-1;?????bool?flag?=?true;?????char?path[MAXSIZE];??????if(b?!=?NULL)?????{?????????temp?=?b;?????????do?????????{?????????????while(temp?!=?NULL)?????????????{?????????????????++top;?????????????????st[top]?=?temp;?????????????????temp?=?temp->lchild;?????????????}?????????????temp2?=?NULL;?????????????flag?=?true;?????????????while(flag?&&?top?>?-1)?????????????{?????????????????temp?=?st[top];?????????????????if(temp->rchild?==?temp2)?????????????????{?????????????????????if(temp->data?==?s)?????????????????????{?????????????????????????cout<<"根節點到?"<<s<<"?之間的路徑為?:"<<endl;?????????????????????????for(int?loop1?=?0;?loop1?<=?top;?++loop1)?????????????????????????????cout<<st[loop1]->data<<'?';?????????????????????????cout<<endl;?????????????????????????return;?????????????????????}?????????????????????--top;?????????????????????temp2?=?temp;?????????????????}else?????????????????{?????????????????????flag?=?false;?????????????????????temp?=?temp->rchild;?????????????????}?????????????}??????????}while(top?>?-1);?????}?}?char?path3[MAXSIZE];?int?top2?=?-1;?void?Link(BTNode?*b)???????????{?????if(b?!=?NULL)?????{?????????if(b->lchild?==?NULL?&&?b->rchild?==?NULL)?????????????path3[++top2]?=?b->data;?????????else?????????{?????????????Link(b->lchild);?????????????Link(b->rchild);?????????}?????}?}??void?Link2(BTNode?*b)???????????{?????if(b?!=?NULL)?????{?????????Link2(b->lchild);?????????if(b->lchild?==?NULL?&&?b->rchild?==?NULL)?????????????path3[++top2]?=?b->data;?????????Link2(b->rchild);?????}?}??void?Print(BTNode?*b,int?w)??????????????{?????if(b?!=?NULL)?????{?????????Print(b->rchild,?w+5);?????????for(int?loop1?=?0;?loop1?<=?w;?++loop1)?????????????cout<<'?';?????????cout<<b->data<<endl;?????????Print(b->lchild,w+5);?????}?}??float?ExpValue(BTNode?*b)???????{?????if(b?!=?NULL)???????????????????{????????????????switch(b->data)?????????{?????????case?'+'?:?????????????return?ExpValue(b->lchild)?+?ExpValue(b->rchild);??break;?????????case?'-'?:?????????????return?ExpValue(b->lchild)?-?ExpValue(b->rchild);??break;?????????case?'*'?:?????????????return?ExpValue(b->lchild)?*?ExpValue(b->rchild);??break;?????????case?'/'?:?????????????return?ExpValue(b->lchild)?/?ExpValue(b->rchild);??break;?????????default?:?????????????return?b->data?-?'0';????????????????????}?????}?}??bool?bflag?=?false;???????????bool?bflag2?=?false;?????????void??InorderExp(BTNode?*b)????????{????????????????????????????????????if(b?!=?NULL)????????????????????{??????????????????switch(b->data)?????????{?????????case?'+'?:?????????case?'-'?:?????????????InorderExp(b->lchild);?????????????????????cout<<b->data<<'?';??????????????????????InorderExp(b->rchild);?break;???????????case?'*'?:?????????case?'/'?:????????????????????????????if(b->lchild?!=?NULL)????????????????????{?????????????????if(b->lchild->data?==?'+'?||?b->lchild->data?==?'-')?????????????????{???cout<<'(';?????????????????????bflag?=?true;?????????????????}?????????????}??????????????InorderExp(b->lchild);??????????????????????????if(bflag)?????????????{?cout<<')';??bflag?=?false;}??????????????????????cout<<b->data<<'?';???????????????????????????????????if(b->rchild?!=?NULL)????????????????????{?????????????????if(b->rchild->data?==?'+'?||?b->rchild->data?==?'-')?????????????????{???cout<<'(';???????????????????????????????????bflag2?=?true;?????????????????}?????????????}?????????????InorderExp(b->rchild);??????????????????????????if(bflag2)?????????????{?cout<<')';?bflag2?=?false;?}?????????????break;?????????default?:?????????????cout<<b->data<<'?';?????????????break;?????????}?????}????}??int?process(char?op1,?char?op2)??{?????if(op1?!=?'+'?&&?op1?!=?'-'?&&?op1?!=?'*'?&&?op1?!=?'/')??return?-1;??????if(op2?!=?'+'?&&?op2?!=?'-'?&&?op2?!=?'*'?&&?op2?!=?'/')??return?-1;??????if(op1?==?'+'?||?op1?==?'-')?????{?????????return?0;?????}?????if(op1?==?'*'?||?op1?==?'/')?????{?????????if(op2?==?'+'?||?op2?==?'-')?????????????return?1;?????}?}??void?InorderExp2(BTNode?*b)?????{?????int?flag1?=?2;?????int?flag2?=?2;?????if(b?!=?NULL)?????{?????????if(b->lchild?!=?NULL)?????????{?????????????flag?=?process(b->data,b->lchild->data);?????????????if(flag?==?1)?cout<<'(';?????????????InorderExp(b->lchild);?????????????if(flag?==?1)?cout<<')';?????????}?????????cout<<b->data;?????????if(b->rchild?!=?NULL)?????????{?????????????flag2?=?process(b->data,b->rchild->data);?????????????if(flag2?==?1)?cout<<'(';?????????????InorderExp(b->rchild);?????????????if(flag2?==?1)?cout<<')';?????????}?????}?}??float?value[MAXSIZE];??int?top22?=?-1;?char?postExp[MAXSIZE];?void?PostExp(BTNode?*b)????{????????????????????????????if(b?!=?NULL)?????{?????????PostExp(b->lchild);?????????PostExp(b->rchild);?????????++top22;?????????postExp[top22]?=?b->data;?????}?}??void?CompValue(char?postExp[])??{???top?=?-1;?????char?ch;?????int?loop1?=?0;?????ch?=?postExp[loop1];?????float?lValue?=?0.0f,?rValue?=?0.0f,?fValue?=?0.0f;?????while(ch?!=?'\0')?????{?????????switch(ch)?????????{?????????case?'+'?:?????????????rValue?=?value[top];?????????????--top;?????????????lValue?=?value[top];?????????????fValue?=?lValue?+?rValue;?????????????value[top]?=?fValue;?????????????ch?=?postExp[++loop1];?????????????break;?????????case?'-'?:?????????????rValue?=?value[top];?????????????--top;?????????????lValue?=?value[top];?????????????fValue?=?lValue?-?rValue;?????????????value[top]?=?fValue;?????????????ch?=?postExp[++loop1];?????????????break;?????????case?'*'?:?????????????rValue?=?value[top];?????????????--top;?????????????lValue?=?value[top];?????????????fValue?=?lValue?*?rValue;?????????????value[top]?=?fValue;?????????????ch?=?postExp[++loop1];?????????????break;?????????case?'/'?:?????????????rValue?=?value[top];?????????????--top;?????????????lValue?=?value[top];?????????????if(rValue?==?0)??{?cout<<"發生除零錯誤。"<<endl;?exit(1);?}??????????????fValue?=?lValue?/?rValue;?????????????value[top]?=?fValue;?????????????ch?=?postExp[++loop1];?????????????break;?????????default?:?????????????fValue?=?float(?ch?-?'0');??????????????????++top;?????????????value[top]?=?fValue;?????????????ch?=?postExp[++loop1];?????????????break;?????????}?????}?????if(top?==?0)?????{?cout<<"所求的值為:"<<value[top]<<endl;?}?????else?????{?cout<<"求值錯誤。"<<endl;?}?}??void?NodeToRoot(BTNode?*b)?????{????????????????????????????????BTNode?*temp;?????if(b?!=?NULL)?????{?????????temp?=?b;?????????while(temp?!=?NULL)?????????{?????????????cout<<temp->data<<'?';?????????????temp?=?temp->parent;?????????}?????????cout<<endl;?????????NodeToRoot(b->lchild);?????????NodeToRoot(b->rchild);?????}?}??void?PreToPost(ElemType?pre[],?int?begin1,?int?end1,?ElemType?post[],?int?begin2,?int?end2)?{????????????????????????????????????int?half?=?0;??????if(end1?>=?begin1)?????{???half?=?(end1?-?begin1?)?/?2;????????????????post[end2]?=?pre[begin1];??????????????PreToPost(pre,?begin1?+?1,?begin1?+?half,?post,?begin2,begin2?+?half?-1);??????????PreToPost(pre,?begin1?+?half?+?1,?end1,?post,begin2?+?half,?end2?-1);??????????}???}??void?CreateHT(HTNode?ht[],?int?n)?????{?????int?loop1?=?0,?loop2?=?0,?loop3?=?0,?lpos?=?0,?rpos?=?0;?????float?weight1?=?0.0f,?weight2?=?0.0f;????????????for(loop1?=0;?loop1?<?n;?++loop1)?????????{???ht[loop1].parent?=?-1;?????????ht[loop1].lchild?=?-1;?????????ht[loop1].rchild?=?-1;?????}??????for(loop1?=?n;?loop1?<?2?*?n?+?1;?++loop1)??????????{?????????weight1?=?32767.0f;?????????weight2?=?32767.0f;??????????for(loop2?=?0;?loop2?<?loop1;?++loop2?)?????????{?????????????if(ht[loop2].parent?==?-1)??????????????????????{?????????????????if(ht[loop2].weight?<?weight1)???????????????????????{?????????????????????weight2?=?weight1;?????rpos?=?lpos;?????????????????????weight1?=?ht[loop2].weight;??lpos?=?loop2;?????????????????}?????????????????else?if(ht[loop2].weight?<?weight2)?????????????????{?????????????????????weight2?=?ht[loop2].weight;?????????????????????rpos?=?loop2;?????????????????}?????????????}?????????}?????????ht[loop1].lchild?=?lpos;????ht[loop1].rchild?=?rpos;??ht[loop1].weight?=?ht[lpos].weight?+?ht[rpos].weight;?????????ht[lpos].parent?=?loop1;????ht[rpos].parent?=?loop1;?????}?}??typedef?struct?{?????char?cd[MAXSIZE];??????int?start;?????????}HCode;????????????????????????????typedef?char?ElemType;?typedef?struct?hnode?{?????int?weight;?????ElemType?data;?????struct?hnode?*lchild,?*rchild;?}HTree;??typedef?struct??{?????ElemType?data;?????int?weight;?}Node;?typedef?struct??{?????char?code[10];?????int?weight;?????ElemType?data;?}NodeCode;??struct?cmp1?{?????bool?operator()?(HTree?*node1,?HTree?*node2)?????{?????????return?node1->weight?>=?node2->weight;?????}?};??void?CreateHuffm(HTree?*&root,?Node?nodes[],?int?num)??{?????int?loop1?=?0,?weight?=?0;?????HTree?*temp?=?NULL,*lchild?=?NULL,?*rchild?=?NULL;?????priority_queue<HTree?*,?vector<HTree?*>,?cmp1>??qp;??????for(loop1?=?0;?loop1?<?num;?++loop1)?????{?????????temp?=?(HTree?*)malloc(sizeof(HTree));?????????temp->lchild?=?temp->rchild?=?NULL;?????????temp->data?=?nodes[loop1].data;?????????temp->weight?=?nodes[loop1].weight;?????????qp.push(temp);?????}?????while(qp.size()?!=?1)?????{?????????lchild?=?qp.top();?????????qp.pop();?????????rchild?=?qp.top();?????????qp.pop();??????????????????temp?=?(HTree?*)malloc(sizeof(HTree));?????????temp->lchild?=?lchild;?????????temp->rchild?=?rchild;?????????temp->weight?=?lchild->weight?+?rchild->weight;??????????????????qp.push(temp);???????????}?????root?=?qp.top();?????qp.pop();????}??void?disp(HTree?*tree)?{?????if(tree?!=?NULL)?????{?????????cout<<tree->weight<<endl;?????????disp(tree->lchild);?????????disp(tree->rchild);?????}?}???NodeCode?code[MAXSIZE];?int?loop?=?0,?looop?=?0;?char?pathx[10];??void?HuffmCode(HTree?*tree)???????{?????if(tree?!=?NULL)?????{?????????if(tree->lchild?==?NULL?&&?tree->rchild?==?NULL)?????????{?????????????code[looop].data?=?tree->data;?????????????code[looop].weight?=?tree->weight;?????????????strcpy(code[looop].code,?pathx);?????????????????????????++looop;?????????}?????????if(tree->lchild?!=?NULL)?????????{?????????????pathx[loop]?=?'0';?????????????++loop;?????????????pathx[loop]?=?'\0';??????????????????????????????HuffmCode(tree->lchild);?????????}?????????if(tree->rchild?!=?NULL)?????????{?????????????pathx[loop]?=?'1';?????????????++loop;?????????????pathx[loop]?=?'\0';?????????????HuffmCode(tree->rchild);?????????}?????????--loop;?????????pathx[loop]?=?'\0';?????}?}? ?
轉載于:https://blog.51cto.com/saibro/1183621
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的树的更多相关算法-3的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。