commit c90d4b1c3c94ee8029fdf6abdfdbc9a19f1b4e24 Author: Lenyiin <569963146@qq.com> Date: Sat Sep 14 13:45:51 2024 +0800 博客 https://blog.lenyiin.com/bstree/ 的代码仓库 diff --git a/Linux/BSTree.hpp b/Linux/BSTree.hpp new file mode 100644 index 0000000..2d4b6a5 --- /dev/null +++ b/Linux/BSTree.hpp @@ -0,0 +1,586 @@ +#pragma once + +#include +#include +#include + +namespace Lenyiin +{ + template + struct BSTreeNode + { + BSTreeNode(const K &key = K(), const V &value = V()) + : _key(key), _value(value), _left(nullptr), _right(nullptr) + { + } + + K _key; + V _value; + BSTreeNode *_left; + BSTreeNode *_right; + }; + + template + class BSTree + { + private: + typedef BSTreeNode Node; + + Node *_Copy(Node *root) + { + if (root == nullptr) + { + return nullptr; + } + + Node *cur = new Node(root->_key, root->_value); + cur->_left = _Copy(root->_left); + cur->_right = _Copy(root->_right); + + return cur; + } + + public: + // 默认构造 + BSTree() + : _root(nullptr) + { + } + + // 拷贝构造 + BSTree(const BSTree &tree) + { + _root = _Copy(tree._root); + } + + // 非递归插入 + // bool Insert(const K& key, const V& value) + //{ + // // 如果根为空, 直接插入 + // if (_root == nullptr) + // { + // _root = new Node(key, value); + // return true; + // } + + // // 如果根不为空, 找到插入位置 + // Node* parent = nullptr; + // Node* cur = _root; + // while (cur) + // { + // if (cur->_key > key) + // { + // parent = cur; + // cur = cur->_left; + // } + // else if (cur->_key < key) + // { + // parent = cur; + // cur = cur->_right; + // } + // else + // { + // return false; // 如果相等, 表示我已经有了, 不需要插入了 + // } + // } + + // // 插入节点 + // cur = new Node(key, value); + // if (parent->_key > key) + // { + // parent->_left = cur; + // } + // else + // { + // parent->_right = cur; + // } + + // return true; + //} + + // 递归插入 + // 辅助函数 + Node *_insert(Node *root, const K &key, const V &value) + { + if (root == nullptr) + { + return new Node(key, value); + } + if (key < root->_key) + { + root->_left = _insert(root->_left, key, value); + } + else if (key > root->_key) + { + root->_right = _insert(root->_right, key, value); + } + return root; + } + + // 插入 + void Insert(const K &key, const V &value) + { + _root = _insert(_root, key, value); + } + + // 非递归查找节点 + // Node* Find(const K& key) + //{ + // Node* cur = _root; + // while (cur) + // { + // if (cur->_key > key) + // { + // cur = cur->_left; + // } + // else if (cur->_key < key) + // { + // cur = cur->_right; + // } + // else + // { + // return cur; + // } + // } + // return nullptr; + //} + + // 递归查找节点 + // 辅助函数 + Node *_find(Node *root, const K &key) + { + if (root == nullptr || root->_key == key) + { + return root; + } + if (key < root->_key) + { + return _find(root->_left, key); + } + else + { + return _find(root->_right, key); + } + } + + // 查找节点 + Node *Find(const K &key) + { + return _find(_root, key); + } + + // 非递归删除节点 + // bool Erase(const K& key) + //{ + // // 找到要删除的节点 + // Node* parent = nullptr; + // Node* cur = _root; + // while (cur) + // { + // if (cur->_key > key) + // { + // parent = cur; + // cur = cur->_left; + // } + // else if (cur->_key < key) + // { + // parent = cur; + // cur = cur->_right; + // } + // else + // { + // break; + // } + // } + + // // 没找到 + // if (cur == nullptr) + // { + // return false; + // } + + // // 找到了 + // // 1. 要删除的节点没有左孩子 + // if (cur->_left == nullptr) + // { + // if (cur == _root) + // { + // _root = cur->_right; + // } + // else + // { + // if (parent->_left == cur) + // { + // parent->_left = cur->_right; + // } + // else + // { + // parent->_right = cur->_right; + // } + // } + // delete cur; + // } + // // 2. 要删除的节点没有右孩子 + // else if (cur->_right == nullptr) + // { + // if (cur == _root) + // { + // _root = cur->_left; + // } + // else + // { + // if (parent->_left == cur) + // { + // parent->_left = cur->_left; + // } + // else + // { + // parent->_right = cur->_left; + // } + // } + // delete cur; + // } + // // 3. 左右都不为空, 不能直接删除, 使用替换法删除 + // // 可以找左子树的最大节点或者右子树的最小节点去替代它 + // else + // { + // // 右子树最小节点 + // Node* rightMinParent = cur; + // Node* rightMin = cur->_right; + // while (rightMin->_left) + // { + // rightMinParent = rightMin; + // rightMin = rightMin->_left; + // } + + // // 替换删除的节点 + // std::swap(cur->_key, rightMin->_key); + // std::swap(cur->_value, rightMin->_value); + + // // 把问题转换成删除 rightMin + // if (rightMinParent->_left == rightMin) + // { + // rightMinParent->_left = rightMin->_right; + // } + // else + // { + // rightMinParent->_right = rightMin->_right; + // } + + // delete rightMin; + // } + + // return true; + //} + + // 递归删除节点 + // 辅助函数 + Node *_erase(Node *root, const K &key) + { + if (root == nullptr) + { + return root; + } + if (key < root->_key) + { + root->_left = _erase(root->_left, key); + } + else if (key > root->_key) + { + root->_right = _erase(root->_right, key); + } + else + { + if (root->_left == nullptr) + { + Node *ret = root->_right; + delete root; + return ret; + } + if (root->_right == nullptr) + { + Node *ret = root->_left; + delete root; + return ret; + } + Node *minNode = getMin(root->_right); + root->_key = minNode->_key; + root->_right = _erase(root->_right, minNode->_key); + } + return root; + } + + Node *getMin(Node *node) + { + while (node->_left != nullptr) + { + node = node->_left; + } + return node; + } + + // 删除节点 + void Erase(const K &key) + { + _erase(_root, key); + } + + // 递归中序遍历 + // 辅助函数 + // void _InOrder(Node* root) + //{ + // if (root == nullptr) + // { + // return; + // } + + // _InOrder(root->_left); + // //std::cout << root->_key << " : " << root->_value << std::endl; + // std::cout << root->_key << " "; + // _InOrder(root->_right); + //} + + // void InOrder() + //{ + // _InOrder(_root); + // std::cout << std::endl; + // } + + // 非递归中序遍历 + void InOrder() + { + std::stack stack; + Node *cur = _root; + + while (cur != nullptr || !stack.empty()) + { + // 找到最左边的节点 + while (cur != nullptr) + { + stack.push(cur); + cur = cur->_left; + } + + // 处理当前节点 + cur = stack.top(); + stack.pop(); + std::cout << cur->_key << " "; + + // 转向右子树 + cur = cur->_right; + } + std::cout << std::endl; + } + + // 递归前序遍历 + // 辅助函数 + // void _PreOrder(Node* root) + //{ + // if (root == nullptr) + // { + // return; + // } + + // std::cout << root->_key << " "; // 访问根节点 + // _PreOrder(root->_left); // 访问左子树 + // _PreOrder(root->_right); // 访问右子树 + //} + + // void PreOrder() + //{ + // _PreOrder(_root); + // std::cout << std::endl; + // } + + // 非递归前序遍历 + void PreOrder() + { + if (_root == nullptr) + { + return; + } + + std::stack stack; + stack.push(_root); + + while (!stack.empty()) + { + Node *cur = stack.top(); + stack.pop(); + std::cout << cur->_key << " "; // 访问当前节点 + + // 先压入右子树再压入左子树(确保左子树先处理) + if (cur->_right != nullptr) + { + stack.push(cur->_right); + } + if (cur->_left != nullptr) + { + stack.push(cur->_left); + } + } + std::cout << std::endl; + } + + // 递归后序遍历 + // 辅助函数 + // void _PostOrder(Node* root) { + // if (root == nullptr) + // { + // return; + // } + + // _PostOrder(root->_left); // 访问左子树 + // _PostOrder(root->_right); // 访问右子树 + // std::cout << root->_key << " "; // 访问根节点 + //} + + // void PostOrder() + //{ + // _PostOrder(_root); + // std::cout << std::endl; + // } + + // 非递归后序遍历 + void PostOrder() + { + if (_root == nullptr) + { + return; + } + + std::stack st; + Node *cur = _root; + Node *lastNode = nullptr; // 最近访问的节点 + while (cur || !st.empty()) + { + while (cur) + { + st.push(cur); + cur = cur->_left; + } + + Node *top = st.top(); + if ((top->_right == nullptr) || (lastNode == top->_right)) + { + std::cout << top->_key << " "; + lastNode = top; + st.pop(); + } + else + { + cur = top->_right; + } + } + std::cout << std::endl; + } + + // 层序遍历 + void LevelOrder() + { + if (_root == nullptr) + { + return; + } + + std::queue queue; + queue.push(_root); + + while (!queue.empty()) + { + Node *cur = queue.front(); + queue.pop(); + std::cout << cur->_key << " "; // 访问当前节点 + + if (cur->_left != nullptr) + { + queue.push(cur->_left); // 将左子树压入队列 + } + if (cur->_right != nullptr) + { + queue.push(cur->_right); // 将右子树压入队列 + } + } + std::cout << std::endl; + } + + // 查找前驱节点 + Node *findMax(Node *node) + { + while (node->_right != nullptr) + { + node = node->_right; + } + return node; + } + + Node *FindPredecessor(Node *node) + { + if (node->_left != nullptr) + { + return findMax(node->_left); + } + + Node *cur = _root; + Node *predecessor = nullptr; + while (cur != nullptr) + { + if (node->_key < cur->_key) + { + cur = cur->_left; + } + else if (node->_key > cur->_key) + { + predecessor = cur; + cur = cur->_right; + } + else + { + break; + } + } + return predecessor; + } + + // 查找后继节点 + Node *findMin(Node *node) + { + while (node->_left != nullptr) + { + node = node->_left; + } + return node; + } + + Node *FindSuccessor(Node *node) + { + if (node->_right != nullptr) + { + return findMin(node->_right); + } + + Node *cur = _root; + Node *successor = nullptr; + while (cur != nullptr) + { + if (node->_key < cur->_key) + { + successor = cur; + cur = cur->_left; + } + else if (node->_key > cur->_key) + { + cur = cur->_right; + } + else + { + break; + } + } + return successor; + } + + private: + Node *_root = nullptr; + }; +} \ No newline at end of file diff --git a/Linux/Main.cc b/Linux/Main.cc new file mode 100644 index 0000000..282b3f6 --- /dev/null +++ b/Linux/Main.cc @@ -0,0 +1,136 @@ +#include "BSTree.hpp" +#include + +using namespace Lenyiin; + +void test_BSTree_1() +{ + BSTree t; + int a[] = {5, 3, 4, 1, 7, 8, 2, 6, 0, 9}; + for (const auto &e : a) + { + t.Insert(e, e); + } + + // 搜索二叉树的中序遍历 + t.InOrder(); + // 搜索二叉树的前序遍历 + t.PreOrder(); + // 搜索二叉树的后序遍历 + t.PostOrder(); + // 搜索二叉树的层序遍历 + t.LevelOrder(); + + auto node = t.Find(5); + if (node != nullptr) + { + std::cout << node->_value << std::endl; + } + else + { + std::cout << "没找到" << std::endl; + } + + auto pre = t.FindPredecessor(node); + if (pre != nullptr) + { + std::cout << node->_key << " 的前驱节点是: " << pre->_key << std::endl; + } + else + { + std::cout << "前驱节点为空" << std::endl; + } + + auto suc = t.FindSuccessor(node); + if (suc != nullptr) + { + std::cout << node->_key << " 的后继节点是: " << suc->_key << std::endl; + } + else + { + std::cout << "后继节点为空" << std::endl; + } + + // 1. 叶子 + t.Erase(5); + t.InOrder(); + + // 左为空或者右为空 + t.Erase(8); + t.Erase(1); + t.InOrder(); + + // 重复删除 + t.Erase(5); + t.InOrder(); +} + +void test_BSTree_2() +{ + BSTree dict; + dict.Insert("sort", "排序"); + dict.Insert("string", "字符串"); + dict.Insert("vector", "向量"); + dict.Insert("map", "映射"); + dict.Insert("set", "集合"); + dict.Insert("algorithm", "算法"); + dict.Insert("iterator", "迭代器"); + dict.Insert("stack", "栈"); + dict.Insert("queue", "队列"); + dict.Insert("list", "链表"); + dict.Insert("deque", "双端队列"); + dict.Insert("priority_queue", "优先级队列"); + dict.Insert("binary_search", "二分查找"); + dict.Insert("lower_bound", "下界"); + dict.Insert("upper_bound", "上界"); + + dict.InOrder(); + + std::string str; + while (std::cin >> str) + { + BSTreeNode *ret = dict.Find(str); + if (ret) + { + std::cout << ret->_key << ":" << ret->_value << std::endl; + } + else + { + std::cout << "没找到" << std::endl; + } + } +} + +void test_BSTree_3() +{ + std::string strArr[] = {"西瓜", "苹果", "西瓜", "苹果", "西瓜", "香蕉", "西瓜", "樱桃", "西瓜", "苹果", + "西瓜", "苹果", "西瓜", "香蕉", "西瓜", "樱桃", "西瓜", "苹果", "西瓜", "苹果", "西瓜", "香蕉", + "西瓜", "樱桃", "西瓜", "苹果", "西瓜", "苹果", "西瓜", "香蕉", "西瓜", "樱桃", "西瓜", "苹果", + "西瓜", "苹果", "西瓜", "香蕉", "西瓜", "樱桃", "西瓜", "苹果", "西瓜", "苹果", "西瓜", "香蕉"}; + + BSTree countTree; + + for (auto str : strArr) + { + BSTreeNode *ret = countTree.Find(str); + if (ret == nullptr) + { + countTree.Insert(str, 1); + } + else + { + ret->_value++; + } + } + + countTree.InOrder(); +} + +int main() +{ + test_BSTree_1(); + // test_BSTree_2(); + test_BSTree_3(); + + return 0; +} \ No newline at end of file diff --git a/Linux/Makefile b/Linux/Makefile new file mode 100644 index 0000000..cd6faa6 --- /dev/null +++ b/Linux/Makefile @@ -0,0 +1,6 @@ +main:Main.cc + g++ -o $@ $^ + +.PHONY:clean +clean: + rm -f main \ No newline at end of file diff --git a/Linux/main b/Linux/main new file mode 100644 index 0000000..ee46a0c Binary files /dev/null and b/Linux/main differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/3f914661-426a-467d-9a49-d07fc40cc4b8.vsidx b/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/3f914661-426a-467d-9a49-d07fc40cc4b8.vsidx new file mode 100644 index 0000000..d379358 Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/3f914661-426a-467d-9a49-d07fc40cc4b8.vsidx differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/73987a0b-eb99-4ef4-b653-a8662bac9be6.vsidx b/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/73987a0b-eb99-4ef4-b653-a8662bac9be6.vsidx new file mode 100644 index 0000000..7f12f30 Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/73987a0b-eb99-4ef4-b653-a8662bac9be6.vsidx differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/819ec7f1-4156-4dac-a05d-ea5b73083436.vsidx b/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/819ec7f1-4156-4dac-a05d-ea5b73083436.vsidx new file mode 100644 index 0000000..e348dd1 Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/FileContentIndex/819ec7f1-4156-4dac-a05d-ea5b73083436.vsidx differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/v17/.suo b/Windows_BSTree/.vs/Windows_BSTree/v17/.suo new file mode 100644 index 0000000..7889375 Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/v17/.suo differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/v17/Browse.VC.db b/Windows_BSTree/.vs/Windows_BSTree/v17/Browse.VC.db new file mode 100644 index 0000000..4eee0d5 Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/v17/Browse.VC.db differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/v17/ipch/AutoPCH/98913816d4adbcbd/BSTREE.ipch b/Windows_BSTree/.vs/Windows_BSTree/v17/ipch/AutoPCH/98913816d4adbcbd/BSTREE.ipch new file mode 100644 index 0000000..853216b Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/v17/ipch/AutoPCH/98913816d4adbcbd/BSTREE.ipch differ diff --git a/Windows_BSTree/.vs/Windows_BSTree/v17/ipch/AutoPCH/c3e03f16ed3512f4/BSTREE.ipch b/Windows_BSTree/.vs/Windows_BSTree/v17/ipch/AutoPCH/c3e03f16ed3512f4/BSTREE.ipch new file mode 100644 index 0000000..e4eb9e7 Binary files /dev/null and b/Windows_BSTree/.vs/Windows_BSTree/v17/ipch/AutoPCH/c3e03f16ed3512f4/BSTREE.ipch differ diff --git a/Windows_BSTree/BSTree.cpp b/Windows_BSTree/BSTree.cpp new file mode 100644 index 0000000..7c5af67 --- /dev/null +++ b/Windows_BSTree/BSTree.cpp @@ -0,0 +1,136 @@ +#include "BSTree.hpp" +#include + +using namespace Lenyiin; + +void test_BSTree_1() +{ + BSTree t; + int a[] = { 5,3,4,1,7,8,2,6,0,9 }; + for (const auto& e : a) + { + t.Insert(e, e); + } + + // + t.InOrder(); + // ǰ + t.PreOrder(); + // ĺ + t.PostOrder(); + // IJ + t.LevelOrder(); + + auto node = t.Find(5); + if (node != nullptr) + { + std::cout << node->_value << std::endl; + } + else + { + std::cout << "ûҵ" << std::endl; + } + + auto pre = t.FindPredecessor(node); + if (pre != nullptr) + { + std::cout << node->_key << " ǰڵ: " << pre->_key << std::endl; + } + else + { + std::cout << "ǰڵΪ" << std::endl; + } + + auto suc = t.FindSuccessor(node); + if (suc != nullptr) + { + std::cout << node->_key << " ĺ̽ڵ: " << suc->_key << std::endl; + } + else + { + std::cout << "̽ڵΪ" << std::endl; + } + + // 1. Ҷ + t.Erase(5); + t.InOrder(); + + // ΪջΪ + t.Erase(8); + t.Erase(1); + t.InOrder(); + + // ظɾ + t.Erase(5); + t.InOrder(); +} + +void test_BSTree_2() +{ + BSTree dict; + dict.Insert("sort", ""); + dict.Insert("string", "ַ"); + dict.Insert("vector", ""); + dict.Insert("map", "ӳ"); + dict.Insert("set", ""); + dict.Insert("algorithm", "㷨"); + dict.Insert("iterator", ""); + dict.Insert("stack", "ջ"); + dict.Insert("queue", ""); + dict.Insert("list", ""); + dict.Insert("deque", "˫˶"); + dict.Insert("priority_queue", "ȼ"); + dict.Insert("binary_search", "ֲ"); + dict.Insert("lower_bound", "½"); + dict.Insert("upper_bound", "Ͻ"); + + dict.InOrder(); + + std::string str; + while (std::cin >> str) + { + BSTreeNode* ret = dict.Find(str); + if (ret) + { + std::cout << ret->_key << ":" << ret->_value << std::endl; + } + else + { + std::cout << "ûҵ" << std::endl; + } + } +} + +void test_BSTree_3() +{ + std::string strArr[] = { "", "ƻ", "", "ƻ", "", "㽶", "", "ӣ", "", "ƻ", + "", "ƻ", "", "㽶", "", "ӣ", "", "ƻ", "", "ƻ", "", "㽶", + "", "ӣ", "", "ƻ", "", "ƻ", "", "㽶", "", "ӣ", "", "ƻ", + "", "ƻ", "", "㽶", "", "ӣ", "", "ƻ", "", "ƻ", "", "㽶" }; + + BSTree countTree; + + for (auto str : strArr) + { + BSTreeNode* ret = countTree.Find(str); + if (ret == nullptr) + { + countTree.Insert(str, 1); + } + else + { + ret->_value++; + } + } + + countTree.InOrder(); +} + +int main() +{ + test_BSTree_1(); + //test_BSTree_2(); + test_BSTree_3(); + + return 0; +} \ No newline at end of file diff --git a/Windows_BSTree/BSTree.hpp b/Windows_BSTree/BSTree.hpp new file mode 100644 index 0000000..f57ca41 --- /dev/null +++ b/Windows_BSTree/BSTree.hpp @@ -0,0 +1,578 @@ +#pragma once + +#include +#include +#include + +namespace Lenyiin +{ + template + struct BSTreeNode + { + BSTreeNode(const K& key = K(), const V& value = V()) + : _key(key), _value(value), _left(nullptr), _right(nullptr) + { + } + + K _key; + V _value; + BSTreeNode* _left; + BSTreeNode* _right; + }; + + template + class BSTree + { + private: + typedef BSTreeNode Node; + + Node* _Copy(Node* root) + { + if (root == nullptr) + { + return nullptr; + } + + Node* cur = new Node(root->_key, root->_value); + cur->_left = _Copy(root->_left); + cur->_right = _Copy(root->_right); + + return cur; + } + + public: + // ĬϹ + BSTree() + : _root(nullptr) + {} + + // + BSTree(const BSTree& tree) + { + _root = _Copy(tree._root); + } + + // ǵݹ + //bool Insert(const K& key, const V& value) + //{ + // // Ϊ, ֱӲ + // if (_root == nullptr) + // { + // _root = new Node(key, value); + // return true; + // } + + // // Ϊ, ҵλ + // Node* parent = nullptr; + // Node* cur = _root; + // while (cur) + // { + // if (cur->_key > key) + // { + // parent = cur; + // cur = cur->_left; + // } + // else if (cur->_key < key) + // { + // parent = cur; + // cur = cur->_right; + // } + // else + // { + // return false; // , ʾѾ, Ҫ + // } + // } + + // // ڵ + // cur = new Node(key, value); + // if (parent->_key > key) + // { + // parent->_left = cur; + // } + // else + // { + // parent->_right = cur; + // } + + // return true; + //} + + // ݹ + // + Node* _insert(Node* root, const K& key, const V& value) + { + if (root == nullptr) + { + return new Node(key, value); + } + if (key < root->_key) + { + root->_left = _insert(root->_left, key, value); + } + else if (key > root->_key) + { + root->_right = _insert(root->_right, key, value); + } + return root; + } + + // + void Insert(const K& key, const V& value) + { + _root = _insert(_root, key, value); + } + + // ǵݹҽڵ + //Node* Find(const K& key) + //{ + // Node* cur = _root; + // while (cur) + // { + // if (cur->_key > key) + // { + // cur = cur->_left; + // } + // else if (cur->_key < key) + // { + // cur = cur->_right; + // } + // else + // { + // return cur; + // } + // } + // return nullptr; + //} + + // ݹҽڵ + // + Node* _find(Node* root, const K& key) + { + if (root == nullptr || root->_key == key) + { + return root; + } + if (key < root->_key) + { + return _find(root->_left, key); + } + else + { + return _find(root->_right, key); + } + } + + // ҽڵ + Node* Find(const K& key) + { + return _find(_root, key); + } + + // ǵݹɾڵ + //bool Erase(const K& key) + //{ + // // ҵҪɾĽڵ + // Node* parent = nullptr; + // Node* cur = _root; + // while (cur) + // { + // if (cur->_key > key) + // { + // parent = cur; + // cur = cur->_left; + // } + // else if (cur->_key < key) + // { + // parent = cur; + // cur = cur->_right; + // } + // else + // { + // break; + // } + // } + + // // ûҵ + // if (cur == nullptr) + // { + // return false; + // } + + // // ҵ + // // 1. ҪɾĽڵû + // if (cur->_left == nullptr) + // { + // if (cur == _root) + // { + // _root = cur->_right; + // } + // else + // { + // if (parent->_left == cur) + // { + // parent->_left = cur->_right; + // } + // else + // { + // parent->_right = cur->_right; + // } + // } + // delete cur; + // } + // // 2. ҪɾĽڵûҺ + // else if (cur->_right == nullptr) + // { + // if (cur == _root) + // { + // _root = cur->_left; + // } + // else + // { + // if (parent->_left == cur) + // { + // parent->_left = cur->_left; + // } + // else + // { + // parent->_right = cur->_left; + // } + // } + // delete cur; + // } + // // 3. ҶΪ, ֱɾ, ʹ滻ɾ + // // ڵСڵȥ + // else + // { + // // Сڵ + // Node* rightMinParent = cur; + // Node* rightMin = cur->_right; + // while (rightMin->_left) + // { + // rightMinParent = rightMin; + // rightMin = rightMin->_left; + // } + + // // 滻ɾĽڵ + // std::swap(cur->_key, rightMin->_key); + // std::swap(cur->_value, rightMin->_value); + + // // תɾ rightMin + // if (rightMinParent->_left == rightMin) + // { + // rightMinParent->_left = rightMin->_right; + // } + // else + // { + // rightMinParent->_right = rightMin->_right; + // } + + // delete rightMin; + // } + + // return true; + //} + + // ݹɾڵ + // + Node* _erase(Node* root, const K& key) + { + if (root == nullptr) + { + return root; + } + if (key < root->_key) + { + root->_left = _erase(root->_left, key); + } + else if (key > root->_key) + { + root->_right = _erase(root->_right, key); + } + else + { + if (root->_left == nullptr) + { + Node* ret = root->_right; + delete root; + return ret; + } + if (root->_right == nullptr) + { + Node* ret = root->_left; + delete root; + return ret; + } + Node* minNode = getMin(root->_right); + root->_key = minNode->_key; + root->_right = _erase(root->_right, minNode->_key); + } + return root; + } + + Node* getMin(Node* node) + { + while (node->_left != nullptr) + { + node = node->_left; + } + return node; + } + + // ɾڵ + void Erase(const K& key) + { + _erase(_root, key); + } + + // ݹ + // + //void _InOrder(Node* root) + //{ + // if (root == nullptr) + // { + // return; + // } + + // _InOrder(root->_left); + // //std::cout << root->_key << " : " << root->_value << std::endl; + // std::cout << root->_key << " "; + // _InOrder(root->_right); + //} + + //void InOrder() + //{ + // _InOrder(_root); + // std::cout << std::endl; + //} + + // ǵݹ + void InOrder() + { + std::stack stack; + Node* cur = _root; + + while (cur != nullptr || !stack.empty()) { + // ҵߵĽڵ + while (cur != nullptr) { + stack.push(cur); + cur = cur->_left; + } + + // ǰڵ + cur = stack.top(); + stack.pop(); + std::cout << cur->_key << " "; + + // ת + cur = cur->_right; + } + std::cout << std::endl; + } + + // ݹǰ + // + //void _PreOrder(Node* root) + //{ + // if (root == nullptr) + // { + // return; + // } + + // std::cout << root->_key << " "; // ʸڵ + // _PreOrder(root->_left); // + // _PreOrder(root->_right); // + //} + + //void PreOrder() + //{ + // _PreOrder(_root); + // std::cout << std::endl; + //} + + // ǵݹǰ + void PreOrder() + { + if (_root == nullptr) + { + return; + } + + std::stack stack; + stack.push(_root); + + while (!stack.empty()) { + Node* cur = stack.top(); + stack.pop(); + std::cout << cur->_key << " "; // ʵǰڵ + + // ѹѹȷȴ + if (cur->_right != nullptr) + { + stack.push(cur->_right); + } + if (cur->_left != nullptr) { + stack.push(cur->_left); + } + } + std::cout << std::endl; + } + + // ݹ + // + //void _PostOrder(Node* root) { + // if (root == nullptr) + // { + // return; + // } + + // _PostOrder(root->_left); // + // _PostOrder(root->_right); // + // std::cout << root->_key << " "; // ʸڵ + //} + + //void PostOrder() + //{ + // _PostOrder(_root); + // std::cout << std::endl; + //} + + // ǵݹ + void PostOrder() + { + if (_root == nullptr) + { + return; + } + + std::stack st; + Node* cur = _root; + Node* lastNode = nullptr; // ʵĽڵ + while (cur || !st.empty()) + { + while (cur) + { + st.push(cur); + cur = cur->_left; + } + + Node* top = st.top(); + if ((top->_right == nullptr) || (lastNode == top->_right)) + { + std::cout << top->_key << " "; + lastNode = top; + st.pop(); + } + else { + cur = top->_right; + } + } + std::cout << std::endl; + } + + // + void LevelOrder() + { + if (_root == nullptr) + { + return; + } + + std::queue queue; + queue.push(_root); + + while (!queue.empty()) + { + Node* cur = queue.front(); + queue.pop(); + std::cout << cur->_key << " "; // ʵǰڵ + + if (cur->_left != nullptr) { + queue.push(cur->_left); // ѹ + } + if (cur->_right != nullptr) { + queue.push(cur->_right); // ѹ + } + } + std::cout << std::endl; + } + + // ǰڵ + Node* findMax(Node* node) + { + while (node->_right != nullptr) + { + node = node->_right; + } + return node; + } + + Node* FindPredecessor(Node* node) + { + if (node->_left != nullptr) + { + return findMax(node->_left); + } + + Node* cur = _root; + Node* predecessor = nullptr; + while (cur != nullptr) + { + if (node->_key < cur->_key) + { + cur = cur->_left; + } + else if (node->_key > cur->_key) + { + predecessor = cur; + cur = cur->_right; + } + else + { + break; + } + } + return predecessor; + } + + // Һ̽ڵ + Node* findMin(Node* node) + { + while (node->_left != nullptr) + { + node = node->_left; + } + return node; + } + + Node* FindSuccessor(Node* node) + { + if (node->_right != nullptr) + { + return findMin(node->_right); + } + + Node* cur = _root; + Node* successor = nullptr; + while (cur != nullptr) + { + if (node->_key < cur->_key) + { + successor = cur; + cur = cur->_left; + } + else if (node->_key > cur->_key) + { + cur = cur->_right; + } + else + { + break; + } + } + return successor; + } + + private: + Node* _root = nullptr; + }; +} \ No newline at end of file diff --git a/Windows_BSTree/Windows_BSTree.sln b/Windows_BSTree/Windows_BSTree.sln new file mode 100644 index 0000000..58a3f8c --- /dev/null +++ b/Windows_BSTree/Windows_BSTree.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34221.43 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Windows_BSTree", "Windows_BSTree.vcxproj", "{BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Debug|x64.ActiveCfg = Debug|x64 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Debug|x64.Build.0 = Debug|x64 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Debug|x86.ActiveCfg = Debug|Win32 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Debug|x86.Build.0 = Debug|Win32 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Release|x64.ActiveCfg = Release|x64 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Release|x64.Build.0 = Release|x64 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Release|x86.ActiveCfg = Release|Win32 + {BAEFFF7F-728A-4BAB-8F43-CFE15FBE2D97}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A3CDF196-D826-40E9-82D9-B280365A075B} + EndGlobalSection +EndGlobal diff --git a/Windows_BSTree/Windows_BSTree.vcxproj b/Windows_BSTree/Windows_BSTree.vcxproj new file mode 100644 index 0000000..9505b70 --- /dev/null +++ b/Windows_BSTree/Windows_BSTree.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {baefff7f-728a-4bab-8f43-cfe15fbe2d97} + WindowsBSTree + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/Windows_BSTree/Windows_BSTree.vcxproj.filters b/Windows_BSTree/Windows_BSTree.vcxproj.filters new file mode 100644 index 0000000..5a5074e --- /dev/null +++ b/Windows_BSTree/Windows_BSTree.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Windows_BSTree/Windows_BSTree.vcxproj.user b/Windows_BSTree/Windows_BSTree.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/Windows_BSTree/Windows_BSTree.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Windows_BSTree/x64/Debug/BSTree.obj b/Windows_BSTree/x64/Debug/BSTree.obj new file mode 100644 index 0000000..b6740c2 Binary files /dev/null and b/Windows_BSTree/x64/Debug/BSTree.obj differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.exe b/Windows_BSTree/x64/Debug/Windows_BSTree.exe new file mode 100644 index 0000000..b4366b8 Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.exe differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.exe.recipe b/Windows_BSTree/x64/Debug/Windows_BSTree.exe.recipe new file mode 100644 index 0000000..e4e749e --- /dev/null +++ b/Windows_BSTree/x64/Debug/Windows_BSTree.exe.recipe @@ -0,0 +1,11 @@ + + + + + E:\Git 仓库\公开仓库\6_BSTree\Windows_BSTree\x64\Debug\Windows_BSTree.exe + + + + + + \ No newline at end of file diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.ilk b/Windows_BSTree/x64/Debug/Windows_BSTree.ilk new file mode 100644 index 0000000..9d1d5bb Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.ilk differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.log b/Windows_BSTree/x64/Debug/Windows_BSTree.log new file mode 100644 index 0000000..29e0e5d --- /dev/null +++ b/Windows_BSTree/x64/Debug/Windows_BSTree.log @@ -0,0 +1,2 @@ + BSTree.cpp + Windows_BSTree.vcxproj -> E:\Git 仓库\公开仓库\6_BSTree\Windows_BSTree\x64\Debug\Windows_BSTree.exe diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.pdb b/Windows_BSTree/x64/Debug/Windows_BSTree.pdb new file mode 100644 index 0000000..3347d9c Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.pdb differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.command.1.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.command.1.tlog new file mode 100644 index 0000000..b228ba9 Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.command.1.tlog differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.read.1.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.read.1.tlog new file mode 100644 index 0000000..9a37ba3 Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.read.1.tlog differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.write.1.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.write.1.tlog new file mode 100644 index 0000000..d4376db Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/CL.write.1.tlog differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/Cl.items.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/Cl.items.tlog new file mode 100644 index 0000000..a53803e --- /dev/null +++ b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/Cl.items.tlog @@ -0,0 +1 @@ +E:\Git 仓库\公开仓库\6_BSTree\Windows_BSTree\BSTree.cpp;E:\Git 仓库\公开仓库\6_BSTree\Windows_BSTree\x64\Debug\BSTree.obj diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/Windows_BSTree.lastbuildstate b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/Windows_BSTree.lastbuildstate new file mode 100644 index 0000000..e6ead61 --- /dev/null +++ b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/Windows_BSTree.lastbuildstate @@ -0,0 +1,2 @@ +PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.37.32822:TargetPlatformVersion=10.0.22000.0: +Debug|x64|E:\Git 仓库\公开仓库\6_BSTree\Windows_BSTree\| diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.command.1.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.command.1.tlog new file mode 100644 index 0000000..b586f85 Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.command.1.tlog differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.read.1.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.read.1.tlog new file mode 100644 index 0000000..2ed36a8 Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.read.1.tlog differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.write.1.tlog b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.write.1.tlog new file mode 100644 index 0000000..9b311b1 Binary files /dev/null and b/Windows_BSTree/x64/Debug/Windows_BSTree.tlog/link.write.1.tlog differ diff --git a/Windows_BSTree/x64/Debug/Windows_BSTree.vcxproj.FileListAbsolute.txt b/Windows_BSTree/x64/Debug/Windows_BSTree.vcxproj.FileListAbsolute.txt new file mode 100644 index 0000000..a761179 --- /dev/null +++ b/Windows_BSTree/x64/Debug/Windows_BSTree.vcxproj.FileListAbsolute.txt @@ -0,0 +1 @@ +E:\Git 仓库\公开仓库\6_BSTree\Windows_BSTree\x64\Debug\Windows_BSTree.exe diff --git a/Windows_BSTree/x64/Debug/vc143.idb b/Windows_BSTree/x64/Debug/vc143.idb new file mode 100644 index 0000000..9f454a4 Binary files /dev/null and b/Windows_BSTree/x64/Debug/vc143.idb differ diff --git a/Windows_BSTree/x64/Debug/vc143.pdb b/Windows_BSTree/x64/Debug/vc143.pdb new file mode 100644 index 0000000..b4856d8 Binary files /dev/null and b/Windows_BSTree/x64/Debug/vc143.pdb differ