构造一棵二叉树,并分别输出其先序遍历、中序遍历和后序遍历的结果 已知一棵二叉树如下图所示:分别写出该二叉树的先序遍历结果、中...
\u5efa\u7acb\u4e00\u68f5\u4e8c\u53c9\u6811\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u904d\u5386\uff08\u5148\u5e8f\u3001\u4e2d\u5e8f\u3001\u540e\u5e8f\uff09\uff0c\u6253\u5370\u8f93\u51fa\u904d\u5386\u7ed3\u679c//\u53ea\u6709\u5148\u5e8f\u904d\u5386\uff0c\u5176\u5b83\u7684\u53ef\u4ee5\u5728\u8fd9\u4e2a\u57fa\u7840\u4e0a\u6539\u3002
//\u5982\u679c\u6709\u4e0d\u61c2\u7684\u53ef\u4ee5hi\u6211
#include
#include
typedef struct tnode
{
char data;
struct tnode *lchild;
struct tnode *rchild;
}tnode;
tnode *Tree_creat(tnode *t)
{
char ch;
ch=getchar();
if(ch==' ')
t=NULL;
else
{
if(!(t=(tnode *)malloc(sizeof(tnode))))
printf("Error!");
t->data=ch;//printf("[%c]",t->data);
t->lchild=Tree_creat(t->lchild);
t->rchild=Tree_creat(t->rchild);
}
return t;
}
void preorder(tnode *t)
{
if(t!=NULL)
{
printf("%c ",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}
void main()
{
tnode *t=NULL;
t=Tree_creat(t);
preorder(t);
}
\u5148\u5e8f\u904d\u5386 \u5148\u6839\u540e\u5de6\u518d\u53f3
ABCDEF
\u540e\u5e8f\u904d\u5386 \u5148\u5de6\u53f3\u540e\u518d\u6839
CBFEDA
\u70e6\u8bf7\u91c7\u7eb3 \u8c22\u8c22
using namespace std;
typedef struct BinaryTree
{
char data;
struct BinaryTree *lchild,*rchild;
}BinaryTree,*BiTree;
void CreateBiTree(BiTree &T)
{
char z;
if((z=getchar())==' ')
T=NULL;
else
{
T=(BinaryTree*)malloc(sizeof(BinaryTree));
T->data=z;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//先序遍历
void preBiTree(BiTree T)
{
if(T!=NULL)
{
cout<<T->data;
preBiTree(T->lchild);
preBiTree(T->rchild);
}
}
//中序遍历
void InBiTree(BiTree T)
{
if(T!=NULL)
{
InBiTree(T->lchild);
cout<<T->data;
InBiTree(T->rchild);
}
}
//后序遍历
void PostBiTree(BiTree T)
{
if(T!=NULL)
{
PostBiTree(T->lchild);
PostBiTree(T->rchild);
cout<<T->data;
}
}
int Depth(BiTree T)
{
if(T==NULL)
{
return 0;
}
else
return 1+(Depth(T->lchild)>Depth(T->rchild)? Depth(T->lchild):Depth(T->rchild));
}
void main()
{
BiTree T;
cout<<"请输入相应二叉树:"<<endl;
CreateBiTree(T);
cout<<"二叉树的先序遍历为:"<<endl;
preBiTree(T);
cout<<endl;
cout<<"二叉树的中序遍历为:"<<endl;
InBiTree(T);
cout<<endl;
cout<<"二叉树的后序遍历为:"<<endl;
PostBiTree(T);
cout<<endl;
cout<<"二叉树的深度为:"<<endl;
cout<<Depth(T)<<endl;
}
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
int count=0;
typedef struct BiTNode {
int data;
struct BiTNode *lchild, *rchild;
} BiTNode, *BiTree;
void CreateBiTree(BiTree &T){
int ch;
scanf("%d",&ch);
if(ch==0)T=NULL;
else{
if(!(T=(BiTNode * )malloc(sizeof(BiTNode)))) exit(-1);
T->data=ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
int PreOrder(BiTree T)
{
if (!T) return 0;
printf("%d ",T->data);
PreOrder(T->lchild);
PreOrder(T->rchild);
return 1;
}
int InOrder(BiTree T)
{
if (!T) return 0;
InOrder(T->lchild);
printf("%d ",T->data);
InOrder(T->rchild);
return 1;
}
int PostOrder(BiTree T)
{
if (!T) return 0;
PostOrder(T->lchild);
PostOrder(T->rchild);
printf("%d ",T->data);
return 1;
}
int CountLeaf (BiTree T){
if (!T ) return 0;
if (!T->lchild && !T->rchild) return 1;
int m;
int n;
m = CountLeaf( T->lchild);
n = CountLeaf( T->rchild);
return (m+n);
}
void main(){
int a;
BiTree T;
CreateBiTree(T);
printf("先序遍历:");
PreOrder(T);
printf("中序遍历:");
InOrder(T);
printf("后序遍历:");
PostOrder(T);
a=CountLeaf(T);
printf("叶子节点个数:");
printf("%d",a);
}
绛旓細cout<<"璇疯緭鍏ョ浉搴浜屽弶鏍:"<<endl;CreateBiTree(T);cout<<"浜屽弶鏍戠殑鍏堝簭閬嶅巻涓:"<<endl;preBiTree(T);cout<<endl;cout<<"浜屽弶鏍戠殑涓簭閬嶅巻涓:"<<endl;InBiTree(T);cout<<endl;cout<<"浜屽弶鏍戠殑鍚庡簭閬嶅巻涓:"<<endl;PostBiTree(T);cout<<endl;cout<<"浜屽弶鏍戠殑娣卞害涓:"<<endl;cout...
绛旓細BiTree( ); //鏋勯鍑芥暟锛屽垵濮嬪寲涓妫典簩鍙夋爲锛屽叾鍓嶅簭搴忓垪鐢遍敭鐩樿緭鍏 ~BiTree(void); //鏋愭瀯鍑芥暟锛岄噴鏀句簩鍙夐摼琛ㄤ腑鍚缁撶偣鐨勫瓨鍌ㄧ┖闂 BiNode<T>* Getroot(); //鑾峰緱鎸囧悜鏍圭粨鐐圭殑鎸囬拡 void PreOrder(BiNode<T> *root); //鍓嶅簭閬嶅巻浜屽弶鏍 void InOrder(BiNode<T> *root); //涓...
绛旓細BiTNode *bulid() /*寤烘爲*/ { BiTNode *q;BiTNode *s[20];int i,j;char x;printf("璇锋寜椤哄簭杈撳叆浜屽弶鏍鐨勭粨鐐逛互杈撳叆0鍜*鍙风粨鏉焅n");printf("璇疯緭鍏ヨ杈撳叆鐨勪负绗嚑涓缁撶偣i=\n");scanf("%d",&i);printf("璇疯緭鍏ヤ綘瑕佽緭鍏ヨ缁撶偣鐨勬暟涓簒=");getchar();scanf("%c",&x);while(i!=...
绛旓細鐜板湪鏉ョ湅鍙冲瓙鏍戯細宸茬煡鍙冲瓙鏍戠殑鏍逛负F 浠庝腑搴忓彲鐭ワ紝F鏈夊乏鍙冲瓙鏍戯紝涓斿乏鍙冲潎涓2涓缁撶偣锛屼粠鍚庡簭搴忓垪鍙煡鍏跺墠鐨処灏辨槸鍙冲瓙鏍戠殑鏍癸紝鍥犳锛屽厛搴廕鍓嶉潰鐨勫氨鏄疘锛屽苟涓斾腑搴忔渶鍚庣殑灏辨槸J 鍓╀笅鐨勫氨鍙互琛ュ厖瀹屾暣浜嗭紙鍏跺疄鐢浜屽弶鏍鐨勯亶鍘嗗簭鍒椾篃鍙‖鎬ф帹瀵煎嚭锛夋渶鍚庣粨鏋滄槸锛氬厛搴忓簭鍒 锛欰 B C D E F G H I J...
绛旓細printf("1.The preorder traverse \n");printf("2.The inorder traverse \n");printf("3.The postorder traverse \n");printf(" Please choose a kind of order\n");scanf("%d",&i);switch(i){ case 1:preorder(root);break;case 2:inorder(root);break;case 3:postorder(root);...
绛旓細if(hl==0&&hr==0) leaf=leaf+1; //鑻ュ乏鍙虫繁搴︿负0锛屽嵆涓哄彾瀛愩俽eturn(max+1);} else return(0);} //===鍒╃敤"鍏堣繘鍏堝嚭"锛團IFO锛夐槦鍒楋紝鎸夊眰娆¢亶鍘浜屽弶鏍=== void Levelorder(BinTree T){ int front=0,rear=1;BinTNode *cq[Max],*p; //瀹氫箟缁撶偣鐨勬寚閽堟暟缁刢q cq[1]=T; //鏍瑰叆...
绛旓細//鏋勯犱簩鍙夋爲 Status CreateBiTree(BiTree &T){ elemtype ch;ch=getchar();if(ch==' '){T=NULL;} else{ if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))return FALSE;T->data=ch;CreateBiTree(T->lchild);CreateBiTree(T->rchild);} return OK;} // 鍏堝簭閬嶅巻 void PreOrderTraverse(...
绛旓細绗涓涓鑲畾鏄爲鐨勬牴锛屽厛鐢籄锛孉鍐嶄腑搴忛亶鍘嗕腑宸﹀彸閮芥湁锛岃鏄嶢鏈夊乏瀛愭爲涔熸湁鍙冲瓙鏍戙傚厛鐪嬪乏瀛╁瓙涓杈癸紝鍏堝簭涓嬩竴涓负B,鏁呭畠涓烘牴鐨勫乏瀛╁瓙锛屼笖涓簭涓瑼鍦˙鐨勫墠杈癸紝鎵浠涓築鐨勫乏瀛╁瓙锛屽啀鐪嬪厛搴忎腑鐨凞锛屽畠灏辨槸B鐨勫彸瀛╁瓙锛屼笖涓簭涓瑿鍦―鐨勫墠闈紝鎵浠涓篋鐨勫乏瀛╁瓙锛屾牴鐨勫乏鏋濆畬浜嬨傚彸杈瑰悓鐞嗐
绛旓細宸茬煡涓妫典簩鍙夋爲鐨勫厛搴忛亶鍘嗗簭鍒楀拰涓簭閬嶅巻搴忓垪鍒嗗埆鏄疉BDCEF銆丅DAECF锛屾眰浜屽弶鏍戝強鍚庡簭閬嶅巻搴忓垪銆傚垎鏋愶細鍏堝簭閬嶅巻搴忓垪鐨勭涓涓瓧绗︿负鏍圭粨鐐广傚浜庝腑搴忛亶鍘嗭紝鏍圭粨鐐瑰湪涓簭閬嶅巻搴忓垪鐨勪腑闂达紝宸﹁竟閮ㄥ垎鏄牴缁撶偣鐨勫乏瀛愭爲鐨勪腑搴忛亶鍘嗗簭鍒楋紝鍙宠竟閮ㄥ垎鏄牴缁撶偣鐨勫彸瀛愭爲鐨勪腑搴忛亶鍘嗗簭鍒椼傚厛搴忥細ABDCEF --> A BD CEF ...
绛旓細void Levelorder(BTree T)//灞傛閬嶅巻浜屽弶鏍 { int front=0,rear=1;BTNode *cq[Max],*p;//瀹氫箟缁撶偣鐨勬寚閽堟暟缁刢q cq[1]=T;//鏍瑰叆闃 while(front!=rear){ front=(front+1)%NodeNum;p=cq[front];//鍑洪槦 printf("%c",p->data);//鍑洪槦锛岃緭鍑缁撶偣鐨勫 if(p->lchild!=NULL){rear=(...