您的位置:首页 > 其它

前序创建二叉树+ 前序/中序/后序遍历二叉树

2015-11-08 16:03 411 查看
二叉树的建立:

如果要在内存中建立一个如下左图这样的树,wield能让每个结点确认是否有左右孩子,我们对它进行扩展,变成如下右图的样子,也就是将二叉树中的每个结点的空指针引出一个虚结点,其值为一个特定值,比如”#”,称之为扩展二叉树。扩展二叉树就可以做到一个遍历序列确定一棵二叉树了。如前序遍历序列为AB#D##C##。



    有了这样的准备,就可以看看如何生成一棵二叉树了。假设二叉树的结点均为一个字符,把刚才前序遍历序列AB#D##C##用键盘挨个输入,实现的算法如下所示。

精简版

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

using namespace std;
struct bitree{
char data ;
struct bitree * lchild , *rchild ;
};
//设置空二叉树
void setnull(struct bitree * bt){
bt->lchild = NULL ;
bt->rchild = NULL ;
}

//先序创建二叉树操作
struct bitree * createbitree(){
char c ;
struct bitree * bt ;
scanf("%c" , & c) ;
if(c == '#')
bt = NULL ; // 又是因为 bt == NULL 造成的错误,导致调试了一下午
else{
bt = (struct bitree *) malloc(sizeof(struct bitree)) ;
bt->data = c ;
bt->lchild = createbitree() ;
bt->rchild = createbitree() ;
}
return bt ; // 返回根结点
}
//前序遍历二叉树
void preorder(struct bitree * root){
if(root != NULL){
printf("%c\n" , root->data) ;
preorder(root->lchild) ;
preorder(root->rchild) ;
}
}

// 中序遍历二叉树
void inorder(struct bitree * root){
if(root != NULL){
inorder(root ->lchild) ;
printf("%c\n" , root->data) ;
inorder(root->rchild) ;
}
}

//后序遍历二叉树

void postorder(struct bitree * root){
if(root != NULL){
postorder(root->lchild) ;
postorder(root->rchild) ;
printf("%c\n" , root->data) ;
}
}
int main(){
int n = 10 ;
while(n--){
struct bitree * t ;
cout<<"前序建立二叉树"<<endl;
cout<<"请输入ABD#F###CE#G### 或者AB#D##C## "<< endl;
t = createbitree() ;
cout<<"前序遍历二叉树" << endl;
preorder(t) ;
cout<<"中序遍历二叉树"<<endl;
inorder(t) ;
cout<<"后序遍历二叉树" << endl;
postorder(t) ;
getchar( ) ;
}
return 0 ;
}





输出版

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

using namespace std;
struct bitree{
char data ;
struct bitree * lchild , *rchild ;
};
//设置空二叉树
void setnull(struct bitree * bt){
bt->lchild = NULL ;
bt->rchild = NULL ;
}

//先序创建二叉树操作
struct bitree * createbitree(){
char c ;
struct bitree * bt ;
scanf("%c" , & c) ;
//printf("输入的c是%c\n" ,c) ;
if(c == '#')
bt = NULL ; // 又是因为 bt == NULL 造成的错误,导致调试了一下午
else{
bt = (struct bitree *) malloc(sizeof(struct bitree)) ;
bt->data = c ;
// cout<<"bt->Data = " << c<< endl;
bt->lchild = createbitree() ;
// cout<<bt->data<< "--- bt-> lchild = " << bt->lchild<<endl ;
bt->rchild = createbitree() ;
//  cout<<bt->data<< "--- bt-> rchild = " <<bt->rchild << endl;
}
//cout<<c << "--bt--"<<bt << endl;
return bt ; // 返回根结点
}

//前序遍历二叉树
void preorder(struct bitree * root){
//cout<<"root = " << root << endl ;
if(root != NULL)
cout<<"****root->data = " << root->data << endl;
else
cout<<"##############"<<endl;
if(root != NULL){
printf("%c\n" , root->data) ;
preorder(root->lchild) ;
preorder(root->rchild) ;
}
}

// 中序遍历二叉树
void inorder(struct bitree * root){
if(root != NULL)
cout<<"****root->data = " << root->data << endl;
else
cout<<"##############"<<endl;
if(root != NULL){
inorder(root ->lchild) ;
printf("%c\n" , root->data) ;
inorder(root->rchild) ;
}
}

//后序遍历二叉树

void postorder(struct bitree * root){
if(root != NULL)
cout<<"****root->data = " << root->data << endl;
else
cout<<"##############"<<endl;
if(root != NULL){
postorder(root->lchild) ;
postorder(root->rchild) ;
printf("%c\n" , root->data) ;
}
}
int main(){
int n = 10 ;
while(n--){
struct bitree * t ;
cout<<"前序建立二叉树"<<endl;
cout<<"请输入ABD#F###CE#G### 或者AB#D##C## "<< endl;
t = createbitree() ;
cout<<"前序遍历二叉树" << endl;
preorder(t) ;
cout<<"中序遍历二叉树"<<endl;
inorder(t) ;
cout<<"后序遍历二叉树" << endl;
postorder(t) ;
getchar( ) ;
}

return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 遍历