普通树的存储与遍历(合集)
来源:互联网 发布:java开源加密工具 编辑:程序博客网 时间:2024/06/10 00:11
1.双亲表示法
#include<iostream>#include<cstdio>#define maxn 1111using namespace std;typedef struct node{ char data;//节点的值 int parent;//该节点的父亲}node;typedef struct tree{ node treelist[maxn];//节点 int length;//节点个数 int r; //存放根节点}Tree;void init(Tree &tree) //初始化树{ tree.length=0;}void create(Tree &tree)//以表的方式输入{ for(int i=0;i<tree.length;i++) cin>>tree.treelist[i].data>>tree.treelist[i].parent;}void Print(Tree &tree){ for(int i=0;i<tree.length;i++) cout<<tree.treelist[i].data<<' '<<tree.treelist[i].parent<<endl;}void preorder(Tree &tree,int num)//前序遍历{ if(tree.length==num) return ; cout<<tree.treelist[num].data; for(int i=num+1;i<tree.length;i++) if(tree.treelist[i].parent==num) preorder(tree,i);}void postorder(Tree &tree,int num)//后序遍历{ if(tree.length==num) return ; for(int i=num+1;i<tree.length;i++) if(tree.treelist[i].parent==num) postorder(tree,i); cout<<tree.treelist[num].data;}int main(){ Tree tree; init(tree); cin>>tree.length; create(tree); Print(tree); preorder(tree,0); cout<<endl; postorder(tree,0); return 0;}
2.孩子指针表示法
#include<iostream>#include<queue>#include<cstdio>#define maxn 11111#define m 3using namespace std;typedef char datetype;typedef struct k{ datetype data; struct k *child[m];//孩子指针表示法建树}Tree;typedef Tree type;typedef struct{ Tree que[maxn]; int f,r;}Queue;void Init(Queue *q){ q->f=q->r=0; }void Push(Queue *q,Tree t){ q->que[q->r++]=t;}void Pop(Queue *q){q->f++;}int Empty(Queue *q){return q->f==q->r?1:0;}type Front(Queue *q){return q->que[q->f];}Tree* createbypre(Tree * t) //以前序遍历的方式建树{ char c; cin>>c; if(c == '#') return NULL; t = new Tree; t->data = c; for(int i =0;i<m;i++) t->child[i] = createbypre(t->child[i]); return t;}void preorder(Tree *t)//树的前序遍历{ if(t==NULL) return ; cout<<t->data; for(int i=0;i<m;i++) preorder(t->child[i]);}void postorder(Tree *t)//树的后续遍历{ if(t==NULL) return ; for(int i=0;i<m;i++) postorder(t->child[i]); cout<<t->data;}void levelorder2(Tree t)//树的层次遍历{ queue<Tree>q; q.push(t); while(!q.empty()) { Tree p = q.front(); cout<<p.data; q.pop(); for(int i=0;i<m;i++) if(p.child[i]!=NULL) q.push(*(p.child[i])); }}void levelorder1(Tree t)//自己创建的队列进行层次遍历{ Queue q; Init(&q); Push(&q,t); while(!Empty(&q)) { Tree p =Front(&q); cout<<p.data; Pop(&q); for(int i=0;i<m;i++) if(p.child[i]!=NULL) Push(&q,*p.child[i]); }}int main(){ Tree *t=NULL; t=createbypre(t); preorder(t); cout<<endl; postorder(t); cout<<endl; levelorder1(*t); cout<<endl; levelorder2(*t); return 0;}
3.孩子数组表示法
#include<iostream>#include<cstdio>#define maxn 111111#define m 3using namespace std;typedef struct{ char data; int child[m];//数组保存了孩子的下标}node;typedef struct{ node treelist[maxn]; int len;}Tree;void create(Tree &t){ for(int i=0;i<t.len;i++) { cin>>t.treelist[i].data; for(int j=0;j<m;j++) cin>>t.treelist[i].child[j]; }}void Print(Tree &t){ for(int i=0;i<t.len;i++) { cout<<t.treelist[i].data<<' '; for(int j=0;j<m;j++) cout<<t.treelist[i].child[j]<<' '; cout<<endl; }}void preorder(Tree *t,int num)//前序遍历{ if(t->treelist[num].data==-1) return ; cout<<t->treelist[num].data; for(int i=0;i<m;i++) if(t->treelist[num].child[i]!=-1) preorder(t,t->treelist[num].child[i]);}void postorder(Tree *t, int num)//后序遍历{ if(t->treelist[num].data==-1) return ; for(int i=0;i<m;i++) if(t->treelist[num].child[i]!=-1) postorder(t,t->treelist[num].child[i]); cout<<t->treelist[num].data;}int main(){ Tree t; cin>>t.len; create(t); //Print(t); preorder(&t,0); cout<<endl; postorder(&t,0); return 0;}4.孩子链式表示法
#include<iostream>#include<cstdio>#define maxn 111111using namespace std;typedef struct k{ int id; struct k *next;}node;typedef struct{ char data; node * child;}treedata;typedef struct{ treedata treelist[maxn]; int len;}Tree;void create(Tree &t){ int id; node *p,*q; for(int i=0;i<t.len;i++) { cin>>t.treelist[i].data; t.treelist[i].child=NULL; cin>>id; while(id!=-1) { if(t.treelist[i].child==NULL) p=t.treelist[i].child = new node ; q=new node; q->id=id; p->next=q; p=p->next; cin>>id; } if(t.treelist[i].child!=NULL) p->next=NULL; }}void Print(Tree &t){ node *p; for(int i=0;i<t.len;i++) { cout<<t.treelist[i].data; if(t.treelist[i].child==NULL) { cout<<endl; continue; } p=t.treelist[i].child->next; while(p) { cout<<p->id; p=p->next; } cout<<endl; }}void preorder(Tree *t,int num)//前序遍历{ if(t->len==num) return ; cout<<t->treelist[num].data; node *p=t->treelist[num].child; if(p!=NULL) p=p->next; while(p) { preorder(t,p->id); p=p->next; }}void postorder(Tree *t,int num){ if(t->len==num) return; node *p=t->treelist[num].child; if(p!=NULL) p=p->next; while(p) { postorder(t,p->id); p=p->next; } cout<<t->treelist[num].data;}int main(){ Tree t; cin>>t.len; create(t); Print(t); preorder(&t,0); cout<<endl; postorder(&t,0); return 0;}
0 0
- 普通树的存储与遍历(合集)
- 树的存储与遍历
- 普通树的遍历算法
- 普通有序树的遍历
- 数据结构之二叉树的遍历算法合集
- 二叉树的存储与遍历
- 树的输入、存储与遍历
- Codevs 二叉树遍历问题 合集
- 图的存储与遍历
- 图的存储与遍历
- 图的存储与遍历
- 图的存储与遍历
- 二叉树 普通的 建立和遍历
- 普通树的非递归遍历
- 普通树的深度优先遍历和广度优先遍历
- ArrayList与LinkedList的普通for循环遍历
- java ArrayList与LinkedList的普通for循环遍历
- ArrayList与LinkedList的普通for循环遍历效率对比
- Web工程中配置文件路径问题
- 学完《软件工程(C编码实践篇)》之后的总结
- Android按钮点击事件
- sicily 1177. Take Your Vitamins
- python 函数学习
- 普通树的存储与遍历(合集)
- hdu Flood-it!(IDA*算法)
- c++学习log1
- 深入理解计算机系统(2)
- notification几种点击事件
- 图片列表换行错位,图片无法左对齐怎么办
- 第3章 一切基于pom
- 推荐系统小结
- WEBSERVICE各家之说