前序创建二叉树+ 前序/中序/后序遍历二叉树
2015-11-08 16:03
411 查看
二叉树的建立:
如果要在内存中建立一个如下左图这样的树,wield能让每个结点确认是否有左右孩子,我们对它进行扩展,变成如下右图的样子,也就是将二叉树中的每个结点的空指针引出一个虚结点,其值为一个特定值,比如”#”,称之为扩展二叉树。扩展二叉树就可以做到一个遍历序列确定一棵二叉树了。如前序遍历序列为AB#D##C##。
有了这样的准备,就可以看看如何生成一棵二叉树了。假设二叉树的结点均为一个字符,把刚才前序遍历序列AB#D##C##用键盘挨个输入,实现的算法如下所示。
精简版
输出版
如果要在内存中建立一个如下左图这样的树,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 ; }
相关文章推荐
- AVL树-自平衡二叉查找树(Java实现)
- 文件遍历排序函数
- Lua 学习笔记之C API 遍历 Table实现代码
- C#遍历文件夹后上传文件夹中所有文件错误案例分析
- C#中遍历Hashtable的4种方法
- Erlang中遍历取出某个位置的最大值代码
- C++实现图的邻接矩阵存储和广度、深度优先遍历实例分析
- C++实现图的邻接表存储和广度优先遍历实例分析
- C语言二叉树的非递归遍历实例分析
- 使用C语言构建基本的二叉树数据结构
- C++非递归队列实现二叉树的广度优先遍历
- php遍历目录方法小结
- 一个目录遍历函数
- php遍历删除整个目录及文件的方法
- PHP遍历文件夹与文件类及处理类用法实例
- PHP遍历XML文档所有节点的方法
- php中使用key,value,current,next和prev函数遍历数组的方法
- C#使用前序遍历、中序遍历和后序遍历打印二叉树的方法
- 平衡二叉树
- 二叉树