二叉树的遍历和存储

来源:互联网 发布:rimworld for mac 编辑:程序博客网 时间:2024/05/24 15:38

例:数据结构实验之二叉树二:遍历二叉树

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。

Input

连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。

Output

每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。

 

Example Input

abc,,de,g,,f,,,

Example Output

cbegdfacgefdba

Hint

 

Author

 xam


Think:二叉树的遍历方法有很多,先序、中序、后序、层序。
              二叉树的建立,遍历,查找,删除,插入等操作,自然也离不开模板 *^_^*。
代码实现:
#include <stdio.h>#include <stdlib.h>#include <string.h>struct node{   char m;   struct node * left;   struct node * right;};int i;char a[55];struct node *creat(struct node * bt)//先序建立二叉树{  if(a[++i]==',')  bt = NULL;  else  {  bt = (struct node *)malloc(sizeof(struct node));  bt->m = a[i];  bt->left= creat(bt->left);  bt->right= creat(bt->right);  }  return bt;}void inordertraverse(struct node * bt)//中序遍历{   if(bt!=NULL)   {     inordertraverse(bt->left);     printf("%c", bt->m);     inordertraverse(bt->right);   }}void postordertraverse(struct node * bt)//后序遍历{    if(bt!=NULL)    {       postordertraverse(bt->left);       postordertraverse(bt->right);       printf("%c", bt->m);    }}int main(){   while(~scanf("%s", a))   {      i = -1;      struct node * p;      p = creat(p);     inordertraverse(p);      printf("\n");      postordertraverse(p);      printf("\n");   }    return 0;}
****************************************************************************
****************************************************************************
函数模板:
struct node *creat(struct node * bt)//创建结点{  if(a[++i]==',')  bt = NULL;  else  {  bt = (struct node *)malloc(sizeof(struct node));  bt->m = a[i];  bt->left= creat(bt->left);  bt->right= creat(bt->right);  }  return bt;}void inordertraverse(struct node * bt)//中序遍历{   if(bt!=NULL)   {     inordertraverse(bt->left);     printf("%c", bt->m);     inordertraverse(bt->right);   }}void postordertraverse(struct node * bt)//后序遍历{    if(bt!=NULL)    {       postordertraverse(bt->left);       postordertraverse(bt->right);       printf("%c", bt->m);    }}void preordertraverse(struct node * bt)//前序遍历{     if(bt!=NULL)     {        printf("%c", bt->m);        preordertraverse(bt->left);        preordertraverse(bt->right);     }}int count(struct node * bt)//求叶子结点{  int m = 0;   if(!bt)   {      return 0;   }   else if(bt->left==NULL&&bt->right==NULL)   {      return 1;   }   m += count(bt->left)+count(bt->right);   return m;}void levelordertraverse(struct node * bt)//层序遍历{   struct node * p;   struct node * queue[1005];   memset(queue, '0', sizeof(queue));   int front, rear =  0;   if(bt)   {      queue[rear++] = bt;      while(front!=rear)      {        p = queue[front++];          printf("%c", p->m);          if(p->left!=NULL)          queue[rear++] = p->left;          if(p->right!=NULL)          queue[rear++] = p->right;      }   }}int deep(struct node * bt)//求树的深度{    int h, lh, rh;    if(bt==NULL)    {       h = 0;    }    else    {      lh = deep(bt->left);      rh = deep(bt->right);      if(lh>=rh)      h = lh + 1;      else      h = rh + 1;    }    return h;}
tree*creat(char * center, char * back, int len)//已知中序、后序求先序{    if(len<=0)    return NULL;    tree * temp = new tree;    temp->data = * back;    int index = search(*back, center, len);    temp->right = creat(center+index+1, back-1,len-index-1);    temp->left = creat(center, back-len+index, index);    return temp;}
int search(char m, char * array, int len){    for(int i = 0; i < len;i++)     if(array[i]==m)     return i;}
char preorder[100];//已知前序、中序求后序char midorder[100];struct node * order(int prel, int prer, int midl, int midr){  if(prer<prel)  {    return NULL;  }  struct node * root;  root = (struct node*)malloc(sizeof(struct node));  root->m = preorder[prel];  if(prel==prer)  {    root->left = NULL;    root->right = NULL;    return root;  }  int index;  for(index = midl;index<=midr;index++)  {    if(midorder[index]==preorder[prel])    break;  }  root->left = order(prel+1, prel+(index-midl), midl, index-1);  root->right = order(prel+1+(index-midl), prer, index+1, midr);  return root;}
 
0 0