From ae5ac856030079619a643923d822016c3a22ac9f Mon Sep 17 00:00:00 2001 From: iridiumR Date: Fri, 3 Dec 2021 15:33:31 +0800 Subject: [PATCH] =?UTF-8?q?9=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex9/BST.hpp | 152 ++++++++++++++++++++++++++++++++++++++++++++++------ ex9/ex9.cpp | 14 ++++- 2 files changed, 148 insertions(+), 18 deletions(-) diff --git a/ex9/BST.hpp b/ex9/BST.hpp index 53f25e2..757a59f 100644 --- a/ex9/BST.hpp +++ b/ex9/BST.hpp @@ -1,8 +1,9 @@ #include "bintree.hpp" #include "stack.hpp" -#include #include #include +#include +#include class BST : binTree { @@ -10,6 +11,10 @@ private: binNodeArray(int) p; public: + ~BST() + { + + } BST(int num) { srand(time(NULL)); @@ -70,23 +75,7 @@ public: } } } - void trav_level() - { - Queue q; - q.enqueue(root()); - while (!q.empty()) - { - binNodeArray(int) node = q.dequeue(); - std::cout << node->pos; - printf("-%02d:%02d \n", node->data / 60, node->data % 60); - - if (hasLC(node)) - q.enqueue(node->lc); - if (hasRC(node)) - q.enqueue(node->rc); - } - } bool searchPath(binNodeArray(int) root, binNodeArray(int) target, Stack *s) { if (root == NULL) @@ -107,4 +96,133 @@ public: s->pop(); return flag; } + + //层次遍历 + void trav_level() + { + Queue q; + int depth = 0; + int old_depth = 0; + q.enqueue(root()); + while (!q.empty()) + { + binNodeArray(int) node = q.dequeue(); + + old_depth = depth; + depth = node->pos.size(); + if (old_depth != depth) + printf("\n"); + + // std::cout << node->pos; + printf("%02d:%02d ", node->data / 60, node->data % 60); + + if (hasLC(node)) + q.enqueue(node->lc); + if (hasRC(node)) + q.enqueue(node->rc); + } + } + + void search_best(int target) + { + Queue q; + + int minus = 1440; + binNodeArray(int) target_p; + q.enqueue(root()); + while (!q.empty()) + { + binNodeArray(int) node = q.dequeue(); + + + if(minus>abs(target-node->data)) + { + minus = abs(target - node->data); + target_p = node; + } + + if (hasLC(node)) + q.enqueue(node->lc); + if (hasRC(node)) + q.enqueue(node->rc); + } + std::cout << target_p->pos; + printf("-%02d:%02d \n", target_p->data / 60, target_p->data % 60); + } + + //先序遍历 + void trav_pre() + { + pre(root()); + } + + //后序遍历 + void trav_post() + { + post(root()); + } + + //中序遍历 + void trav_in() + { + in(root()); + } + +protected: + void pre(binNodeArray(int) node) + { + if (!node) + return; + + std::cout << node->pos; + printf("-%02d:%02d \n", node->data / 60, node->data % 60); + + pre(node->lc); + pre(node->rc); + } + + void post(binNodeArray(int) node) + { + if (!node) + return; + + pre(node->lc); + pre(node->rc); + + std::cout << node->pos; + printf("-%02d:%02d \n", node->data / 60, node->data % 60); + } + + void in(binNodeArray(int) node) + { + if (!node) + return; + + pre(node->lc); + + std::cout << node->pos; + printf("-%02d:%02d \n", node->data / 60, node->data % 60); + pre(node->rc); + } + +public: + int height() + { + Queue q; + q.enqueue(root()); + int depth; + while (!q.empty()) + { + binNodeArray(int) node = q.dequeue(); + + depth = node->pos.size(); + + if (hasLC(node)) + q.enqueue(node->lc); + if (hasRC(node)) + q.enqueue(node->rc); + } + + return depth; + } }; \ No newline at end of file diff --git a/ex9/ex9.cpp b/ex9/ex9.cpp index c18608f..5d5fa74 100644 --- a/ex9/ex9.cpp +++ b/ex9/ex9.cpp @@ -5,8 +5,20 @@ int main() { printf("初始化\n"); BST bst(31); - printf("层次遍历\n"); + + printf("========层次遍历========\n"); bst.trav_level(); + printf("\n========先序遍历========\n"); + bst.trav_pre(); + + printf("========中序遍历========\n"); + bst.trav_in(); + + printf("========后序遍历========\n"); + bst.trav_post(); + + printf("高度为%d\n",bst.height()); + return 0; } \ No newline at end of file