数据结构 二叉树的建立 与各种遍历
2012-11-08 21:39
537 查看
#include <iostream> #include <string.h> using namespace std; class tree { public: char c; tree* rchild,*lchild; }; char c; int main() { void build(tree *&p); void Preorder(tree *p); void Inorder1(tree *p); void Inorder2(tree *p); void Inorder3(tree *p); void Postorder(tree *p); int i,j,n,m,s,t; tree *head; build(head); cout<<"先序递归遍历:"<<endl; Preorder(head); cout<<endl; cout<<"中序递归遍历遍历:"<<endl; Inorder1(head); cout<<endl; cout<<"中序非递归遍历:"<<endl; Inorder2(head); cout<<endl; cout<<"中序非递归遍历:"<<endl; Inorder3(head); cout<<endl; cout<<"后续遍历:"<<endl; Postorder(head); cout<<endl; return 0; } void build(tree *&p) { cin>>c; if(c=='$') { p=NULL; }else { p=new(tree); p->c=c; build(p->lchild); build(p->rchild); } } void Preorder(tree *p) { if(p) { cout<<p->c; Preorder(p->lchild); Preorder(p->rchild); } } void Inorder1(tree *p) { if(p) { Inorder1(p->lchild); cout<<p->c; Inorder1(p->rchild); } } void Inorder2(tree *p) { int top=0; tree* statck[10000]; statck[top++]=p; while(top>0) { while((p=statck[top-1])&&p) { p=p->lchild; statck[top++]=p; } top-=1; if(top>0) { p=statck[--top]; cout<<p->c; statck[top++]=p->rchild; } } } void Inorder3(tree *p) { int top=0; tree* statck[100000]; while(p||top>0) { if(p) { statck[top++]=p; p=p->lchild; }else { p=statck[--top]; cout<<p->c; p=p->rchild; } } } void Postorder(tree *p) { if(p) { Postorder(p->lchild); Postorder(p->rchild); cout<<p->c; } }
相关文章推荐
- 二叉树节点数据结构-练习 5 二叉树的建立 遍历
- 顺序结构实现二叉树的建立和各种遍历
- 二叉树的建立、销毁、各种遍历(递归、非递归)
- 数据结构二叉树——建立二叉树、中序递归遍历、非递归遍历、层次遍历
- 数据结构--排序-查询-二叉树各种遍历-求深度
- 基本数据结构——二叉树的建立,遍历,求叶子节点,深度计算
- 数据结构_二叉树的先序建立与先序,中序,后序(递归)遍历方式_C语言源代码
- 20170513_一步一步写数据结构(二叉树的建立和遍历)
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- 数据结构-二叉树的各种遍历(先中后层序!!)
- python数据结构--二叉树建立与遍历
- 二叉树的建立、销毁、各种遍历(递归、非递归)
- 数据结构----二叉树----建立与遍历
- 数据结构_树_二叉树的建立、遍历、复制与移除_二叉链表存储_C++实现
- 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)
- 二叉树是笔试面试中考试最频繁的数据结构之一,主要包括,程序建立一个二叉树,三种次序遍历二叉树,返回叶子节点的数目,求二叉树节点的总数等。建立一个二叉树节点的数据结构
- 数据结构二叉树——建立二叉树、中序递归遍历、非递归遍历、层次遍历
- 数据结构试验-试验7二叉树的建立与遍历
- 二叉树建立、各种遍历、求叶子节点
- C语言创建二叉树数据结构, 以及各种遍历