[Data structures]Digital search tree - Double link tree
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> typedef struct _DLTree{ char symbol; // key struct _DLTree *next; int kind; // 0-leaf; 1-branch union{ char *infoptr; //Pointer to the data struct _DLTree *first; }; }DLTree; void *SearchDLTree(DLTree *T, char *keys, DLTree **f, int *idx) { int i = 0; DLTree *p = T->first; int n = strlen(keys); *f = T; *idx = i; while(p && i< n ){ while(p && p->symbol != keys[i]){ p = p->next; } if(p && i<n-1 ){ *f = p; *idx = i+1; p = p->first; } i++; } if(!p){ return NULL; }else{ return p->infoptr; } } void InsertDLTree(DLTree *T, char *keys) { DLTree *F; DLTree *p; int ret = 0; int i; int n = strlen(keys); while( !SearchDLTree(T, keys, &F, &i) ){ p = (DLTree *)malloc(sizeof(DLTree)); p->symbol = keys[i]; p->next = NULL; if(i==n-1){ p->kind = 0; p->infoptr = keys; }else{ p->kind = 1; p->first = NULL; } if(!F->first){ F->first = p; }else{ F = F->first; while(F->next){ F = F->next; } F->next = p; } } } void main() { char seq1[] = "aboutge$"; char seq2[] = "hello$"; char seq3[] = "world$"; char seq4[] = "again$"; char seq5[] = "help$"; int i; DLTree *F; DLTree T; T.next = NULL; T.kind = 1; T.symbol = 0; T.first = NULL; printf("log.anycle.com\n\n"); printf("Original data:\n"); printf("%s", seq1); printf("%s", seq2); printf("%s", seq3); printf("%s", seq4); printf("%s", seq5); printf("\n"); printf("Insert double links tree:\n"); InsertDLTree(&T, seq1); InsertDLTree(&T, seq2); InsertDLTree(&T, seq3); InsertDLTree(&T, seq4); InsertDLTree(&T, seq5); printf("Print the tree:\n"); printf("%s", (char *)SearchDLTree(&T, seq2, &F, &i) ); 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 ...
发表评论: