您的位置:首页 > 其它

普通树的存储与遍历(合集)

2015-11-11 20:00 232 查看
1.双亲表示法

#include<iostream>
#include<cstdio>
#define maxn 1111
using 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 3
using 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 3
using 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 111111
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息