[Data structures]Create binary tree by sequence
#include <stdio.h> #include <stdlib.h> #define MAX 20 typedef struct _BiTree{ char data; struct _BiTree *left; struct _BiTree *right; }BiTree; void CreateByPreInOrder(BiTree **T, char *pre, int pre_begin, int pre_end, char *in, int in_begin, int in_end) { int len; int i; if(pre_begin > pre_end){ return; } *T = (BiTree *)malloc(sizeof(BiTree)); (*T)->data = pre[pre_begin]; for(i=in_begin; i<=in_end; i++){ if((*T)->data == in[i]){ break; } } len = i - in_begin; CreateByPreInOrder(&(*T)->left, pre, pre_begin+1, pre_begin+len, in, in_begin, i-1); CreateByPreInOrder(&(*T)->right, pre, pre_begin+1+len, pre_end, in, i+1, in_end); } void PreOrderTranvers(BiTree *T) { if(T){ printf(" %c(", T->data); PreOrderTranvers(T->left); printf(","); PreOrderTranvers(T->right); printf(")"); } } void main() { BiTree *T; char pre[] = {'A','B','C','D','E','F','G'}; char in[] = {'C','B','E','D','A','F','G'}; int n = sizeof(pre)/sizeof(char); int i; printf("log.anycle.com\n\n"); printf("Pre order sequence:\n"); for(i=0; i<n; i++){ printf(" %c", pre[i]); } printf("\n"); printf("In order sequence:\n"); for(i=0; i<n; i++){ printf(" %c", in[i]); } printf("\n"); printf("Create binary tree by pre and in order:\n"); CreateByPreInOrder(&T, pre, 0, n-1, in, 0, n-1); printf("\n"); printf("Pre order traverse:\n"); PreOrderTranvers(T); printf("\n"); }
日历
最新微语
- 有的时候,会站在分叉路口,不知道向左还是右
2023-12-26 15:34
- 繁花乱开,鸟雀逐风。心自宁静,纷扰不闻。
2023-03-14 09:56
- 对于不可控的事,我们保持乐观,对于可控的事情,我们保持谨慎。
2023-02-09 11:03
- 小时候,
暑假意味着无忧无虑地玩很长一段时间,
节假意味着好吃好喝还有很多长期不见的小朋友来玩...
长大后,
这是女儿第一个暑假,
一个半月...
2022-07-11 08:54
- Watching the autumn leaves falling as you grow older together
2018-10-25 09:45
分类
最新评论
- Goonog
i get it now :) - 萧
@Fluzak:The web host... - Fluzak
Nice blog here! Also... - Albertarive
In my opinion you co... - ChesterHep
What does it plan? - ChesterHep
No, opposite. - mojoheadz
Everything is OK!... - Josephmaigh
I just want to say t... - ChesterHep
What good topic - AnthonyBub
Certainly, never it ...
发表评论: