133 lines
3.2 KiB
C++
133 lines
3.2 KiB
C++
//
|
||
// Created by PC on 25-8-9.
|
||
//
|
||
|
||
#ifndef MAXHBLT_H
|
||
#define MAXHBLT_H
|
||
#include <utility>
|
||
#include <iostream>
|
||
#include <vector>
|
||
#include "binaryTreeNode.h"
|
||
template<class T>
|
||
class maxHblt {
|
||
private:
|
||
binaryTreeNode<std::pair<int, T>>* root;
|
||
int bsize;
|
||
std::vector<binaryTreeNode<std::pair<int, T>>*> data;
|
||
static void meld(binaryTreeNode<std::pair<int, T>>* &x, binaryTreeNode<std::pair<int, T>>* &y);
|
||
public:
|
||
maxHblt() : root(nullptr), bsize(0) {}
|
||
maxHblt(binaryTreeNode<std::pair<int, T>>* r, int b) : root(r), bsize(b) {}
|
||
~maxHblt();
|
||
|
||
void push(const T& x);
|
||
void pop();
|
||
void meld(maxHblt &y);
|
||
void initialize() {}
|
||
void display();
|
||
|
||
T& top() {return root->element.second;}
|
||
bool empty() {return bsize == 0;}
|
||
int size() {return bsize;}
|
||
void dispose(binaryTreeNode<std::pair<int, T>>* r) {
|
||
if (r == nullptr) return;
|
||
if (r->left != nullptr) dispose(r->left);
|
||
if (r->right != nullptr) dispose(r->right);
|
||
delete r;
|
||
}
|
||
};
|
||
|
||
template<class T>
|
||
maxHblt<T>::~maxHblt() {
|
||
dispose(root);
|
||
root = nullptr;
|
||
bsize = 0;
|
||
}
|
||
|
||
template<class T>
|
||
void maxHblt<T>::push(const T& x) {
|
||
if (root == nullptr) {
|
||
root = new binaryTreeNode<std::pair<int, T>>(std::make_pair(1, x));
|
||
return;
|
||
}
|
||
auto tmp = new binaryTreeNode<std::pair<int, T>>(std::make_pair(1, x));
|
||
meld(root, tmp);
|
||
bsize++;
|
||
}
|
||
|
||
template<class T>
|
||
void maxHblt<T>::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<class T>
|
||
void maxHblt<T>::meld(maxHblt &y) {
|
||
meld(root, y.root);
|
||
bsize += y.bsize;
|
||
y.dispose(y.root);
|
||
y.root = nullptr;
|
||
y.bsize = 0;
|
||
}
|
||
|
||
template<class T>
|
||
void maxHblt<T>::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<class T>
|
||
void maxHblt<T>::meld(binaryTreeNode<std::pair<int, T>>*& x, binaryTreeNode<std::pair<int, T>>*& y) {
|
||
if (y == nullptr) return;
|
||
if (x == nullptr) {
|
||
x = y;
|
||
//一定要y = nullptr
|
||
y = nullptr;
|
||
return;
|
||
}
|
||
|
||
if (x->element.second < y->element.second) {
|
||
std::swap(x, y);
|
||
}
|
||
|
||
meld(x->right, y);
|
||
|
||
if (x->left == nullptr) {
|
||
x->left = x->right;
|
||
x->right = nullptr;
|
||
x->element.first = 1;
|
||
}
|
||
else {
|
||
if (x->left->element.first < x->right->element.first) {
|
||
std::swap(x->left, x->right);
|
||
}
|
||
int rightNpl = (x->right ? x->right->element.first : 0);
|
||
x->element.first = rightNpl + 1;
|
||
}
|
||
}
|
||
|
||
#endif //MAXHBLT_H
|