Update test_radix_tree.c
This commit is contained in:
parent
95e5dbb04a
commit
8a61313c4c
|
@ -1,43 +1,31 @@
|
||||||
/**
|
|
||||||
* @file: test_radix_tree.c
|
|
||||||
* @brief: Implement a simple radix tree
|
|
||||||
* @version: 1.0
|
|
||||||
* @date: 2023/5/24
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <transform.h>
|
#include <transform.h>
|
||||||
#include "test_radix_tree.h"
|
#include "test_radix_tree.h"
|
||||||
|
|
||||||
/**
|
// 定义节点结构
|
||||||
* @description: Create a radix tree node
|
typedef struct Node {
|
||||||
* @return node pointer
|
void *value; // 存储值
|
||||||
*/
|
struct Node *next[256]; // 子节点数组,每个元素对应一个字节的所有可能值
|
||||||
node* CreateNode()
|
} node;
|
||||||
{
|
|
||||||
node* n = (node*)malloc(sizeof(node));
|
// 创建并初始化节点
|
||||||
|
node *CreateNode() {
|
||||||
|
node *n = (node *) malloc(sizeof(node));
|
||||||
n->value = NULL;
|
n->value = NULL;
|
||||||
for (int i = 0; i < NODE_SIZE; i++) {
|
for (int i = 0; i < 256; i++) { // 256表示所有可能的字节值
|
||||||
n->next[i] = NULL;
|
n->next[i] = NULL;
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 插入键值对到树中
|
||||||
* @description: Insert a new node to radix tree
|
void InsertNode(node *root, const char *key, void *value) {
|
||||||
* @param root - radix tree root
|
|
||||||
* @param key - new node key
|
|
||||||
* @param value - new node value
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
void InsertNode(node* root, const char* key, void* value)
|
|
||||||
{
|
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
node* cur = root;
|
node *cur = root;
|
||||||
size_t len = strlen(key);
|
size_t len = strlen(key);
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
uint8_t b = (uint8_t)key[i];
|
uint8_t b = (uint8_t) key[i];
|
||||||
if (cur->next[b] == NULL) {
|
if (cur->next[b] == NULL) {
|
||||||
cur->next[b] = CreateNode();
|
cur->next[b] = CreateNode();
|
||||||
}
|
}
|
||||||
|
@ -46,21 +34,15 @@ void InsertNode(node* root, const char* key, void* value)
|
||||||
cur->value = value;
|
cur->value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 删除键值对
|
||||||
* @description: Delete a node from radix tree
|
void DeleteNode(node *root, const char *key) {
|
||||||
* @param root - radix tree root
|
|
||||||
* @param key - key which is needed to delete
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
void DeleteNode(node* root, const char* key)
|
|
||||||
{
|
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
node** cur = &root;
|
node **cur = &root;
|
||||||
size_t len = strlen(key);
|
size_t len = strlen(key);
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
uint8_t b = (uint8_t)key[i];
|
uint8_t b = (uint8_t) key[i];
|
||||||
if ((*cur)->next[b] == NULL) {
|
if ((*cur)->next[b] == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -74,7 +56,7 @@ void DeleteNode(node* root, const char* key)
|
||||||
(*cur)->value = NULL;
|
(*cur)->value = NULL;
|
||||||
|
|
||||||
int has_children = 0;
|
int has_children = 0;
|
||||||
for (int i = 0; i < NODE_SIZE; i++) {
|
for (int i = 0; i < 256; i++) { // 256表示所有可能的字节值
|
||||||
if ((*cur)->next[i] != NULL) {
|
if ((*cur)->next[i] != NULL) {
|
||||||
has_children = 1;
|
has_children = 1;
|
||||||
break;
|
break;
|
||||||
|
@ -86,21 +68,15 @@ void DeleteNode(node* root, const char* key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 查找键对应的值
|
||||||
* @description: find a node by key
|
void *FindNode(node *root, const char *key) {
|
||||||
* @param root - radix tree root
|
|
||||||
* @param key - key which is needed to find
|
|
||||||
* @return value pointer corresponding to key
|
|
||||||
*/
|
|
||||||
void* FindNode(node* root, const char* key)
|
|
||||||
{
|
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
node* cur = root;
|
node *cur = root;
|
||||||
size_t len = strlen(key);
|
size_t len = strlen(key);
|
||||||
for (size_t i = 0; i < len; i++) {
|
for (size_t i = 0; i < len; i++) {
|
||||||
uint8_t b = (uint8_t)key[i];
|
uint8_t b = (uint8_t) key[i];
|
||||||
if (cur->next[b] == NULL) {
|
if (cur->next[b] == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -109,38 +85,33 @@ void* FindNode(node* root, const char* key)
|
||||||
return cur->value;
|
return cur->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// 销毁整个树
|
||||||
* @description: Destroy the radix tree
|
void DestroyTree(node *root) {
|
||||||
* @param root - radix tree root
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
void DestroyTree(node* root)
|
|
||||||
{
|
|
||||||
if (root == NULL) {
|
if (root == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < NODE_SIZE; i++) {
|
for (int i = 0; i < 256; i++) { // 256表示所有可能的字节值
|
||||||
DestroyTree(root->next[i]);
|
DestroyTree(root->next[i]);
|
||||||
}
|
}
|
||||||
free(root);
|
free(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestRadix()
|
// 测试函数
|
||||||
{
|
void TestRadix() {
|
||||||
char keys[][MAX_WORD_LEN] = {
|
char keys[][MAX_WORD_LEN] = {
|
||||||
"what",
|
"what",
|
||||||
"where",
|
"where",
|
||||||
"why",
|
"why",
|
||||||
"how",
|
"how",
|
||||||
"hello!",
|
"hello!",
|
||||||
"apple",
|
"apple",
|
||||||
"12345"
|
"12345"
|
||||||
};
|
};
|
||||||
int values[] = {1, 2, 3, 4, 5, 6, 7};
|
int values[] = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
|
||||||
printf("\nCreate tree and add key & value:\n");
|
printf("\n创建树并添加键值对:\n");
|
||||||
node* root = CreateNode();
|
node *root = CreateNode();
|
||||||
if (!root) printf("Create node failed.\n");
|
if (!root) printf("创建节点失败.\n");
|
||||||
|
|
||||||
int num = sizeof(keys) / sizeof(keys[0]);
|
int num = sizeof(keys) / sizeof(keys[0]);
|
||||||
for (int i = 0; i < num - 1; ++i) {
|
for (int i = 0; i < num - 1; ++i) {
|
||||||
|
@ -148,40 +119,42 @@ void TestRadix()
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < num; ++i) {
|
for (int i = 0; i < num; ++i) {
|
||||||
int* v = (int*)FindNode(root, keys[i]);
|
int *v = (int *) FindNode(root, keys[i]);
|
||||||
if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
if (v) printf("keys[%d] \"%s\" 的值 = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
||||||
else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
|
else printf("keys[%d] \"%s\" 未找到\n", i, keys[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nDelete \"where\" and \"how\":\n");
|
printf("\n删除 \"where\" 和 \"how\":\n");
|
||||||
DeleteNode(root, keys[1]);
|
DeleteNode(root, keys[1]);
|
||||||
DeleteNode(root, keys[3]);
|
DeleteNode(root, keys[3]);
|
||||||
|
|
||||||
for (int i = 0; i < num; ++i) {
|
for (int i = 0; i < num; ++i) {
|
||||||
int* v = (int*)FindNode(root, keys[i]);
|
int *v = (int *) FindNode(root, keys[i]);
|
||||||
if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
if (v) printf("keys[%d] \"%s\" 的值 = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
||||||
else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
|
else printf("keys[%d] \"%s\" 未找到\n", i, keys[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nInsert \"where\" and \"12345\":\n");
|
printf("\n插入 \"where\" 和 \"12345\":\n");
|
||||||
InsertNode(root, keys[1], &values[1]);
|
InsertNode(root, keys[1], &values[1]);
|
||||||
InsertNode(root, keys[6], &values[6]);
|
InsertNode(root, keys[6], &values[6]);
|
||||||
|
|
||||||
for (int i = 0; i < num; ++i) {
|
for (int i = 0; i < num; ++i) {
|
||||||
int* v = (int*)FindNode(root, keys[i]);
|
int *v = (int *) FindNode(root, keys[i]);
|
||||||
if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
if (v) printf("keys[%d] \"%s\" 的值 = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
||||||
else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
|
else printf("keys[%d] \"%s\" 未找到\n", i, keys[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nDestroy tree:\n");
|
printf("\n销毁树:\n");
|
||||||
DestroyTree(root);
|
DestroyTree(root);
|
||||||
root = NULL;
|
root = NULL;
|
||||||
|
|
||||||
for (int i = 0; i < num; ++i) {
|
for (int i = 0; i < num; ++i) {
|
||||||
int* v = (int*)FindNode(root, keys[i]);
|
int *v = (int *) FindNode(root, keys[i]);
|
||||||
if (v) printf("keys[%d] \"%s\"'v = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
if (v) printf("keys[%d] \"%s\" 的值 = %d, values[%d] = %d\n", i, keys[i], *v, i, values[i]);
|
||||||
else printf("keys[%d] \"%s\" not found\n", i, keys[i]);
|
else printf("keys[%d] \"%s\" 未找到\n", i, keys[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
PRIV_SHELL_CMD_FUNCTION(TestRadix, Implement a simple radix tree, PRIV_SHELL_CMD_MAIN_ATTR);
|
// 注册为特权Shell命令
|
||||||
|
PRIV_SHELL_CMD_FUNCTION(TestRadix, 实现一个简单的基数树, PRIV_SHELL_CMD_MAIN_ATTR
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in New Issue