From 9b43dd8f8858205ca4e3469a2521325d2728ae16 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Fri, 17 Dec 2021 16:38:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E6=A0=B7=E5=90=A7=E3=80=82=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex10/BST.hpp | 228 -------------------------------------------------- ex10/ex10.cpp | 2 +- ex9/BST.hpp | 118 +++++++++++++++++++------- ex9/ex9.cpp | 2 + 4 files changed, 93 insertions(+), 257 deletions(-) delete mode 100644 ex10/BST.hpp diff --git a/ex10/BST.hpp b/ex10/BST.hpp deleted file mode 100644 index 757a59f..0000000 --- a/ex10/BST.hpp +++ /dev/null @@ -1,228 +0,0 @@ -#include "bintree.hpp" -#include "stack.hpp" -#include -#include -#include -#include - -class BST : binTree -{ -private: - binNodeArray(int) p; - -public: - ~BST() - { - - } - BST(int num) - { - srand(time(NULL)); - int temp = rand() % 1440; - _root->data = temp; - _root->pos = "root"; - std::cout << _root->pos; - printf("-%02d:%02d \n", temp / 60, temp % 60); - - for (int i = 0; i < num - 1; i++) - { - temp = rand() % 1440; - p = root(); - std::string position; - while (temp != -1) - { - if (temp < (p->data)) - { - if (hasLC(p)) - { - p = p->lc; - position.append("0"); - } - - else - { - position.append("0"); - addLC(p, temp); - p->lc->pos = position; - std::cout << position; - printf("-%02d:%02d \n", temp / 60, temp % 60); - temp = -1; - } - } - else if (temp > (p->data)) - { - if (hasRC(p)) - { - p = p->rc; - position.append("1"); - } - - else - { - position.append("1"); - addRC(p, temp); - p->rc->pos = position; - std::cout << position; - printf("-%02d:%02d \n", temp / 60, temp % 60); - temp = -1; - } - } - else - { - i--; - temp = -1; - } - } - } - } - - bool searchPath(binNodeArray(int) root, binNodeArray(int) target, Stack *s) - { - if (root == NULL) - return false; - s->push(root); - if (root->data == target->data) - return true; - - bool flag = false; - //先去左子树找 - if (root->lc != NULL) - flag = searchPath(root->lc, target, s); - //左子树找不到并且右子树不为空的情况下才去找 - if (!flag && root->rc != NULL) - flag = searchPath(root->rc, target, s); - //左右都找不到,弹出栈顶元素 - if (!flag) - 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/ex10/ex10.cpp b/ex10/ex10.cpp index 4fcb7e9..a04b368 100644 --- a/ex10/ex10.cpp +++ b/ex10/ex10.cpp @@ -1,4 +1,4 @@ -#include "BST.hpp" +#include "../ex9/BST.hpp" #include int main() diff --git a/ex9/BST.hpp b/ex9/BST.hpp index 757a59f..76731b2 100644 --- a/ex9/BST.hpp +++ b/ex9/BST.hpp @@ -1,9 +1,9 @@ #include "bintree.hpp" #include "stack.hpp" +#include #include #include #include -#include class BST : binTree { @@ -13,7 +13,6 @@ private: public: ~BST() { - } BST(int num) { @@ -125,29 +124,37 @@ public: void search_best(int target) { - Queue q; - - int minus = 1440; - binNodeArray(int) target_p; - q.enqueue(root()); - while (!q.empty()) + binNodeArray(int) node = root(); + int old_data = 0; + Stack S; + while (1) { - binNodeArray(int) node = q.dequeue(); + gotoLeftEnd(node, S); - - if(minus>abs(target-node->data)) + if (S.empty()) + break; + node = S.pop(); + + if (target > old_data && target <= node->data) { - minus = abs(target - node->data); - target_p = node; + if (abs(old_data - target) < abs(node->data - target)) + { + // std::cout << node->pos; + printf("%02d:%02d \n", old_data / 60, old_data % 60); + } + else + { + // std::cout << node->pos; + printf("%02d:%02d \n", node->data / 60, node->data % 60); + } + return; } - if (hasLC(node)) - q.enqueue(node->lc); - if (hasRC(node)) - q.enqueue(node->rc); + old_data = node->data; + + node = node->rc; } - std::cout << target_p->pos; - printf("-%02d:%02d \n", target_p->data / 60, target_p->data % 60); + printf("未找到\n"); } //先序遍历 @@ -174,11 +181,24 @@ protected: if (!node) return; - std::cout << node->pos; - printf("-%02d:%02d \n", node->data / 60, node->data % 60); - - pre(node->lc); - pre(node->rc); + Stack S; + while (1) + { + visitAlongL(node, S); + if (S.empty()) + break; + node = S.pop(); + } + } + void visitAlongL(binNodeArray(int) x, Stack &S) + { + while (x) + { + std::cout << x->pos; + printf("-%02d:%02d \n", x->data / 60, x->data % 60); + S.push(x->rc); + x = x->lc; + } } void post(binNodeArray(int) node) @@ -197,12 +217,29 @@ protected: { if (!node) return; + Stack S; + while (1) + { + gotoLeftEnd(node, S); - pre(node->lc); + if (S.empty()) + break; + node = S.pop(); - std::cout << node->pos; - printf("-%02d:%02d \n", node->data / 60, node->data % 60); - pre(node->rc); + std::cout << node->pos; + printf("-%02d:%02d \n", node->data / 60, node->data % 60); + + node = node->rc; + } + } + + void gotoLeftEnd(binNodeArray(int) x, Stack &S) + { + while (x) + { + S.push(x); + x = x->lc; + } } public: @@ -225,4 +262,29 @@ public: return depth; } + binNodeArray(int) find_ans(binNodeArray(int) a, binNodeArray(int) b) + { + binNodeArray(int) ans = a; + binNodeArray(int) node; + while (ans) + { + node = ans; + Stack S; + while (1) + { + gotoLeftEnd(node, S); + + if (S.empty()) + break; + node = S.pop(); + + if(node==b) + return ans; + + node = node->rc; + } + ans = ans->parent; + } + return NULL; + } }; \ No newline at end of file diff --git a/ex9/ex9.cpp b/ex9/ex9.cpp index 5d5fa74..fb906d9 100644 --- a/ex9/ex9.cpp +++ b/ex9/ex9.cpp @@ -20,5 +20,7 @@ int main() printf("高度为%d\n",bst.height()); + + return 0; } \ No newline at end of file