您好,欢迎访问三七文档
#includeiostreamusingnamespacestd;#includestdlib.h#includemath.h#definemaxsize100#includetree.h#defineLENsizeof(structbtree)intmax=1;typedefstructbtree//¶þ²æÊ÷½Úµã½á¹¹Ìå£ûbtree*lchild,*rchild;chardata;}*BiNode;typedefstructStackElemType//Õ»µÄ½á¹¹Ìå£ûBiNodeptr;intflag;}StackElemType;BiNodep;//¶þ²æÊ÷µÄ½¨Á¢BiNodestree_creat(char*a,intk){BiNoderoot;max++;if(a[k]=='\0'||kmaxsize)returnNULL;//ÅжÏÊ÷ÊÇ·ñΪ¿Õelse{root=£¨BiNode)malloc(LEN);//¶¯Ì¬ÉêÇë½Úµãroot-data=a[k];root-lchild=stree_creat(a,2*k+1);//µÝ¹éµ÷ÓÃΪ×óº¢×Ó¸³Öµroot-rchild=stree_creat(a,2*k+2);//µÝ¹éµ÷ÓÃΪÓÒº¢×Ó¸³Öµreturnroot;//·µ»Ø¸ù½ÚµãÖ¸Õë}}voidprint(BiNoderoot)//Êä³öËù½¨¶þ²æÊ÷{BiNodeh[maxsize]={NULL};inttop=0,base=0,j=0,k=0,n=0,m=0;h[top]=root;j=log(max+1)/log(2)-1;if(pow(2,j+1)-1max)j++;//coutÄã¸ÕÊäÈëµÄÊÇ:\n;while(h[base]!=NULL)//°Ñ¶þ²æÊ÷µÄÖµÒÀ´Î´æÈëÊý×é{h[++top]=h[base]-lchild;h[++top]=h[base]-rchild;base++;}for(top=0;h[k]!=NULL;top++)//°´²ãÊä³ö{m=pow(2,j)-top;//¼ÆËãÿÐÐÊä³öµÄ¿Õ¸ñÊýif(top!=0)m=m-top;for(n=0;nm;n++)cout;//for(base=0;basepow(2,top)&&h[k]!=NULL;base++)//¿ØÖÆÿ²ãÊä³öµÄ¸öÊý{if(h[k]-data=='0')cout[];//µ±Îª¿ÕʱÊä³ö[]elsecouth[k]-data;//k++;}cout\n;for(n=0;n(m-1);n++)cout;for(base=0;basepow(2,top)&&h[k]!=NULL;base++)cout/|;cout\n;}}//¶þ²æÊ÷µÄºóÐò±éÀúµÝ¹éËã·¨voidPostOrder(BiNoderoot){if(root==NULL)return;//µÝ¹éµ÷ÓõÄÔ¼ÊøÌõ¼þelse{PostOrder(root-lchild);PostOrder(root-rchild);if(root-data=='0');elsecout[root-data];}}//¶þ²æÊ÷µÄÖÐÐò±éÀúËã·¨voidInOrder(BiNoderoot){if(root==NULL)return;//µÝ¹éµ÷ÓõÄÔ¼ÊøÌõ¼þelse{InOrder(root-lchild);if(root-data=='0');elsecout[root-data];InOrder(root-rchild);}}//¶þ²æÊ÷µÄÇ°ÐòµÝ¹é±éÀúËã·¨voidPreOrder(BiNoderoot){if(root==NULL)return;//µÝ¹éµ÷ÓõÄÔ¼ÊøÌõ¼þelse{if(root-data=='0');elsecout[root-data];PreOrder(root-lchild);PreOrder(root-rchild);}}//¶þ²æÊ÷µÄÇ°Ðò±éÀú·ÇµÝ¹éËã·¨voidF_PreOrder(BiNoderoot){BiNodes[maxsize];//ÉêÇëÒ»¸öBiNodeÊý×éinttop=0;//²ÉÓÃ˳ÐòÕ»£¬²¢¼Ù¶¨²»»á·¢ÉúÉÏÒçdo{while(root!=NULL)//µ±½Úµã²»Îª¿Õʱ{if(root-data=='0');elsecout[root-data];s[++top]=root;//ÒÀ´Î½«½ÚµãѹÈëÕ»root=root-lchild;//¸ù¸³ÖµÎªËüµÄ×óº¢×Ó}if(top0)//µ±½ÚµãΪ¿Õµ«Õ»¶¥²»ÎªÁã{root=s[top--];Õ»¶¥ÏÂÒÆ°ÑÕ»¶¥ÔªËظº¸ø¸ù½Úµãroot=root-rchild;¸ù¸³ÖµÎªËüµÄÓÒº¢×Ó}}while(root!=NULL||top0);}//ÖÐÐò·ÇµÝ¹é±éÀúËã·¨voidF_InOrder(BiNoderoot){BiNodes[maxsize];//ÉêÇëÒ»¸öBiNodeÊý×éinttop=0;//²ÉÓÃ˳ÐòÕ»£¬²¢¼Ù¶¨²»»á·¢ÉúÉÏÒçdo{while(root!=NULL)//µ±½Úµã²»Îª¿Õʱ{s[++top]=root;//ÒÀ´Î½«½ÚµãѹÈëÕ»root=root-lchild;//¸ù¸³ÖµÎªËüµÄ×óº¢×Ó}if(top0)//µ±½ÚµãΪ¿Õµ«Õ»¶¥²»ÎªÁãʱ{root=s[top];if(root-data=='0');elsecout[root-data];root=s[top--];root=root-rchild;}}while(root!=NULL||top0);}//ºóÐò·ÇµÝ¹é±éÀúËã·¨voidF_PostOrder(BiNoderoot){StackElemTypes[maxsize];BiNodep=root;inttop=0;do{while(p!=NULL){s[top].flag=0;s[top].ptr=p;p=p-lchild;top++;}while(s[top-1].flag==1{if(s[top-1].ptr-data=='0')top--;elsecout[s[--top].ptr-data];}if£¨top0){s[top-1].flag=1;p=s[top-1].ptr;p=p-rchild;}}while(top0);}//²ã´ÎÐòÁбéÀúËã·¨voidLeveOrder(BiNoderoot){BiNodes[maxsize];intmax,i=0;s[0]=root;while(root!=NULL){s[2*i+1]=root-lchild;s[2*i+2]=root-rchild;i++;root=s[i];max=i;}for(i=0;imax;i++){if(s[i]-data=='0')elsecout[s[i]-data];}}voidPause(){coutendlendlendl;system(pause);coutendlendlendl;}//Ê÷ת»»Îª¶þ²æÊ÷voidexchange(){TreecharmyTree1;myTree1.InputTree();Pause();myTree1.showTree();Pause();myTree1.show_BinaryTree();Pause();}//ÊäÈë¶þ²æÊ÷µÄÐÅÏ¢µÄº¯ÊýBiNodecreat(){BiNodep=NULL;coutÇëÊäÈëÄãµÄ¶þ²æÊ÷£¨Çë°´²ã´ÎÓÉÉÏÍùÏ´Ó×óÍùÓÒÒÀ´ÎÊäÈë²¢°´#½áÊø£¬Èç¹û½ÚµãΪ¿ÕÇëÊäÈë'0',±¾±éÀú½öÖ§³Öµ¥×Ö·û£©£¬ÊäÈëΪ:\n;inti=0,j=0;charb[maxsize]={'#'},n;//µ±ÊäÈ벻Ϊ#ʱ¸øÊý×鸳ֵdo{cinn;if(n!='#')b[i]=n;i++;}while(n!='#');//p=stree_creat(b,0);print(p);//returnp;}intmain(){intk;cout\n;cout\n_____________________________________________________________________;cout|»¶ÓʹÓÃÊ÷µÄ¶àÖÖ±éÀú|;cout|Ê÷Óë¶þ²æÊ÷µÄ½»»»|;cout|___________________________________________________________________\n;do{cout\n;cout\n1.´´½¨¶þ²æÊ÷;cout\n2.Ç°ÐòµÝ¹é±éÀúËã·¨;cout\n3.ÖÐÐòµÝ¹é±éÀúËã·¨;cout\n4.ºóÐòµÝ¹é±éÀúËã·¨;cout\n5.Ç°Ðò·ÇµÝ¹é±éÀúËã·¨;cout\n6.ÖÐÐò·ÇµÝ¹é±éÀúËã·¨;cout\n7.ºóÐò·ÇµÝ¹é±éÀúËã·¨;cout\n8.²ãÐò·ÇµÝ¹é±éÀúËã·¨;cout\n9.Ê÷Óë¶þ²æÊ÷µÄת»»;cout\n10.Í˳ö²Ù×÷\ncout\nÇëÑ¡ÔñÄãÒª½øÐеIJÙ×÷£¨Êý×Ö¼ü£©£º;cink;switch(k){case1:{p=creat();//µ÷ÓÃcreat£¨£©´´½¨¶þ²æÊ÷}break;case2:{cout¶þ²æÊ÷µÄÇ°ÐòµÝ¹é±éÀú£º;PreOrder(p);//}break;case3{cout¶þ²æÊ÷ÖÐÐòµÝ¹é±éÀú£º;InOrder(p);}break;case4{cout¶þ²æÊ÷ºóÐòµÝ¹é±éÀú£º;PostOrder(p);}break;case5{cout¶þ²æÊ÷Ç°Ðò·ÇµÝ¹é±éÀú£º;F_PreOrder(p);}break;case6{cout¶þ²æÊ÷ÖÐÐò·ÇµÝ¹é±éÀú£º;F_inOrder(p);}break;case7{cout¶þ²æÊ÷ºóÐò·ÇµÝ¹é±éÀú£º;F_PostOrder(p);}break;case8{cout¶þ²æÊ÷²ãÐò·ÇµÝ¹é±éÀú£º;LeveOrder(p);}break;case9:{exchange();deletep;returnmain();}break;}while(k=0&&k9);return0;}
本文标题:二叉树的中序、前序、后序的递归、非递归遍历算法-层次序的非递归遍历算法的实现-应包含建树实现
链接地址:https://www.777doc.com/doc-2756936 .html