diff --git a/BinaryTree/priorityQueue/maxWBLT/CMakeLists.txt b/BinaryTree/priorityQueue/maxWBLT/CMakeLists.txt new file mode 100644 index 0000000..7894444 --- /dev/null +++ b/BinaryTree/priorityQueue/maxWBLT/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.31) +project(maxWBLT) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(maxWBLT main.cpp) diff --git a/BinaryTree/priorityQueue/maxWBLT/binaryTreeNode.h b/BinaryTree/priorityQueue/maxWBLT/binaryTreeNode.h new file mode 100644 index 0000000..05791a9 --- /dev/null +++ b/BinaryTree/priorityQueue/maxWBLT/binaryTreeNode.h @@ -0,0 +1,22 @@ +// +// Created by PC on 25-8-6. +// + +#ifndef BINARYTREENODE_H +#define BINARYTREENODE_H + +template +class binaryTreeNode { +public: + T element; + binaryTreeNode* left; + binaryTreeNode* right; +public: + binaryTreeNode() {left = right = nullptr;} + explicit binaryTreeNode(const T& e) : element(e), left(nullptr), right(nullptr) {} + binaryTreeNode(const T& e, binaryTreeNode* l, binaryTreeNode* r) : element(e), left(l), right(r) {} + ~binaryTreeNode() = default; + +}; + +#endif //BINARYTREENODE_H diff --git a/BinaryTree/priorityQueue/maxWBLT/main.cpp b/BinaryTree/priorityQueue/maxWBLT/main.cpp new file mode 100644 index 0000000..fbfe037 --- /dev/null +++ b/BinaryTree/priorityQueue/maxWBLT/main.cpp @@ -0,0 +1,34 @@ +#include "maxWBLT.h" + +int main() { + /* +Tree1 + 50 + / \ + 40 30 + / \ +20 10 +Tree2 + 45 + / \ + 35 25 + / \ +15 5 +*/ + maxWBLT t1,t2; + t1.push(50);t1.push(30);t1.push(40);t1.push(20);t1.push(10); + t2.push(45);t2.push(25);t2.push(35);t2.push(15);t2.push(5); + t1.display(); + t2.display(); + //正确的树 + t1.meld(t2); + t1.display(); + return 0; +} +/* +最后输出了 +{5, 50}, {2, 40}, {2, 30}, {1, 20}, {1, 10}, +{5, 45}, {2, 35}, {2, 25}, {1, 15}, {1, 5}, +{10, 50}, {7, 45}, {2, 40}, {4, 30}, {2, 35}, {1, 20}, {2, 25}, {1, 10}, {1, 15}, {1, 5}, +与预测一致,详情见README.MD +*/ \ No newline at end of file diff --git a/BinaryTree/priorityQueue/maxWBLT/maxWBLT.h b/BinaryTree/priorityQueue/maxWBLT/maxWBLT.h new file mode 100644 index 0000000..5c69c95 --- /dev/null +++ b/BinaryTree/priorityQueue/maxWBLT/maxWBLT.h @@ -0,0 +1,127 @@ +// +// Created by PC on 25-8-10. +// + +#ifndef MAXHBLT_H +#define MAXHBLT_H +#include +#include +#include +#include "binaryTreeNode.h" +template +class maxWBLT { +private: + binaryTreeNode>* root; + int bsize; + std::vector>*> data; + static void meld(binaryTreeNode>* &x, binaryTreeNode>* &y); +public: + maxWBLT() : root(nullptr), bsize(0) {} + maxWBLT(binaryTreeNode>* r, int b) : root(r), bsize(b) {} + ~maxWBLT(); + + void push(const T& x); + void pop(); + void meld(maxWBLT &y); + void initialize() {} + void display(); + + T& top() {return root->element.second;} + bool empty() {return bsize == 0;} + int size() {return bsize;} + void dispose(binaryTreeNode>* r) { + if (r == nullptr) return; + if (r->left != nullptr) dispose(r->left); + if (r->right != nullptr) dispose(r->right); + delete r; + } +}; + +template +maxWBLT::~maxWBLT() { + dispose(root); + root = nullptr; + bsize = 0; +} + +template +void maxWBLT::push(const T& x) { + if (root == nullptr) { + root = new binaryTreeNode>(std::make_pair(1, x)); + return; + } + auto tmp = new binaryTreeNode>(std::make_pair(1, x)); + meld(root, tmp); + bsize++; +} + +template +void maxWBLT::pop() { + if (root == nullptr) { + std::cerr << "Empty Error!" << std::endl; + } + auto left = root->left; + auto right = root->right; + delete root; + root = nullptr; + meld(left, right); + bsize--; +} + +template +void maxWBLT::meld(maxWBLT &y) { + meld(root, y.root); + bsize += y.bsize; + y.dispose(y.root); + y.root = nullptr; + y.bsize = 0; +} + +template +void maxWBLT::display() { + if (root == nullptr) { + std::cerr << "Empty Error!" << std::endl; + return; + } + data.clear(); + data.push_back(root); + int cnt = 0; + while (cnt < bsize) { + if (data.at(cnt)->left != nullptr) data.push_back(data.at(cnt)->left); + if (data.at(cnt)->right != nullptr) data.push_back(data.at(cnt)->right); + cnt++; + } + for (auto x : data) { + std::cout << "{" << x->element.first << ", " << x->element.second << "}" << ", "; + } + std::cout << std::endl; +} + +//重要 -> 合并两个子树,把y合并进入x +//* &x,指针的引用,表示指针可以修改(传入函数) +//关注:统计树的特定数据的时候一定要从下往上统计(类似后序遍历),优先汇总叶子,最后汇总到根 +template +void maxWBLT::meld(binaryTreeNode>*& x, binaryTreeNode>*& y) { + if (y == nullptr) return; + if (x == nullptr) { + x = y; + y = nullptr; + return; + } + + if (x->element.second < y->element.second) + std::swap(x, y); + + meld(x->right, y); + + // 左重性检查,写成两个变量更好,更清晰 + int lw = x->left ? x->left->element.first : 0; + int rw = x->right ? x->right->element.first : 0; + if (lw < rw) + std::swap(x->left, x->right); + + x->element.first = 1 + lw + rw; +} + + +#endif //MAXHBLT_H