二叉树递归,非递归,前中后遍历
2013-11-26 15:38
239 查看
又是一个数据结构的实验。这个实验要求实现二叉树的前,中,后序递归,非递归遍历。
一开始,想了一个最普遍的做法,其实也是实验题目上的建议。比如,输入1 2 0 0 3 0 0,0表示节点为空。
后来在网上逛了逛,发现这种输入形式的二叉树比较清晰,比如1(2,3) 显然,2是1的左孩子,3是1的右孩子。其实实现的办法也不是很复杂,定义一个字符数组s[100],再定义两个栈,一个用来放节点数据,一个用来放“(”,“,”,“)”,
输出的时候,再加一些判断就可以。下面是源程序,里面有二叉树的前,中,后序递归,非递归遍历的功能,也暂时达到了实验的要求,过段日子打算把左右孩子节点交换,求二叉树节点个数,高度也加进去。
运行结果:
源程序:
一开始,想了一个最普遍的做法,其实也是实验题目上的建议。比如,输入1 2 0 0 3 0 0,0表示节点为空。
后来在网上逛了逛,发现这种输入形式的二叉树比较清晰,比如1(2,3) 显然,2是1的左孩子,3是1的右孩子。其实实现的办法也不是很复杂,定义一个字符数组s[100],再定义两个栈,一个用来放节点数据,一个用来放“(”,“,”,“)”,
输出的时候,再加一些判断就可以。下面是源程序,里面有二叉树的前,中,后序递归,非递归遍历的功能,也暂时达到了实验的要求,过段日子打算把左右孩子节点交换,求二叉树节点个数,高度也加进去。
运行结果:
源程序:
#include<iostream> #include<string.h> #include<stack> char s[100]; using namespace std; //定义树的节点结构 typedef struct node { char data; //节点数据 struct node *lchild; //指向左孩子的指针 struct node *rchild; //指向右孩子的指针 }Bin_Tree; typedef struct node1 { Bin_Tree *btnode; bool isFirst; }BTNode; //创建二叉树,s为A(B(C,D))结构的字符串 Bin_Tree *Creat_Tree(char *s,Bin_Tree *&root) { int i; bool isRight=false; stack<Bin_Tree*> s1; //创建BinTree类型栈存放结点 stack<char> s2; //创建char类型栈存放分隔符 Bin_Tree *p; Bin_Tree *temp; root->data=s[0]; root->lchild=NULL; root->rchild=NULL; s1.push(root); //根节点进栈 i=1; while(i<strlen(s)) { if(s[i]=='(') { s2.push(s[i]); //如果输入是"(",就将其放入s2 isRight=false; } else if(s[i]==',') { isRight=true; } else if(s[i]==')') { s1.pop(); s2.pop(); } else if(isalpha(s[i])) { p=(Bin_Tree *)malloc(sizeof(Bin_Tree)); p->data=s[i]; p->lchild=NULL; p->rchild=NULL; temp=s1.top(); if(isRight==true) { temp->rchild=p; cout<<temp->data<<"的右孩子是"<<s[i]<<endl; } else { temp->lchild=p; cout<<temp->data<<"的左孩子是"<<s[i]<<endl; } if(s[i+1]=='(') s1.push(p); } i++; } return root; } //显示树形结构 //----------------------------------------- void Display_Tree(Bin_Tree *root) { if(root!=NULL) { cout<<root->data; if(root->lchild!=NULL) { cout<<'('; Display_Tree(root->lchild); } if(root->rchild!=NULL) { cout<<','; Display_Tree(root->rchild); cout<<')'; } } } //*************************************** //递归前,中,后序遍历 //*************************************** //递归前序遍历 void preorder(Bin_Tree *root) { if(root!=NULL) { cout<<root->data<<" "; preorder(root->lchild); preorder(root->rchild); } } //递归中序遍历 void inorder(Bin_Tree *root) { if(root!=NULL) { inorder(root->lchild); cout<<root->data<<" "; inorder(root->rchild); } } //递归后序遍历 void postorder(Bin_Tree *root) { if(root!=NULL) { postorder(root->lchild); postorder(root->rchild); cout<<root->data<<" "; } } //*************************************** //非递归前,中,后序遍历 //*************************************** //非递归前序遍历 void pre_order(Bin_Tree *root) { stack<Bin_Tree*> s; Bin_Tree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { cout<<p->data<<" "; s.push(p); p=p->lchild; } if(!s.empty()) { p=s.top(); s.pop(); p=p->rchild; } } } //非递归中序遍历 void in_order(Bin_Tree *root) { stack<Bin_Tree*> s; Bin_Tree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { s.push(p); p=p->lchild; } if(!s.empty()) { p=s.top(); cout<<p->data<<" "; s.pop(); p=p->rchild; } } } //非递归后序遍历 void post_order(Bin_Tree *root) { stack<BTNode*> s; Bin_Tree *p=root; BTNode *temp; while(p!=NULL||!s.empty()) { while(p!=NULL) //沿左孩子一直往下找,直至出现没有左孩子的结点 { BTNode *btn=(BTNode *)malloc(sizeof(BTNode)); btn->btnode=p; btn->isFirst=true; s.push(btn); p=p->lchild; } if(!s.empty()) { temp=s.top(); s.pop(); if(temp->isFirst==true) //表示是第一次出现在栈顶 { temp->isFirst=false; s.push(temp); p=temp->btnode->rchild; } else //第二次出现在栈顶 { cout<<temp->btnode->data<<" "; p=NULL; } } } } //功能界面函数 //***************************************************************** void Menu() { int n; loop: cout<<endl<<"输入数字选择功能!"<<endl<<endl; cout<<"1.创建二叉树"<<endl; cout<<"2.显示二叉树"<<endl; cout<<"3.递归实现前,中,后遍历"<<endl; cout<<"4.非递归实现前,中,后遍历"<<endl; cout<<"5.退出程序"<<endl; cout<<endl<<"选择功能: "; cin>>n; if(n==1) { cout<<"请输入二叉树(注意!A(B(C,D))结构输入)"<<endl; cin>>s; Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree)); Creat_Tree(s,root); goto loop; } else if(n==2) { Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree)); root=Creat_Tree(s,root); cout<<endl<<"所建二叉树的结构是:"; Display_Tree(root); cout<<endl; goto loop; } else if(n==3) { Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree)); root=Creat_Tree(s,root); cout<<endl<<"递归前序遍历:"; preorder(root); cout<<endl<<"递归中序遍历:"; inorder(root); cout<<endl<<"递归后序遍历:"; postorder(root); cout<<endl; goto loop; } else if(n==4) { Bin_Tree *root=(Bin_Tree *)malloc(sizeof(Bin_Tree)); root=Creat_Tree(s,root); cout<<endl<<"非递归前序遍历:"; pre_order(root); cout<<endl<<"非递归前序遍历:"; in_order(root); cout<<endl<<"非递归前序遍历:"; post_order(root); cout<<endl; goto loop; } else if(n==5) { exit(0); } else { cout<<endl<<"不在功能选择范围之内!请重新输入!"<<endl<<endl; goto loop; } } //主函数 //************************************************************************ int main() { Menu(); return 0; }
相关文章推荐
- 二叉树的基本操作(非递归前中后遍历,先序,表达式建树,复制二叉树)
- 二叉树的前中后层次遍历(递归+非递归)、创建树(数组、前序+中序、中序加后序)
- 二叉树的建立&&前中后遍历(递归实现)&&层次遍历
- 非递归前中后序遍历二叉树
- 学习日志---非递归二叉树游标遍历(前中后层序)
- 二叉树的前中后序遍历的递归非递归算法
- 二叉树前中后序遍历非递归及应用
- 二叉树的前中后序的递归、非递归遍历
- 二叉树非递归遍历(前中后)
- 二叉链树的创建结点,递归构造二叉树、非递归构造二叉树、递归进行前、中、后遍历,非递归进行前中后遍历、层次遍历
- Java方式实现二叉树的前中后序遍历的递归及非递归算法
- 二叉树的建立,前中后序遍历的递归版本和非递归版本,层序遍历
- <二叉树 前中后 层序 非递归遍历 c语言实现>
- 剑指offer面试题19二叉树的镜像和二叉树的前中后遍历递归和非递归算法
- 【数据结构】二叉树前中后序遍历、层序遍历(递归实现)
- Java实现的二叉树常用操作【前序建树,前中后递归非递归遍历及层序遍历】
- 【二叉树5】前中后遍历二叉树的递归和非递归方法
- 二叉树的前中后遍历递归非递归实现
- 二叉树的遍历 -前中后--递归非递归 --链表,数组
- <二叉树 前中后 层序 非递归遍历 c语言实现>