diff --git a/APP_Framework/Applications/main.c b/APP_Framework/Applications/main.c index e1de024b5..e0b12d24e 100644 --- a/APP_Framework/Applications/main.c +++ b/APP_Framework/Applications/main.c @@ -11,22 +11,219 @@ */ #include +#include #include // #include #include +#define NODE_SIZE 128 +#define MAX_WORD_LEN 128 +typedef struct node +{ + void* value; + struct node* next[NODE_SIZE]; +} NODE; + +NODE* CreateNode() +{ + NODE* n=(NODE*)malloc(sizeof(NODE)); + n->value=NULL; + for(int i=0;inext[i]=NULL; + } + return n; +} + +void InsertNode(NODE* root, const char* key, void* value) +{ + if(root==NULL) + { + return; + } + + NODE* p=root; + size_t len=strlen(key); + for(size_t i=0;inext[ch]==NULL) + { + p->next[ch]=CreateNode(); + } + p=p->next[ch]; + } + p->value=value; +} + +void* FindNode(NODE* root,const char* key) +{ + if(root==NULL) + { + return NULL; + } + + size_t len=strlen(key); + NODE* p=root; + for(size_t i=0;inext[ch]==NULL) + { + return NULL; + } + p=p->next[ch]; + } + return p->value; +} + +void DeleteNode(NODE* root,const char* key) +{ + if(root==NULL) + { + return; + } + + size_t len=strlen(key); + NODE** p=&root; + for(size_t i=0;inext[ch]==NULL) + { + return; + } + p=&((*p)->next[ch]); + } + + if((*p)->value==NULL) + { + return; + } + (*p)->value=NULL; + + int has_next=0; + for(int i=0;inext[i]!=NULL) + { + has_next=1; + break; + } + } + if(has_next==0) + { + free(*p); + (*p)=NULL; + } +} + +void DestroyTree(NODE* root) +{ + if(root==NULL) + { + return; + } + + for(int i=0;inext[i]); + } + free(root); +} + +void TestRadixTree() +{ + char keys[][MAX_WORD_LEN]={"a","app","apple","b","big","hello","world","123456"}; + int values[]={1,2,3,4,5,6,7,8}; + + printf("Create radix tree:\n"); + NODE* root=CreateNode(); + if(!root) + { + printf("Create node failed.\n"); + } + printf("\n"); + + printf("Insert nodes' key and value:\n"); + int num=sizeof(keys)/sizeof(keys[0]); + for(int i=0;i