📄 subject_63323.htm
字号:
<p>
序号:63323 发表者:fox_xu 发表日期:2003-12-02 22:43:06
<br>主题:[求助请哪位高手帮忙看一下程序,关于排序二叉树的
<br>内容:#include <stdio.h><BR>#include <malloc.h><BR>struct bsnode<BR>{<BR> int data;<BR> struct bsnode *lc;<BR> struct bsnode *rc;<BR>};<BR>//利用插入算法生成一棵二叉树<BR>struct bsnode *insert(struct bsnode *root,int j)<BR>{<BR> if (root == NULL)<BR> {<BR> root = (struct bsnode*) malloc (sizeof(struct bsnode));<BR> root ->lc = NULL;<BR> root ->rc = NULL;<BR> root ->data = j;<BR> return root;<BR> }<BR> if (j < root ->data)<BR> {<BR> insert2(root ->lc,j);<BR> }<BR> else if (root ->data < j)<BR> {<BR> insert2(root->rc,j);<BR> }<BR> else if (j == root->data)<BR> return root;<BR> return root;<BR>}<BR>struct bsnode *creat(void)<BR>{<BR> struct bsnode *root = NULL, *t;<BR> int k;<BR> printf("please input the root:");<BR> scanf("%d",&k);<BR> t = insert(root,k); <BR> while(k != -1)<BR> {<BR> insert(t,k);<BR> printf("please input the node:");<BR> scanf("%d",&k);<BR> }<BR> return t;<BR>}<BR><BR>//中序遍历<BR>void m_order(struct bsnode *root)<BR>{<BR> if (root != NULL)<BR> {<BR> m_order(root ->lc);<BR> printf("%d\n",root ->data);<BR> m_order(root ->rc);<BR> }<BR>}<BR>void main()<BR>{<BR> struct bsnode *tree;<BR> tree = creat();<BR> m_order(tree);<BR>}<BR><BR>这个程序我觉得没错,但是我输入节点数据后,利用中序遍历却只能得到根节点的值,百思不得其解,好像insert()没有起作用,请大家帮帮我呀!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:宝儿 回复日期:2003-12-03 15:15:56
<br>内容:以下是小弟大二时编的一个二叉排序树的建立及合并,可以供参考之用。不当之处往多多指教 !<BR><BR>#include<iostream.h><BR>#include<string.h><BR>int i=1,k=1;<BR>//----------------------------------BTree------------------------------<BR>typedef struct BBSNode{<BR> char data;<BR> struct BBSNode *lchild,*rchild;<BR>}BBSNode,*BTree;<BR>//----------------------------------Createline-------------------------<BR>BTree Createline()<BR>{<BR> BTree headl,P2;<BR> P2=new(struct BBSNode);<BR> headl=NULL;<BR> cout<<"●请输出要构造二叉排序树("<<i<<")的数据(以!结束):→";<BR> P2=new(struct BBSNode);<BR> cin>>P2->data;<BR> while(P2->data!='!')<BR> { P2->lchild=headl;<BR> P2->rchild=NULL;<BR> headl=P2;<BR> P2=new(struct BBSNode);<BR> cin>>P2->data;<BR> }<BR> return(headl);<BR>}<BR>//-----------------------------------Createtree_1----------------------<BR>void Createtree_1(BTree &h,BTree &T1,BTree &L1)<BR>{<BR> char t1[1];<BR> char l1[1];<BR> BTree T2,p;<BR> T2=new(struct BBSNode);<BR> p=new(struct BBSNode);<BR> T2=T1;<BR> p->data=L1->data;<BR> p->lchild=p->rchild=NULL;<BR> if(!T2)<BR> {<BR><BR> T2=p;<BR> if(L1->lchild)<BR> {T2=h;<BR> Createtree_1(h,T2,L1->lchild);<BR> }<BR> }<BR> else<BR> {<BR> t1[0]=T2->data;l1[0]=p->data;<BR> if(T2->data==p->data)<BR> {<BR> if(L1->lchild)<BR> {T2=h;<BR> Createtree_1(h,T2,L1->lchild);<BR> }<BR> }<BR> <BR> else<BR> <BR> if(strcmp(t1,l1)<0)<BR> {<BR> if(T2->rchild)<BR> {<BR> T2=T2->rchild;<BR> Createtree_1(h,T2,L1);<BR> }<BR> else<BR> {<BR> <BR> T2->rchild=p;<BR> if(L1->lchild)<BR> {<BR> T2=h;<BR> Createtree_1(h,T2,L1->lchild);<BR> }<BR> }<BR> } <BR> else<BR> if(T1->lchild)<BR> {<BR> T2=T2->lchild;<BR> Createtree_1(h,T2,L1);<BR> }<BR> else<BR> { T2->lchild=p;<BR> if(L1->lchild)<BR> {T2=h;<BR> Createtree_1(h,T2,L1->lchild);<BR> }<BR> }<BR> <BR> <BR> }<BR>}<BR><BR> <BR><BR><BR>//-----------------------------------Createtree------------------------<BR>BTree Createtree(BTree &head,BTree &L)<BR>{<BR> BTree T,p,L2;<BR> T=new(struct BBSNode);<BR> L2=new(struct BBSNode);<BR> L2=L;<BR> T=NULL;<BR> T=head;<BR> char t1[1];<BR> char l1[1];<BR> p=new(struct BBSNode);<BR> if(L!=NULL)<BR> {<BR> p->data=L->data;<BR> p->lchild=p->rchild=NULL;<BR> if(!head)<BR> {<BR> head=p;<BR> T=head;<BR> Createtree_1(head,T,L->lchild);<BR> }<BR> else<BR> {<BR> t1[0]=T->data;l1[0]=p->data;<BR> if(T->data==p->data)<BR> {<BR> if(L->lchild)<BR> {<BR> T=head;<BR> Createtree_1(head,T,L->lchild);<BR> }<BR> }<BR> else<BR> if(strcmp(t1,l1)<0)<BR> {<BR> if(T->rchild)<BR> {<BR> T=T->rchild;<BR> Createtree_1(head,T,L);<BR> }<BR> else<BR> {<BR> T->rchild=p;<BR> if(L->lchild)<BR> {<BR> T=head;<BR> Createtree_1(head,T,L->lchild);<BR> }<BR> }<BR> } <BR> else<BR> if(T->lchild)<BR> {<BR> T=T->lchild;<BR> Createtree_1(head,T,L);<BR> }<BR> else<BR> {<BR> T->lchild=p;<BR> if(L->lchild)<BR> {<BR> T=head;<BR> Createtree_1(head,T,L->lchild);<BR> }<BR> }<BR> }<BR> }<BR><BR> return(head);<BR>}<BR><BR>//------------------------------------Disptree-----------------------------<BR>void Disptree(BTree T)<BR>{<BR> BTree T1;<BR> T1=new(struct BBSNode);<BR> T1=T;<BR> if(T1)<BR> {<BR> Disptree(T1->lchild);<BR> cout<<T1->data<<"→";<BR> Disptree(T1->rchild);<BR> }<BR>}<BR>void Disptree_1(BTree T)<BR>{<BR> BTree T1;<BR> T1=new(struct BBSNode);<BR> T1=T;<BR> if(T1)<BR> {<BR> cout<<T1->data<<"→";<BR> Disptree(T1->lchild);<BR> Disptree(T1->rchild);<BR> }<BR>}<BR>void Displine(BTree &L1)<BR>{<BR> BTree L2;<BR> for(L2=L1;L2;L2=L2->lchild)<BR> cout<<L2->data<<"→";<BR>}<BR><BR>//------------------------------------main---------------------------------<BR>void main()<BR>{<BR>cout<<"〓〓〓〓〓〓〓"<<endl;<BR>cout<<"〓二叉排序树〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓";<BR>cout<<"〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓二叉排序树的建立及合并〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓";<BR><BR>cout<<" ! !"<<endl;<BR>cout<<" ! !"<<endl;<BR>cout<<" ! !"<<endl;<BR>cout<<" ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !"<<endl;<BR>cout<<" !! !!! !!"<<endl; <BR>cout<<" !=! !!!!!!! !=!"<<endl;<BR>cout<<" !=! !!!!!!! !=!"<<endl;<BR>cout<<" !=! !!! !=!"<<endl;<BR>cout<<" !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;<BR>cout<<" ! !"<<endl<<endl;<BR>cout<<" ! ! ! ! ! ! ! ! ! ! ! ! "<<endl;<BR>cout<<" ! -\\-/--- ! ! --|-- ! "<<endl;<BR>cout<<" ! \\ -|- ! ----+---- !"<<endl;<BR>cout<<" ! / )--- ! ! / \\ !"<<endl;<BR>cout<<" ! ! ! ! ! ! ! ! ! ! ! !"<<endl<<endl;<BR>cout<<" ! ! ! !"<<endl<<endl;<BR>loop2: cout<<endl;<BR> cout<<"〓〓〓〓〓〓〓〓"<<endl;cout<<"〓 第"<<k<<"组数据 〓"<<endl;<BR> cout<<"〓〓〓〓〓〓〓〓"<<endl;<BR> BTree head1,L1,head2,L2,head3;<BR> i=1;<BR> k++; <BR> head1=new(struct BBSNode);<BR> L1=new(struct BBSNode);<BR> head2=new(struct BBSNode);<BR> L2=new(struct BBSNode);<BR> head3=new(struct BBSNode);<BR> head1=NULL;<BR> head2=NULL;<BR> head3=NULL;<BR> L1=Createline();//建立二叉排序树1<BR> cout<<">>输出要构造二叉排序树("<<i<<")的数据:→"<<endl<<"★→";<BR> Displine(L1);<BR> cout<<endl<<endl;<BR> head1=Createtree(head1,L1);<BR> cout<<">>排序二叉树("<<i<<")(中序遍历):→"<<endl<<"★→";<BR> Disptree(head1);<BR> cout<<"(累^.^了)"<<endl<<endl;<BR> cout<<">>排序二叉树("<<i<<")(前序遍历):→"<<endl<<"★→";<BR> Disptree_1(head1);<BR> cout<<"(休~.~息)"<<endl<<endl<<endl;<BR> i++;<BR> L2=Createline();//建立二叉排序树2<BR> cout<<">>输出要构造二叉排序树("<<i<<")的数据:→"<<endl<<"★→";<BR> Displine(L2);<BR> cout<<"^"<<endl<<endl;<BR> head2=Createtree(head2,L2);<BR> cout<<">>排序二叉树("<<i<<")(中序遍历):→"<<endl<<"★→";<BR> Disptree(head2);<BR> cout<<"(累^.^了)"<<endl<<endl;<BR> cout<<">>排序二叉树("<<i<<")(前序遍历):→"<<endl<<"★→";<BR> Disptree_1(head2);<BR> cout<<"(休~.~息)"<<endl<<endl<<endl;<BR> cout<<"●合并二叉排序树得新的排序二叉树●"<<endl;<BR> head3=Createtree(head2,L1);<BR> cout<<endl;<BR> cout<<">>合并后的排序二叉树(中序遍历):→"<<endl<<"★→";<BR> Disptree(head3);//合并二叉排序树1和2<BR> cout<<"(累^.^了)"<<endl<<endl;<BR> cout<<">>合并后的排序二叉树(前序遍历):→"<<endl<<"★→";<BR> Disptree_1(head3);<BR> cout<<"(完$_$了)"<<endl<<endl;<BR> cout<<"▲▲▲▲▲▲▲▲▲▲▲"<<endl;<BR> cout<<"▲▲▲要再接再厉▲▲▲"<<endl;<BR> cout<<"▲▲▲▲▲▲▲▲▲▲▲"<<endl<<endl;<BR> loop:char yono;<BR> cout<<">>是否继续运行?(是的话输入y,否则输入n):→";<BR> cin>>yono;<BR> if(yono=='y'||yono=='n')<BR> {<BR> if(yono=='y')<BR> goto loop2;<BR> else<BR> return;<BR> <BR> }<BR> else<BR> goto loop;<BR><BR> <BR>}
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -