change function and class name.

This commit is contained in:
LeviXubbbb 2023-07-24 02:36:08 +08:00
parent bcfb881a6a
commit 36584b95ce
5 changed files with 100 additions and 97 deletions

View File

@ -30,40 +30,40 @@ void TestHash(void)
HashTable table;
printf("Init Hash Table\n");
Init(&table);
HashTableInit(&table);
printf("Test Hash Table\n");
printf("Insert 1, 10\n");
Insert(&table, 1, 10);
HashTableInsert(&table, 1, 10);
printf("Insert 2, 20\n");
Insert(&table, 2, 20);
HashTableInsert(&table, 2, 20);
printf("Insert 3, 30\n");
Insert(&table, 3, 30);
HashTableInsert(&table, 3, 30);
printf("Insert 101, 40\n");
Insert(&table, 101, 40);
HashTableInsert(&table, 101, 40);
printf("Insert 102, 50\n");
Insert(&table, 102, 50);
HashTableInsert(&table, 102, 50);
printf("Insert 103, 60\n");
Insert(&table, 103, 60);
HashTableInsert(&table, 103, 60);
printf("Insert 104, 70\n");
Insert(&table, 104, 70);
HashTableInsert(&table, 104, 70);
printf("Search 1, result: %d\n", Search(&table, 1));
printf("Search 2, result: %d\n", Search(&table, 2));
printf("Search 3, result: %d\n", Search(&table, 3));
printf("Search 4, result: %d\n", Search(&table, 4));
printf("Search 101, result: %d\n", Search(&table, 101));
printf("Search 102, result: %d\n", Search(&table, 102));
printf("Search 103, result: %d\n", Search(&table, 103));
printf("Search 104, result: %d\n", Search(&table, 104));
printf("Search 1, result: %d\n", HashTableSearch(&table, 1));
printf("Search 2, result: %d\n", HashTableSearch(&table, 2));
printf("Search 3, result: %d\n", HashTableSearch(&table, 3));
printf("Search 4, result: %d\n", HashTableSearch(&table, 4));
printf("Search 101, result: %d\n", HashTableSearch(&table, 101));
printf("Search 102, result: %d\n", HashTableSearch(&table, 102));
printf("Search 103, result: %d\n", HashTableSearch(&table, 103));
printf("Search 104, result: %d\n", HashTableSearch(&table, 104));
printf("Delete 1\n");
Delete(&table, 1);
HashTableDelete(&table, 1);
printf("Search 1, result: %d\n", Search(&table, 1));
printf("Search 1, result: %d\n", HashTableSearch(&table, 1));
printf("Clear Table\n");
Clear(&table);
HashTableClear(&table);
return;
}

View File

@ -25,44 +25,46 @@
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 101
#define HASH_TABLE_SIZE 101
typedef struct Node{
typedef struct HashTableNode HashTableNode;
struct HashTableNode{
int key;
int value;
struct Node *next;
} Node;
HashTableNode *next;
};
typedef struct HashTable{
Node *table[TABLE_SIZE];
HashTableNode *table[HASH_TABLE_SIZE];
} HashTable;
unsigned int Hash(int key)
static unsigned int Hash(int key)
{
return key % TABLE_SIZE;
return key % HASH_TABLE_SIZE;
}
Node* CreateNode(int key, int value)
static HashTableNode* CreateNode(int key, int value)
{
Node *new_node = (Node*)malloc(sizeof(Node));
HashTableNode* new_node = (HashTableNode*)malloc(sizeof(HashTableNode));
new_node->key = key;
new_node->value = value;
new_node->next = NULL;
return new_node;
}
void Init(HashTable *hash_table)
void HashTableInit(HashTable *hash_table)
{
int i;
for(i = 0; i < TABLE_SIZE; i++)
for(i = 0; i < HASH_TABLE_SIZE; i++)
hash_table->table[i] = NULL;
}
void Insert(HashTable *hash_table, int key, int value)
void HashTableInsert(HashTable *hash_table, int key, int value)
{
unsigned int hash_value = Hash(key);
Node *node = hash_table->table[hash_value];
HashTableNode* node = hash_table->table[hash_value];
while(node != NULL && node->key != key)
node = node->next;
if(node != NULL){
@ -70,15 +72,15 @@ void Insert(HashTable *hash_table, int key, int value)
return;
}
Node *new_node = CreateNode(key, value);
HashTableNode* new_node = CreateNode(key, value);
new_node->next = hash_table->table[hash_value];
hash_table->table[hash_value] = new_node;
}
int Search(HashTable *hash_table, int key)
int HashTableSearch(HashTable *hash_table, int key)
{
unsigned int hash_value = Hash(key);
Node *node = hash_table->table[hash_value];
HashTableNode* node = hash_table->table[hash_value];
while(node != NULL && node->key != key)
node = node->next;
if(node == NULL)
@ -87,11 +89,11 @@ int Search(HashTable *hash_table, int key)
return node->value;
}
void Delete(HashTable *hash_table, int key)
void HashTableDelete(HashTable *hash_table, int key)
{
unsigned int hash_value = Hash(key);
Node *node = hash_table->table[hash_value];
Node *prev_node = NULL;
HashTableNode* node = hash_table->table[hash_value];
HashTableNode* prev_node = NULL;
while(node != NULL && node->key != key){
prev_node = node;
node = node->next;
@ -105,13 +107,13 @@ void Delete(HashTable *hash_table, int key)
free(node);
}
void Clear(HashTable *hash_table)
void HashTableClear(HashTable *hash_table)
{
int i;
for(i = 0; i < TABLE_SIZE; i++){
Node *node = hash_table->table[i];
for(i = 0; i < HASH_TABLE_SIZE; i++){
HashTableNode* node = hash_table->table[i];
while(node != NULL){
Node *temp = node;
HashTableNode* temp = node;
node = node->next;
free(temp);
}
@ -119,12 +121,12 @@ void Clear(HashTable *hash_table)
}
}
void PrintTable(HashTable *hash_table)
void HashTablePrint(HashTable *hash_table)
{
int i;
for(i = 0; i < TABLE_SIZE; i++){
for(i = 0; i < HASH_TABLE_SIZE; i++){
printf("Print %d\n", i);
Node *node = hash_table->table[i];
HashTableNode* node = hash_table->table[i];
while(node != NULL){
printf("key: %d, value: %d\n", node->key, node->value);
node = node->next;

View File

@ -27,7 +27,7 @@
void TestRBTree(void)
{
struct Node *root = NULL;
RBTreeNode* root = NULL;
printf("创建红黑树并且插入以下结点\n");
printf("key: 7, value: 7\n");
printf("key: 3, value: 3\n");
@ -49,21 +49,21 @@ void TestRBTree(void)
int values[] = {7, 3, 18, 10, 22, 8, 11, 26, 2, 6, 13, 20, 27, 1, 12, 14, 25};
for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++)
insertNode(&root, values[i], values[i]);
RBTreeInsert(&root, values[i], values[i]);
printf("插入创建完毕后中序遍历红黑树结果:\n");
inorderTraversal(root);
RBTreeInorderTraversal(root);
printf("\n");
printf("查找key值为 10 的结点\n");
struct Node *searchResult = searchNode(root, 10);
RBTreeNode* searchResult = RBTreeSearch(root, 10);
if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else
printf("未找到key值为 %d 的结点\n", 10);
printf("查找key值为 100 的结点\n");
searchResult = searchNode(root, 100);
searchResult = RBTreeSearch(root, 100);
if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else
@ -71,18 +71,18 @@ void TestRBTree(void)
printf("删除结点 10 和 22\n");
int deleteData = 10;
deleteNode(&root, 10);
deleteNode(&root, 22);
RBTreeDelete(&root, 10);
RBTreeDelete(&root, 22);
printf("查找key值为 10 的结点\n");
searchResult = searchNode(root, 10);
searchResult = RBTreeSearch(root, 10);
if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else
printf("未找到key值为 %d 的结点\n", 10);
printf("查找key值为 22 的结点\n");
searchResult = searchNode(root, 22);
searchResult = RBTreeSearch(root, 22);
if (searchResult != NULL)
printf("找到了key值为 %d 的结点, value值为 %d \n", searchResult->key, searchResult->value);
else
@ -90,10 +90,10 @@ void TestRBTree(void)
printf("删除结点 %d 后的中序遍历结果:\n", deleteData);
inorderTraversal(root);
RBTreeInorderTraversal(root);
printf("\n");
printf("清空红黑树并且释放内存\n");
clear(root);
RBTreeClear(root);
}
PRIV_SHELL_CMD_FUNCTION(TestRBTree, a red black tree test sample, PRIV_SHELL_CMD_MAIN_ATTR);

View File

@ -26,19 +26,20 @@
#include <string.h>
// define color of red-black tree
enum Color {RED, BLACK};
enum RBTreeColor {RED, BLACK};
typedef struct RBTreeNode RBTreeNode;
// define a node of red-black tree
struct Node {
struct RBTreeNode{
int key;
int value;
enum Color color;
struct Node *left, *right, *parent;
enum RBTreeColor color;
RBTreeNode *left, *right, *parent;
};
// create a new nodes
struct Node *createNode(int key, int value) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
static RBTreeNode *createNode(int key, int value) {
RBTreeNode *newNode = (RBTreeNode*)malloc(sizeof(RBTreeNode));
newNode->key = key;
newNode->value = value;
newNode->color = RED;
@ -47,8 +48,8 @@ struct Node *createNode(int key, int value) {
}
// red-black tree left rotation
void leftRotate(struct Node **root, struct Node *x) {
struct Node *y = x->right;
static void leftRotate(RBTreeNode **root, RBTreeNode *x) {
RBTreeNode *y = x->right;
x->right = y->left;
if (y->left != NULL)
y->left->parent = x;
@ -64,8 +65,8 @@ void leftRotate(struct Node **root, struct Node *x) {
}
// red-black tree right rotation
void rightRotate(struct Node **root, struct Node *y) {
struct Node *x = y->left;
static void rightRotate(RBTreeNode **root, RBTreeNode *y) {
RBTreeNode *x = y->left;
y->left = x->right;
if (x->right != NULL)
x->right->parent = y;
@ -81,16 +82,16 @@ void rightRotate(struct Node **root, struct Node *y) {
}
// red-black tree fix-up
void fixViolation(struct Node **root, struct Node *newNode) {
struct Node *parent = NULL;
struct Node *grandparent = NULL;
static void fixViolation(RBTreeNode **root, RBTreeNode *newNode) {
RBTreeNode *parent = NULL;
RBTreeNode *grandparent = NULL;
while ((newNode != *root) && (newNode->color != BLACK) && (newNode->parent->color == RED)) {
parent = newNode->parent;
grandparent = newNode->parent->parent;
if (parent == grandparent->left) {
struct Node *uncle = grandparent->right;
RBTreeNode *uncle = grandparent->right;
if (uncle != NULL && uncle->color == RED) {
grandparent->color = RED;
@ -105,13 +106,13 @@ void fixViolation(struct Node **root, struct Node *newNode) {
}
rightRotate(root, grandparent);
int temp = parent->color;
enum RBTreeColor temp = parent->color;
parent->color = grandparent->color;
grandparent->color = temp;
newNode = parent;
}
} else {
struct Node *uncle = grandparent->left;
RBTreeNode *uncle = grandparent->left;
if ((uncle != NULL) && (uncle->color == RED)) {
grandparent->color = RED;
@ -126,7 +127,7 @@ void fixViolation(struct Node **root, struct Node *newNode) {
}
leftRotate(root, grandparent);
int temp = parent->color;
enum RBTreeColor temp = parent->color;
parent->color = grandparent->color;
grandparent->color = temp;
newNode = parent;
@ -138,10 +139,10 @@ void fixViolation(struct Node **root, struct Node *newNode) {
}
// insert a node into red-black tree
void insertNode(struct Node **root, int key, int value) {
struct Node *newNode = createNode(key, value);
struct Node *current = *root;
struct Node *parent = NULL;
void RBTreeInsert(RBTreeNode **root, int key, int value) {
RBTreeNode* newNode = createNode(key, value);
RBTreeNode* current = *root;
RBTreeNode* parent = NULL;
while (current != NULL) {
parent = current;
@ -163,18 +164,18 @@ void insertNode(struct Node **root, int key, int value) {
}
// search a node in red-black tree
struct Node *searchNode(struct Node *root, int key) {
RBTreeNode* RBTreeSearch(RBTreeNode* root, int key) {
if (root == NULL || root->key == key)
return root;
if (root->key < key)
return searchNode(root->right, key);
return RBTreeSearch(root->right, key);
return searchNode(root->left, key);
return RBTreeSearch(root->left, key);
}
// find the minimum node in red-black tree
struct Node *minimumNode(struct Node *node) {
static RBTreeNode* minimumNode(RBTreeNode* node) {
while (node->left != NULL)
node = node->left;
@ -182,7 +183,7 @@ struct Node *minimumNode(struct Node *node) {
}
// replace a node with another node
void transplant(struct Node **root, struct Node *u, struct Node *v) {
static void transplant(RBTreeNode** root, RBTreeNode* u, RBTreeNode* v) {
if (u->parent == NULL)
*root = v;
else if (u == u->parent->left)
@ -195,8 +196,8 @@ void transplant(struct Node **root, struct Node *u, struct Node *v) {
}
// fix the delete violation in red-black tree
void fixDeleteViolation(struct Node **root, struct Node *node, struct Node *parent) {
struct Node *sibling = NULL;
static void fixDeleteViolation(RBTreeNode** root, RBTreeNode* node, RBTreeNode* parent) {
RBTreeNode* sibling = NULL;
while ((node != *root) && ((node == NULL) || (node->color == BLACK))) {
if (node == parent->left) {
@ -268,14 +269,14 @@ void fixDeleteViolation(struct Node **root, struct Node *node, struct Node *pare
// delete a node from red-black tree
void deleteNode(struct Node **root, int key) {
struct Node *nodeToDelete = searchNode(*root, key);
void RBTreeDelete(RBTreeNode** root, int key) {
RBTreeNode* nodeToDelete = RBTreeSearch(*root, key);
if (nodeToDelete == NULL)
return;
struct Node *child = NULL;
struct Node *parent = NULL;
enum Color originalColor = nodeToDelete->color;
RBTreeNode* child = NULL;
RBTreeNode* parent = NULL;
enum RBTreeColor originalColor = nodeToDelete->color;
if (nodeToDelete->left == NULL) {
child = nodeToDelete->right;
@ -286,7 +287,7 @@ void deleteNode(struct Node **root, int key) {
parent = nodeToDelete->parent;
transplant(root, nodeToDelete, nodeToDelete->left);
} else {
struct Node *successor = minimumNode(nodeToDelete->right);
RBTreeNode* successor = minimumNode(nodeToDelete->right);
originalColor = successor->color;
child = successor->right;
parent = successor->parent;
@ -311,19 +312,19 @@ void deleteNode(struct Node **root, int key) {
}
// inorder traversal
void inorderTraversal(struct Node *root) {
void RBTreeInorderTraversal(RBTreeNode* root) {
if (root != NULL) {
inorderTraversal(root->left);
RBTreeInorderTraversal(root->left);
printf("%d ", root->key);
inorderTraversal(root->right);
RBTreeInorderTraversal(root->right);
}
}
//clear the tree
void clear(struct Node *root) {
void RBTreeClear(RBTreeNode* root) {
if (root != NULL) {
clear(root->left);
clear(root->right);
RBTreeClear(root->left);
RBTreeClear(root->right);
free(root);
}
}

View File

@ -7,7 +7,7 @@ export APPLFLAGS := -mcmodel=medany -march=rv64imac -mabi=lp64 -nostartfiles -Wl
export CXXFLAGS := -mcmodel=medany -march=rv64imac -mabi=lp64 -fno-common -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -O0 -Wa,-g -ggdb -Werror
export CROSS_COMPILE ?=/opt/xpack-riscv-none-embed-gcc-8.3.0-1.2/bin/riscv-none-embed-
export CROSS_COMPILE ?=/opt/gnu-mcu-eclipse/riscv-none-gcc/8.2.0-2.1-20190425-1021/bin/riscv-none-embed-
export DEFINES := -DHAVE_CCONFIG_H -DHAVE_SIGINFO