2023_open_source_contest_warmup_1st_issue3 finish
This commit is contained in:
parent
36584b95ce
commit
7d8fffd0b3
|
@ -247,6 +247,8 @@ menu "test app"
|
||||||
menuconfig USER_TEST_RBTREE
|
menuconfig USER_TEST_RBTREE
|
||||||
bool "Config test rbtree"
|
bool "Config test rbtree"
|
||||||
default n
|
default n
|
||||||
|
menuconfig USER_TEST_RADIX_TREE
|
||||||
|
bool "Config test radix tree"
|
||||||
|
default n
|
||||||
endif
|
endif
|
||||||
endmenu
|
endmenu
|
||||||
|
|
|
@ -109,5 +109,9 @@ ifeq ($(CONFIG_ADD_XIZI_FETURES),y)
|
||||||
SRC_FILES += test_rbtree/test_rbtree.c
|
SRC_FILES += test_rbtree/test_rbtree.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_USER_TEST_RADIX_TREE),y)
|
||||||
|
SRC_FILES += test_radix_tree/test_radix_tree.c
|
||||||
|
endif
|
||||||
|
|
||||||
include $(KERNEL_ROOT)/compiler.mk
|
include $(KERNEL_ROOT)/compiler.mk
|
||||||
endif
|
endif
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
# **基于k210-emulator实现基数树并测试验证**
|
||||||
|
|
||||||
|
## 1. 简介
|
||||||
|
|
||||||
|
基于矽璓模拟器k210-emulator,实现基数树,并编写测试程序在shell终端打印结果
|
||||||
|
|
||||||
|
## 2. 数据结构设计说明
|
||||||
|
|
||||||
|
```c
|
||||||
|
struct RadixTreeNode{
|
||||||
|
char key;
|
||||||
|
int value;
|
||||||
|
RadixTreeNode* children[26];
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
每个节点存储一个字符串中的一个字母,同时存储他的后一个字符的26种可能情况(在这里仅考虑小写英文字母)
|
||||||
|
|
||||||
|
## 3. 测试程序说明
|
||||||
|
|
||||||
|
```c
|
||||||
|
void TestRadixTree(void)
|
||||||
|
{
|
||||||
|
RadixTreeNode* root = (RadixTreeNode*)malloc(sizeof(RadixTreeNode));
|
||||||
|
root->key = 0;
|
||||||
|
root->value = 0;
|
||||||
|
for(int i = 0; i < 26; i++)
|
||||||
|
root->children[i] = NULL;
|
||||||
|
printf("创建基数树并且插入以下结点\n");
|
||||||
|
printf("key: apple, value: 1\n");
|
||||||
|
printf("key: banana, value: 2\n");
|
||||||
|
printf("key: orange, value: 3\n");
|
||||||
|
printf("key: pear, value: 4\n");
|
||||||
|
printf("key: peach, value: 5\n");
|
||||||
|
printf("key: xiuos, value: 6\n");
|
||||||
|
RadixTreeInsert(root, "apple", 1);
|
||||||
|
RadixTreeInsert(root, "banana", 2);
|
||||||
|
RadixTreeInsert(root, "orange", 3);
|
||||||
|
RadixTreeInsert(root, "pear", 4);
|
||||||
|
RadixTreeInsert(root, "peach", 5);
|
||||||
|
RadixTreeInsert(root, "xiuos", 6);
|
||||||
|
printf("插入创建完毕后查询基数树结果:\n");
|
||||||
|
printf("若输入的内容是某些key的前缀则会返回value 0, 若输入的内容完全不匹配则返回value -1, 若输入的内容是某个key则会返回对应的value\n");
|
||||||
|
printf("Value of key \"apple\" is %d\n", RadixTreeSearch(root, "apple"));
|
||||||
|
printf("Value of key \"banana\" is %d\n", RadixTreeSearch(root, "banana"));
|
||||||
|
printf("Value of key \"orange\" is %d\n", RadixTreeSearch(root, "orange"));
|
||||||
|
printf("Value of key \"pear\" is %d\n", RadixTreeSearch(root, "pear"));
|
||||||
|
printf("Value of key \"peach\" is %d\n", RadixTreeSearch(root, "peach"));
|
||||||
|
printf("Value of key \"xiuos\" is %d\n", RadixTreeSearch(root, "xiuos"));
|
||||||
|
printf("Value of key \"app\" is %d\n", RadixTreeSearch(root, "app"));
|
||||||
|
printf("Value of key \"xiuoss\" is %d\n", RadixTreeSearch(root, "xiuoss"));
|
||||||
|
printf("测试结束, 清空基数树\n");
|
||||||
|
RadixTreeClear(root);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
创建一个基数树并插入一些字符串作为key,以及它的相对应的value值(这里仅考虑value为int)。然后进行查找。若查找的key是某些key的前缀,则返回0,若查找的key不存在且不是某些key的前缀则返回-1.
|
||||||
|
|
||||||
|
## 4. 运行结果(##需结合运行测试截图按步骤说明##)
|
||||||
|
|
||||||
|
首先启动k210-emulator
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
然后运行TestRadixTree指令进行测试
|
||||||
|
|
||||||
|

|
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 AIIT XUOS Lab
|
||||||
|
* XiUOS is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file: test_radix_tree.c
|
||||||
|
* @brief: a application to test radix tree and print result
|
||||||
|
* @version: 1.1
|
||||||
|
* @author: LeviXubbbb(Bin Xu)
|
||||||
|
* @date: 2023/07/26
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <transform.h>
|
||||||
|
#include "test_radix_tree.h"
|
||||||
|
#ifdef ADD_XIZI_FETURES
|
||||||
|
|
||||||
|
void TestRadixTree(void)
|
||||||
|
{
|
||||||
|
RadixTreeNode* root = (RadixTreeNode*)malloc(sizeof(RadixTreeNode));
|
||||||
|
root->key = 0;
|
||||||
|
root->value = 0;
|
||||||
|
for(int i = 0; i < 26; i++)
|
||||||
|
root->children[i] = NULL;
|
||||||
|
printf("创建基数树并且插入以下结点\n");
|
||||||
|
printf("key: apple, value: 1\n");
|
||||||
|
printf("key: banana, value: 2\n");
|
||||||
|
printf("key: orange, value: 3\n");
|
||||||
|
printf("key: pear, value: 4\n");
|
||||||
|
printf("key: peach, value: 5\n");
|
||||||
|
printf("key: xiuos, value: 6\n");
|
||||||
|
RadixTreeInsert(root, "apple", 1);
|
||||||
|
RadixTreeInsert(root, "banana", 2);
|
||||||
|
RadixTreeInsert(root, "orange", 3);
|
||||||
|
RadixTreeInsert(root, "pear", 4);
|
||||||
|
RadixTreeInsert(root, "peach", 5);
|
||||||
|
RadixTreeInsert(root, "xiuos", 6);
|
||||||
|
printf("插入创建完毕后查询基数树结果:\n");
|
||||||
|
printf("若输入的内容是某些key的前缀则会返回value 0, 若输入的内容完全不匹配则返回value -1, 若输入的内容是某个key则会返回对应的value\n");
|
||||||
|
printf("Value of key \"apple\" is %d\n", RadixTreeSearch(root, "apple"));
|
||||||
|
printf("Value of key \"banana\" is %d\n", RadixTreeSearch(root, "banana"));
|
||||||
|
printf("Value of key \"orange\" is %d\n", RadixTreeSearch(root, "orange"));
|
||||||
|
printf("Value of key \"pear\" is %d\n", RadixTreeSearch(root, "pear"));
|
||||||
|
printf("Value of key \"peach\" is %d\n", RadixTreeSearch(root, "peach"));
|
||||||
|
printf("Value of key \"xiuos\" is %d\n", RadixTreeSearch(root, "xiuos"));
|
||||||
|
printf("Value of key \"app\" is %d\n", RadixTreeSearch(root, "app"));
|
||||||
|
printf("Value of key \"xiuoss\" is %d\n", RadixTreeSearch(root, "xiuoss"));
|
||||||
|
printf("测试结束, 清空基数树\n");
|
||||||
|
RadixTreeClear(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
PRIV_SHELL_CMD_FUNCTION(TestRadixTree, a radix tree test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||||
|
#endif
|
|
@ -0,0 +1,90 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 AIIT XUOS Lab
|
||||||
|
* XiUOS is licensed under Mulan PSL v2.
|
||||||
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||||
|
* You may obtain a copy of Mulan PSL v2 at:
|
||||||
|
* http://license.coscl.org.cn/MulanPSL2
|
||||||
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||||
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||||
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||||
|
* See the Mulan PSL v2 for more details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file: test_radix_tree.h
|
||||||
|
* @brief: a header file to implement radix tree
|
||||||
|
* @version: 1.1
|
||||||
|
* @author: LeviXubbbb(Bin Xu)
|
||||||
|
* @date: 2023/07/26
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef XIUOS_TEST_RADIX_TREE_H
|
||||||
|
#define XIUOS_TEST_RADIX_TREE_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct RadixTreeNode RadixTreeNode;
|
||||||
|
|
||||||
|
struct RadixTreeNode{
|
||||||
|
char key;
|
||||||
|
int value;
|
||||||
|
RadixTreeNode* children[26];
|
||||||
|
};
|
||||||
|
|
||||||
|
static RadixTreeNode* createNode(char key, int value){
|
||||||
|
RadixTreeNode* node = (RadixTreeNode*)malloc(sizeof(RadixTreeNode));
|
||||||
|
node->key = key;
|
||||||
|
node->value = value;
|
||||||
|
for(int i = 0; i < 26; i++){
|
||||||
|
node->children[i] = NULL;
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RadixTreeInsert(RadixTreeNode* root, char* key, int value){
|
||||||
|
RadixTreeNode* node = root;
|
||||||
|
int len = strlen(key);
|
||||||
|
for(int i = 0; i < len; i++){
|
||||||
|
int index = key[i] - 'a';
|
||||||
|
if(index < 0 || index > 25){
|
||||||
|
printf("Invalid key! Please use lowercase letters!\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(node->children[index] == NULL){
|
||||||
|
node->children[index] = createNode(key[i], 0);
|
||||||
|
}
|
||||||
|
node = node->children[index];
|
||||||
|
}
|
||||||
|
node->value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int RadixTreeSearch(RadixTreeNode* root, char* key){
|
||||||
|
RadixTreeNode* node = root;
|
||||||
|
int len = strlen(key);
|
||||||
|
for(int i = 0; i < len; i++){
|
||||||
|
int index = key[i] - 'a';
|
||||||
|
if(index < 0 || index > 25){
|
||||||
|
printf("Invalid key! Please use lowercase letters!\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if(node->children[index] == NULL){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
node = node->children[index];
|
||||||
|
}
|
||||||
|
return node->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RadixTreeClear(RadixTreeNode* root){
|
||||||
|
if(root == NULL){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(int i = 0; i < 26; i++){
|
||||||
|
RadixTreeClear(root->children[i]);
|
||||||
|
}
|
||||||
|
free(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //XIUOS_TEST_RADIX_TREE_H
|
Loading…
Reference in New Issue