信管家源代码c语言,用队列实现按层次创建二叉树的源代码,最好是C语言
滿意答案
Dcool
2016.08.27
采納率:58%????等級:9
已幫助:416人
隊列??你每輸入一個節(jié)點將其存入隊列中,再輸入它的左孩子,它的左孩子也會入隊,我們取的時候應先取該節(jié)點的左孩子,
LZ一定要用隊列也行,但絕對不是正確的選擇!
隊列如下:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990#include using namespace std;typedef struct bitnode{????char data;????struct bitnode *lchild,*rchild;}*bitree,tree;int number=0;void createbitree(bitree &t){????char c;????int i=0,r=0,f=0;//r,f分別指向隊首和隊尾????bitree p=NULL,temp=NULL,pre=NULL,s[100];????s[0]=NULL;????int lflag[100]={0};????int rflag[100]={0};????printf("請輸入根節(jié)點:");????t=new tree;????t->lchild=t->rchild=NULL;????scanf("%c",&t->data);????temp=pre=t->lchild;????s[++i]=t;????f=i;????p = t;????while(f!=r)????{????????if(p->lchild==NULL&&lflag[i]==0)????????{????????????printf("請輸入%c的左孩子:",p->data);????????????fflush(stdin);????????????scanf("%c",&c);????????????if(c!='#')????????????{????????????????p->lchild = new tree;????????????????p = p->lchild;????????????????p->lchild=p->rchild=NULL;????????????????p->data = c;????????????????s[++f]=p;????????????????i = f;????????????????lflag[i]=rflag[i]=0;????????????}????????????else????????????????lflag[i]=1;????????}????????else if(p->rchild==NULL&&rflag[i]==0)????????{????????????printf("請輸入%c的右孩子:",p->data);????????????fflush(stdin);????????????scanf("%c",&c);????????????if(c!='#')????????????{????????????????p->rchild = new tree;????????????????p = p->rchild;????????????????p->lchild=p->rchild=NULL;????????????????p->data = c;????????????????s[++f]=p;????????????????i=f;????????????????lflag[i]=rflag[i]=0;????????????}????????????else????????????{????????????????rflag[i]=1;????????????????p=s[++r];????????????????i=r;????????????}????????}????????else????????{????????????p=s[++r];????????????i=r;????????}????}}void preorder(bitree &t)//遍歷二叉樹,輸出函數(shù){????if (t!=0)????{????????cout<data<lchild);????????preorder(t->rchild);????}}void main(){????bitree t;????t=0;????createbitree(t);????cout<
在此,強烈建議LZ用棧,更符合樹的輸入層次:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283#include using namespace std;typedef struct bitnode{????char data;????struct bitnode *lchild,*rchild;}*bitree,tree;int number=0;void createbitree(bitree &t){????char c;????int i=0;????bitree p=NULL,temp=NULL,pre=NULL,s[100];????s[0]=NULL;????int lflag[100]={0};????int rflag[100]={0};????printf("請輸入根節(jié)點:");????t=new tree;????t->lchild=t->rchild=NULL;????scanf("%c",&t->data);????temp=pre=t->lchild;????s[++i]=t;????p = t;????while(s[i]!=NULL)????{????????if(p->lchild==NULL&&lflag[i]==0)????????{????????????printf("請輸入%c的左孩子:",p->data);????????????fflush(stdin);????????????scanf("%c",&c);????????????if(c!='#')????????????{????????????????p->lchild = new tree;????????????????p = p->lchild;????????????????p->lchild=p->rchild=NULL;????????????????p->data = c;????????????????s[++i]=p;????????????????lflag[i]=rflag[i]=0;????????????}????????????else????????????????lflag[i]=1;????????}????????else if(p->rchild==NULL&&rflag[i]==0)????????{????????????printf("請輸入%c的右孩子:",p->data);????????????fflush(stdin);????????????scanf("%c",&c);????????????if(c!='#')????????????{????????????????p->rchild = new tree;????????????????p = p->rchild;????????????????p->lchild=p->rchild=NULL;????????????????p->data = c;????????????????s[++i]=p;????????????????lflag[i]=rflag[i]=0;????????????}????????????else????????????{????????????????rflag[i]=1;????????????????p=s[--i];????????????}????????}????????else????????????p=s[--i];????}}void preorder(bitree &t)//遍歷二叉樹,輸出函數(shù){????if (t!=0)????{????????cout<data<lchild);????????preorder(t->rchild);????}}void main(){????bitree t;????t=0;????createbitree(t);????cout<
不懂追問!你的疑問往往是我要學習的地方!追問: 附件失效了
追答:逗……服了百度……老是出事
不行啊,傳不上去,
你QQ多少……我發(fā)郵件
01分享舉報
總結
以上是生活随笔為你收集整理的信管家源代码c语言,用队列实现按层次创建二叉树的源代码,最好是C语言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在c语言中,可以使用动态内存分配技术定义
- 下一篇: android 广告弹出层,安卓广告活动