要编一个前中后的遍历函数加进去构成程序,请问该怎么做?

#include<iostream.h>
#include<stdio.h>

struct tree {
char info;
struct tree *left,*right;
};

struct tree *create_btree(struct tree *root,struct tree *r,char info);
struct tree *search_btree(struct tree *root,char key);
void print_btree(struct tree *r,int l);

void main ()
{ char s[100],c,key=' ' ;
struct tree *root=0 ;
do {
printf("Enter a letter:");
gets(s);
if (!root)
root=create_btree(root,root,*s);
else
create_btree(root,root,*s);
} while (*s) ;
print_btree(root,0);
key='1';
while ( key)
{
printf("Enter a key to find:");
scanf("%s",&c);
root=search_btree(root,c);
printf("press to continue\n");
}
} /* Btree.C 结束 */

struct tree *create_btree(struct tree *root,struct tree *r,char info)
{ if (r ==0 )
{ r=new (struct tree);// same as function: malloc(sizeof())
if ( r == 0)
{ printf("Out of memory\n"); return 0 ; }
r->left= 0; r->right=0; r->info=info;
if (root)
{ if(info<root->info) root -> left=r;
else root -> right=r;
}
else
{ r->right=0; r->left = 0; }
return r;
} /* if = = 0 接下页 */
if (info < r->info)
create_btree(r,r->left,info);
if(info>=r->info)
create_btree(r,r->right,info);
} /* create_btree(root,r,info) */

struct tree *search_btree(struct tree *root,char key)
{ if (!root)
{ printf("Emptu btree\n"); return root; }
while(root->info!=key)
{ if(key<root->info)
root=root->left;
else
root=root->right;
if(root==0)
{ printf("Search Failure\n");
break ;
}
} /* while(root->info!=key) */
if (root !=0)
printf("Successful search\n key=%c\n",root->info);
return root ;
} /* *search_btree(root,key) */

void print_btree(struct tree *r,int l)

{ int i;
if (r == 0) return ;
print_btree(r->left,l+1);
for(i=0;i<l;i++)
printf(" ");
printf("%c\n",r->info);
print_btree(r->right,l+1);
}

胡子哥哥
浏览 112回答 2
2回答

白衣非少年

这是我写的一个关于二叉树的查找遍历的程序,你可以参考一下#include<iostream>using namespace std;#include<stdio.h>#include<stdlib.h>#define TURE 1#define FALSE 0#define OK 1#define ERROR 0#define OVERFLOW -2typedef char TElemType;typedef int Status;typedef struct BiTNode{TElemType data;struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;void CreateBiTree(BiTree &T){TElemType c;cin>>c;if(c=='#') T=NULL;else{T=new BiTNode;T->data=c;CreateBiTree(T->lchild);CreateBiTree(T->rchild);}}Status visitT(TElemType e){ int c;printf("%c",c);return OK;}Status PreOrderTraverse(BiTree T){if(T){cout<<T->data;PreOrderTraverse(T->lchild);PreOrderTraverse(T->rchild);return OK;}elsereturn ERROR;}Status InOrderTraverse(BiTree T){if(T){InOrderTraverse(T->lchild);cout<<T->data;InOrderTraverse(T->rchild);return OK;}elsereturn ERROR;}Status PostOrderTraverse(BiTree T){if(T){PostOrderTraverse(T->lchild);PostOrderTraverse(T->rchild);cout<<T->data;return OK;}elsereturn ERROR;}int NodeCount(BiTree T ){if(T==NULL)return 0; // 如果是空树,则结点个数为0elsereturn(1+NodeCount(T->lchild)+ NodeCount(T->rchild));//否则结点个数为1+左子树的结点个数+右子树的结点个数}int LeafNodeCount(BiTree T ){if(T==NULL) //如果是空树,则叶子个数为0;return 0;else{if(NodeCount(T)==1)return 1; //如果是叶子结点,则叶子结点个数为1(如何表示叶子结点???)elsereturn (LeafNodeCount(T->lchild)+LeafNodeCount(T->rchild));//否则叶结点个数为左子树的叶结点个数+右子树的叶结点个数}}int Depth(BiTree T){int m,n;if(T==NULL) //如果是空树,则深度为0;return 0;else{ //否则m=Depth(T->lchild); //(1) 计算左子树的深度记为m;n=Depth(T->rchild); //(2) 计算右左子树的深度记为n;if(m>n)return(m+1); //二叉树的深度为m 与n的较大者加1elsereturn(n+1);}}int exchange(BiTree T){struct BiTNode *t;if(T==NULL) return 0;else{t=T->lchild;if(T->lchild!=NULL)exchange(T->lchild);if(T->rchild!=NULL)exchange(T->rchild);T->lchild=T->rchild;T->rchild=t;return OK;}}void main(){BiTree T;cout<<"请按先序遍历的顺序输入一棵二叉树:"<<endl;CreateBiTree(T);cout<<"先序遍历的结果为:";PreOrderTraverse(T);cout<<endl;cout<<"中序遍历的结果为:";InOrderTraverse(T);cout<<endl;cout<<"后序遍历的结果为:";PostOrderTraverse(T);cout<<endl;cout<<"结点数为:";cout<<NodeCount(T)<<endl;cout<<"叶子结点数为:";cout<<LeafNodeCount(T)<<endl;cout<<"树的深度为:";cout<<Depth(T)<<endl;exchange(T);cout<<"左右孩子交换后的结果--先序遍历为:";PreOrderTraverse(T);cout<<endl;cout<<"左右孩子交换后的结果--中序遍历为:";InOrderTraverse(T);cout<<endl;cout<<"左右孩子交换后的结果--后序遍历为:";PostOrderTraverse(T);cout<<endl;}

郎朗坤

#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef struct shu{struct shu *lp,*rp;char ch;}shu,*bishu,;////////////////////////创建///////////////////////////////////int creat(bishu &t){char ch;ch=getchar();if(ch==' ')t=NULL;else{if(!(t=(struct shu *)malloc(sizeof(struct shu))))exit(0);(*t).ch=ch;creat((*t).lp);creat((*t).rp);}return 1;}///////////////////////////////遍历(递归)////////////////////////int traverse(struct shu *t){if(t==NULL)return 1;printf("%c ",(*t).ch);traverse((*t).lp);traverse((*t).rp);return 1;}/////////////////////////////层序遍历////////////////////////////int ftraverse(struct shu *t){int i=1,length=1;shu tree[100];if(t==NULL)return 0;while(1){if(t!=NULL){printf("%c ",(*t).ch);i++;length--;tree[i+length].rp=(*t).lp;length++;tree[i+length].rp=(*t).rp;length++;t=tree[i].rp;}else{i++;t=tree[i].rp;length--;}if(t==NULL&&length!=0)return 0;;}}/////////////////////////////遍历(非递归)/////////////////////int mtraverse(struct shu *t){int i=0;shu tree[100];if(t==NULL)return 1;printf("%c ",(*t).ch);tree[i++].rp=(*t).rp;t=(*t).lp;while(i!=-1){if(t!=NULL){printf("%c ",(*t).ch);tree[i++].rp=(*t).rp;t=(*t).lp;}else{t=tree[--i].rp;}}return 0;}/////////////////////////主函数///////////////////////////////////int main(){bishu t;creat(t);mtraverse(t);printf("\n");ftraverse(t);printf("\n");traverse(t);system("pause");return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP