From 785482d7fba6d679ef28b7680579520d32356bee Mon Sep 17 00:00:00 2001 From: jf-home Date: Sun, 23 Jun 2024 17:57:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E6=B5=8Bqueue=E5=85=A5=E9=98=9F?= =?UTF-8?q?=E5=92=8C=E5=87=BA=E9=98=9F=E5=85=83=E7=B4=A0=E4=B8=BA=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E4=BD=93=E9=80=9A=E8=BF=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- src/tree.c | 37 ++++++++++++++ test/test.c | 4 +- test/test_queue.c | 117 ++++++++++++++++++++++++++++++++++++++++++-- test/tree_test.c | 8 +++ 5 files changed, 160 insertions(+), 8 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index ba73187..fade41a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "program": "${workspaceFolder}/build/release/bin/test.exe", "args": [], - "stopAtEntry": true, + "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, diff --git a/src/tree.c b/src/tree.c index 15793c8..22ff887 100644 --- a/src/tree.c +++ b/src/tree.c @@ -9,6 +9,7 @@ * */ #include "tree.h" +#include "queue.h" #if RAVLTREE == 1 // function declare @@ -1681,7 +1682,43 @@ void tree_avl_postorder(struct _tree* self, struct _tree_node* root) // traversal breadth void tree_avl_breadth(struct _tree* self, struct _tree_node* root) { + assert(self != NULL); + struct _tree_node* node = self->_root; + queue_t queue = queue_new(); + queue_init(queue, self->_obj_size); + if(node != NULL) + { + queue->push(queue, node); + while(!queue->empty(queue)) + { + queue->pop(queue, node); + if(!self->_right_priority) + { + if(node->left != NULL) + { + queue->push(queue, node->left); + } + if(node->right != NULL) + { + queue->push(queue, node->right); + } + } + else + { + if(node->right != NULL) + { + queue->push(queue, node->right); + } + if(node->left != NULL) + { + queue->push(queue, node->left); + } + } + self->print_obj(node->obj); + } + } + queue_free(queue); } bool tree_avl_init(struct _tree *self, uint32_t obj_size) diff --git a/test/test.c b/test/test.c index 2b4e99f..ec6e894 100644 --- a/test/test.c +++ b/test/test.c @@ -38,8 +38,8 @@ int main() // test_list(); // test_stack(); // test_deque(); - // test_queue(); - test_tree(); + test_queue(); + // test_tree(); // rbtree_test(); } diff --git a/test/test_queue.c b/test/test_queue.c index e11133b..4d082e5 100644 --- a/test/test_queue.c +++ b/test/test_queue.c @@ -142,7 +142,7 @@ static void test_queue_char(void) } printf("----- after pop -----\n"); - for (i = 0; i < len + 1; i++) + while(!queue.empty(&queue)) { if (true == queue.pop(&queue, &temp)) { @@ -163,15 +163,31 @@ static void test_queue_char(void) printf("size = %2d\n", queue.size(&queue)); } - else + } + + if (true == queue.pop(&queue, &temp)) + { + printf("pop = "); + queue.print_obj(&temp); + + if (true == queue.front(&queue, &temp)) { - printf("pop failed! because it is empty\n"); + printf("front = "); + queue.print_obj(&temp); } - if (queue.empty(&queue)) + if (queue.back(&queue, &temp)) { - printf("----- empty -----\n"); + printf("back = "); + queue.print_obj(&temp); } + + printf("size = %2d\n", queue.size(&queue)); + } + + if (queue.empty(&queue)) + { + printf("----- empty -----\n"); } printf("----- print -----\n"); @@ -181,8 +197,99 @@ static void test_queue_char(void) queue.destory(&queue); } +static void test_queue_struct(void) +{ + uint32_t i = 0; + struct _student data[] = { + {"zhao", 1001},{"qian", 1002}, {"sun", 1003}, {"li", 1004}, + "zhou", 1005, "wu", 1006, "zheng", 1007, "wang", 1008, + }; + struct _student temp; + uint32_t len = sizeof(data) / sizeof(data[0]) - 1; + + queue_t queue = queue_new(); + queue_init(queue, sizeof(struct _student)); + queue->print_obj = print_struct; + + printf("\n\n----- test_queue_struct -----\n"); + + printf("----- after push-----\n"); + for (i = 0; i < len; i++) + { + if(queue->push(queue, &data[i])) + { + queue->front(queue, &temp); + printf("front = "); + queue->print_obj(&temp); + + queue->back(queue, &temp); + printf("\tback = "); + queue->print_obj(&temp); + + printf("\tsize = %2d\n", queue->size(queue)); + } + } + printf("----- print -----\n"); + queue->print(queue); + printf("\n"); + + queue->clear(queue); + if (queue->empty(queue)) + { + printf("----- empty -----\n"); + } + + printf("----- push -----\n"); + for (i = 0; i < len; i++) + { + queue->push(queue, &data[i]); + } + + printf("----- after pop -----\n"); + while(!queue->empty(queue)) + { + if (true == queue->pop(queue, &temp)) + { + printf("pop = "); + queue->print_obj(&temp); + + if (true == queue->front(queue, &temp)) + { + printf("front = "); + queue->print_obj(&temp); + } + + if (queue->back(queue, &temp)) + { + printf("back = "); + queue->print_obj(&temp); + } + + printf("size = %2d\n", queue->size(queue)); + } + } + + if (!queue->pop(queue, &temp)) + { + printf("----- pop failed! -----\n"); + } + + if (queue->empty(queue)) + { + printf("----- empty -----\n"); + } + + printf("----- print -----\n"); + queue->print(queue); + printf("\n"); + + queue_free(queue); +} + + void test_queue(void) { test_queue_num(); test_queue_char(); + test_queue_struct(); } diff --git a/test/tree_test.c b/test/tree_test.c index e1dd2a9..9b3bd4e 100644 --- a/test/tree_test.c +++ b/test/tree_test.c @@ -299,6 +299,10 @@ void test_tree_num(void) tree->postorder(tree, tree->_root); printf("\n"); + printf("----- breadth -----\n"); + tree->breadth(tree, tree->_root); + printf("\n"); + printf("----- right priority -----\n"); tree->order(tree, true); @@ -314,6 +318,10 @@ void test_tree_num(void) tree->postorder(tree, tree->_root); printf("\n"); + printf("----- breadth -----\n"); + tree->breadth(tree, tree->_root); + printf("\n"); + tree_free(tree); }