建立二叉树

本文介绍了一种根据后序遍历和中序遍历序列构造二叉树的方法,并实现了层次遍历与先序遍历算法。通过C++代码详细展示了如何递归地构建二叉树并进行遍历。
  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <queue>
  4 #define TREE_NODE_NUM 100
  5 
  6 using namespace std;
  7 typedef struct node {
  8         char data;
  9         struct node *lchild,*rchild;
 10         }treenode;
 11 
 12 void Creat_tree(treenode *&bt,char post[],int post_l,int post_h,char in[],int in_l,int in_h)//
 13 {int i;
 14 for(i=in_l;i<=in_h;i++)
 15 if(in[i]==post[post_h])
 16 break;
 17 if(i>in_h)
 18 {cout<<"输入序列错误,不能构成二叉树\n";exit(1);}
 19 bt->data=post[post_h];
 20 if(i==in_l)
 21 {bt->lchild=NULL;}
 22 else
 23 {treenode *p=(treenode *)malloc(sizeof(treenode));
 24  bt->lchild=p;
 25  
 26  Creat_tree(p,post,post_l,post_l+i-1-in_l,in,in_l,i-1);//
 27 }
 28 if(i==in_h)
 29 {bt->rchild=NULL;}
 30 else
 31 {treenode *p=(treenode *)malloc(sizeof(treenode));
 32  bt->rchild=p;
 33   Creat_tree(p,post,post_l+i-in_l,post_h-1,in,i+1,in_h);//
 34 }
 35 }
 36 
 37 void C_order(treenode *bt)
 38 {if(bt==NULL)
 39  {cout<<"空树\n";return ;}
 40  cout<<bt->data;
 41 queue<treenode *> q;
 42  q.push(bt);
 43  treenode *p;
 44  while(!q.empty())
 45  {p=q.front();
 46   q.pop();
 47   if(p->lchild)
 48   {cout<<p->lchild->data;
 49   q.push(p->lchild);}
 50   if(p->rchild)
 51   {cout<<p->rchild->data;
 52    q.push(p->rchild);}
 53   }
 54 }
 55 void Preorder(treenode *bt)
 56 {
 57      if(!bt)
 58      return ;
 59      cout<<bt->data;
 60      Preorder(bt->lchild);
 61      Preorder(bt->rchild);
 62 }
 63      
 64  
 65   
 66 
 67 int main(int argc, char *argv[])
 68 {cout<<"请输入后序遍历序列\n";
 69 int i;
 70  char post[TREE_NODE_NUM];
 71  int post_l,post_h;
 72  gets(post);
 73  for(i=0;post[i];i++);
 74  post_l=0;post_h=i-1;
 75  cout<<"请输入中序遍历序列\n";
 76  char in[TREE_NODE_NUM];
 77  int in_l,in_h;
 78  gets(in);
 79  in_l=0;in_h=i-1;
 80  if(in_l>in_h)
 81  {cout<<"数为空树\n";return 1;}
 82   
 83  
 84  treenode *bt=(treenode *)malloc(sizeof(treenode));
 85  Creat_tree(bt,post,post_l,post_h,in,in_l,in_h);
 86  cout<<"层次遍历:";
 87  C_order(bt);
 88  cout<<endl;
 89  cout<<"先序遍历: ";
 90  Preorder(bt);
 91  cout<<endl; 
 92 
 93  
 94  
 95  
 96  
 97     system("PAUSE");
 98     return EXIT_SUCCESS;
 99 }
100 
101  

转载于:https://www.cnblogs.com/zjushuiping/archive/2012/05/30/2527011.html

在C语言中,常见的建立二叉树方法有多种,下面介绍几种典型的方法。 ### 基于链式存储结构,通过前序遍历序列建立二叉树 可以使用链式存储结构来表示二叉树,其节点的定义如下: ```c typedef struct BitNode { char data; struct BitNode* lchild, * rchild; } BitNode, * BiTree; ``` 通过前序遍历序列来创建二叉树的代码示例如下: ```c #include <stdio.h> #include <stdlib.h> typedef struct BitNode { char data; struct BitNode* lchild, * rchild; } BitNode, * BiTree; // 通过前序遍历序列创建二叉树 BiTree createBiTree() { char ch; scanf("%c", &ch); if (ch == '#') { return NULL; } else { BiTree T = (BiTree)malloc(sizeof(BitNode)); T->data = ch; T->lchild = createBiTree(); T->rchild = createBiTree(); return T; } } // 测试代码 int main() { BiTree T = createBiTree(); // 这里可以添加对二叉树的其他操作 return 0; } ``` 在上述代码中,使用 `#` 表示空节点。通过递归的方式,根据前序遍历序列依次创建节点。 ### 基于数组存储结构建立完全二叉树 对于完全二叉树,可以使用数组来存储。假设数组 `tree` 存储二叉树的节点,节点 `i` 的左子节点为 `2 * i + 1`,右子节点为 `2 * i + 2`。 ```c #include <stdio.h> #define MAX_SIZE 100 // 数组存储完全二叉树 char tree[MAX_SIZE]; // 初始化完全二叉树 void initTree() { // 这里可以进行数组元素的赋值操作 // 例如: tree[0] = 'A'; tree[1] = 'B'; tree[2] = 'C'; // ... } // 测试代码 int main() { initTree(); // 这里可以添加对二叉树的其他操作 return 0; } ``` ### 基于中序和前序遍历序列建立二叉树 已知二叉树的中序和前序遍历序列,可以唯一确定一棵二叉树。代码示例如下: ```c #include <stdio.h> #include <stdlib.h> typedef struct BitNode { char data; struct BitNode* lchild, * rchild; } BitNode, * BiTree; // 在中序序列中查找根节点的位置 int findIndex(char in[], int start, int end, char value) { for (int i = start; i <= end; i++) { if (in[i] == value) { return i; } } return -1; } // 根据中序和前序序列创建二叉树 BiTree buildTree(char in[], char pre[], int inStart, int inEnd, int* preIndex) { if (inStart > inEnd) { return NULL; } BiTree T = (BiTree)malloc(sizeof(BitNode)); T->data = pre[(*preIndex)++]; if (inStart == inEnd) { T->lchild = NULL; T->rchild = NULL; return T; } int inIndex = findIndex(in, inStart, inEnd, T->data); T->lchild = buildTree(in, pre, inStart, inIndex - 1, preIndex); T->rchild = buildTree(in, pre, inIndex + 1, inEnd, preIndex); return T; } // 测试代码 int main() { char in[] = {'D', 'B', 'E', 'A', 'F', 'C'}; char pre[] = {'A', 'B', 'D', 'E', 'C', 'F'}; int preIndex = 0; BiTree T = buildTree(in, pre, 0, sizeof(in) / sizeof(in[0]) - 1, &preIndex); // 这里可以添加对二叉树的其他操作 return 0; } ``` 在上述代码中,通过递归的方式,根据前序序列确定根节点,在中序序列中找到根节点的位置,从而划分左右子,再递归创建左右子
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值