二叉树
来源:互联网 发布:cookie登录 php 编辑:程序博客网 时间:2024/06/05 02:34
简单的二叉树的创建和前中后遍历
#include <stdio.h>#include <iostream>char Nil='#';//空结点的标志using namespace std;//结点定义struct node{ char data;//数据 node *leftchild;//左指针 node *rightchild;//右指针};void CreateBiTree(node* &T){ //用前序遍历的方式建立二叉树 //变量Nil表示空(子)树 char ch; cin>>ch; if(ch==Nil) { T=NULL; } else { T=new node; T->data=ch; CreateBiTree(T->leftchild); CreateBiTree(T->rightchild); }}//================================================中序遍历二叉树void Inorder(node* T){//中序递归遍历二叉树 if(T){//bt=null退层 Inorder(T->leftchild);//中序遍历左子树 cout<<T->data;//访问参数 Inorder(T->rightchild);//中序遍历右子树 } else cout<<""; } //===============================================先序递归遍历二叉树void PreOrderTraverse(node* &T){ //先序递归遍历二叉树 if(T){//当结点不为空的时候执行 cout<<T->data; PreOrderTraverse(T->leftchild);// PreOrderTraverse(T->rightchild); } else cout<<"";}//=================================================后序递归遍历二叉树void Posorder(node* &T){ if(T){ Posorder(T->leftchild);//后序递归遍历左子树 Posorder(T->rightchild);//后序递归遍历右子树 cout<<T->data;//访问根结点 } else cout<<"";}int main(){ freopen("in.txt","r",stdin); node *T;//二叉树根节点 CreateBiTree(T); cout<<"前序遍历的结果"<<endl; PreOrderTraverse(T);cout<<endl; cout<<"中序遍历的结果"<<endl; Inorder(T);cout<<endl; cout<<"后序遍历的结果"<<endl; Posorder(T);cout<<endl; return 0;}
1 0
- 二叉树、二叉堆
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- XCode 快捷键
- 60. Permutation Sequence
- CODEFORCES 272D Dima and Two Sequences <排列组合>
- Cocos网络访问——代理模式(2)
- Hibernate应用的优化
- 二叉树
- POJ 1961 Period(KMP)
- 【CQYZ-vijos】P1333 舞伴的搭配(贪心算法)
- C++中虚函数解析
- 日期类问题
- 分享一个圆角自定义的漂亮AlertDialog
- java旅行第二站--JavaSE第四天
- 【字符串8】正则表达式匹配
- codeforces #365 div2ABC题解