热身赛一级赛题基数树
This commit is contained in:
parent
1a6ee0234b
commit
6f0d087f83
|
@ -11,22 +11,219 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
// #include <user_api.h>
|
// #include <user_api.h>
|
||||||
#include <transform.h>
|
#include <transform.h>
|
||||||
|
#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;i<NODE_SIZE;i++)
|
||||||
|
{
|
||||||
|
n->next[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;i<len;i++)
|
||||||
|
{
|
||||||
|
uint8_t ch=(uint8_t)key[i];
|
||||||
|
if(p->next[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;i<len;i++)
|
||||||
|
{
|
||||||
|
uint8_t ch=(uint8_t)key[i];
|
||||||
|
if(p->next[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;i<len;i++)
|
||||||
|
{
|
||||||
|
uint8_t ch=(uint8_t)key[i];
|
||||||
|
if((*p)->next[ch]==NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
p=&((*p)->next[ch]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if((*p)->value==NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
(*p)->value=NULL;
|
||||||
|
|
||||||
|
int has_next=0;
|
||||||
|
for(int i=0;i<NODE_SIZE;i++)
|
||||||
|
{
|
||||||
|
if((*p)->next[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;i<NODE_SIZE;i++)
|
||||||
|
{
|
||||||
|
DestroyTree(root->next[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<num-1;i++)
|
||||||
|
{
|
||||||
|
InsertNode(root,keys[i],&values[i]);
|
||||||
|
}
|
||||||
|
for(int i=0;i<num;i++)
|
||||||
|
{
|
||||||
|
int* findValue=(int*)FindNode(root,keys[i]);
|
||||||
|
if(findValue)
|
||||||
|
{
|
||||||
|
printf("key=\"%s\",value=%d\n",keys[i],*findValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("key=\"%s\" not found\n",keys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Delete \"apple\" and \"world\":\n");
|
||||||
|
DeleteNode(root,keys[2]);
|
||||||
|
DeleteNode(root,keys[6]);
|
||||||
|
for(int i=0;i<num;i++)
|
||||||
|
{
|
||||||
|
int* findValue=(int*)FindNode(root,keys[i]);
|
||||||
|
if(findValue)
|
||||||
|
{
|
||||||
|
printf("key=\"%s\",value=%d\n",keys[i],*findValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("key=\"%s\" not found\n",keys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Insert \"apple\" and \"123456\":\n");
|
||||||
|
InsertNode(root,keys[2],&values[2]);
|
||||||
|
InsertNode(root,keys[7],&values[7]);
|
||||||
|
for(int i=0;i<num;i++)
|
||||||
|
{
|
||||||
|
int* findValue=(int*)FindNode(root,keys[i]);
|
||||||
|
if(findValue)
|
||||||
|
{
|
||||||
|
printf("key=\"%s\",value=%d\n",keys[i],*findValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("key=\"%s\" not found\n",keys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
printf("Destroy radix tree:\n");
|
||||||
|
DestroyTree(root);
|
||||||
|
root=NULL;
|
||||||
|
for(int i=0;i<num;i++)
|
||||||
|
{
|
||||||
|
int* findValue=(int*)FindNode(root,keys[i]);
|
||||||
|
if(findValue)
|
||||||
|
{
|
||||||
|
printf("key=\"%s\",value=%d\n",keys[i],*findValue);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("key=\"%s\" not found\n",keys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
extern int FrameworkInit();
|
|
||||||
extern void ApplicationOtaTaskInit(void);
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
printf("Hello, world! \n");
|
printf("Hello World!!!\n");
|
||||||
FrameworkInit();
|
|
||||||
#ifdef APPLICATION_OTA
|
|
||||||
ApplicationOtaTaskInit();
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PRIV_SHELL_CMD_FUNCTION(TestRadixTree, Implement a simple radix tree, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||||
// int cppmain(void);
|
// int cppmain(void);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue