AlteredTree

This commit is contained in:
e2hang
2025-08-12 12:09:53 +08:00
parent ba9ceaab14
commit a2e3b28ac0
8 changed files with 350 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.31)
project(indexbinarySearchTree)
set(CMAKE_CXX_STANDARD 20)
add_executable(indexbinarySearchTree main.cpp)

View File

@@ -0,0 +1,19 @@
//
// Created by PC on 25-8-11.
//
#ifndef BINARYTREENODE_H
#define BINARYTREENODE_H
#include <utility>
template<class K, class E>
class binaryTreeNode {
public:
std::pair<K, E> element;
binaryTreeNode<K, E>* left;
binaryTreeNode<K, E>* right;
binaryTreeNode() : left(nullptr), right(nullptr){}
explicit binaryTreeNode(const std::pair<K, E>& e) : element(e), left(nullptr), right(nullptr) {}
binaryTreeNode(const std::pair<K, E>& e, binaryTreeNode<K, E>* l, binaryTreeNode<K, E>* r) : element(e), left(l), right(r) {}
};
#endif //BINARYTREENODE_H

View File

@@ -0,0 +1,131 @@
#ifndef INDEXBINARYSEARCHTREE_H
#define INDEXBINARYSEARCHTREE_H
#include <iostream>
#include "binaryTreeNode.h"
template<class K, class E>
class indexBinarySearchTree {
private:
binaryTreeNode<K, E>* root;
int treeSize;
int leftSize;
void inOrder(binaryTreeNode<K, E>* node);
public:
indexBinarySearchTree() : root(nullptr), leftSize(0) {}
indexBinarySearchTree(const indexBinarySearchTree<K, E>& e) : leftSize(e.leftSize) {
//深拷贝 采取中序遍历的方法
}
std::pair<K, E>* find(const K& e);
void insert(const std::pair<K, E>& e);
void erase(const K& e);
void ascent();
};
template<class K, class E>
void indexBinarySearchTree<K, E>::inOrder(binaryTreeNode<K, E> *node) {
if (node == nullptr) return;
inOrder(node->left);
std::cout << node->element.second << " ";
inOrder(node->right);
}
template<class K, class E>
std::pair<K, E> * indexBinarySearchTree<K, E>::find(const K &e) {
binaryTreeNode<K, E> *current = root;
while (current != nullptr) {
if (current->element.first < e) {
current = current->right;
}
else if (current->element.first > e) {
current = current->left;
}
else {
return &current->element;
}
}
//没有匹配的
return nullptr;
}
template<class K, class E>
void indexBinarySearchTree<K, E>::insert(const std::pair<K, E> &e) {
binaryTreeNode<K, E>* pp = nullptr;
binaryTreeNode<K, E>* p = root;
while (p != nullptr) {
pp = p;
if (e.first < p->element.first) {
p = p->left;
}
else if (e.first > p->element.first) {
p = p->right;
}
else {
p->element.second = e.second;
return;
}
}
auto newNode = new binaryTreeNode<K, E>(e);
if (root != nullptr) {
if (e.first < pp->element.first) {
pp->left = newNode;
}
else if (e.first > pp->element.first) {
pp->right = newNode;
}
}
else {
root = newNode;
}
treeSize++;
}
template<class K, class E>
void indexBinarySearchTree<K, E>::erase(const K &e) {
//这里用find不好因为没法确定其父节点是哪个
binaryTreeNode<K, E>* p = root;
binaryTreeNode<K, E>* pp = nullptr;
bool isleft = false;
while (p != nullptr && p->element.first != e) {
pp = p;
if (e < p->element.first) {
p = p->left;
isleft = true;
}
else if (e > p->element.first) {
p = p->right;
isleft = false;
}
}
if (p == nullptr) return;
//p-nochild
if (p->left == nullptr && p->right == nullptr) {
if (pp->left == p) pp->left = nullptr;
else if (pp->right == p) pp->right = nullptr;
treeSize--;
delete p;
}
//p-child == 2
if (p->left != nullptr && p->right != nullptr) {
pp = p->right;
while (pp->left != nullptr) {
pp = pp->left;
}
p->element = pp->element;
treeSize--;
delete pp;
}
//p-child == 1
if (p->right == nullptr) {
isleft ? pp->left = p->left : pp->right = p->left;
}
if (p->left == nullptr) {
isleft ? pp->left = p->right : pp->right = p->right;
}
}
template<class K, class E>
void indexBinarySearchTree<K, E>::ascent(){
inOrder(root);
std::cout << ";" << std::endl;
}
#endif //INDEXBINARYSEARCHTREE_H

View File

@@ -0,0 +1,52 @@
#include "indexBinarySearchTree.h"
using namespace std;
int main() {
indexBinarySearchTree<int, string> t;
std::pair<int, string> p1 = std::make_pair(10, "10");
std::pair<int, string> p2 = std::make_pair(12, "12");
std::pair<int, string> p3 = std::make_pair(30, "30");
std::pair<int, string> p4 = std::make_pair(24, "24");
std::pair<int, string> p5 = std::make_pair(35, "35");
std::pair<int, string> p6 = std::make_pair(16, "16");
std::pair<int, string> p7 = std::make_pair(27, "27");
std::pair<int, string> p8 = std::make_pair(81, "81");
std::pair<int, string> p9 = std::make_pair(29, "29");
std::pair<int, string> p10 = std::make_pair(20, "20");
t.insert(p1);
t.ascent();
t.insert(p2);
t.ascent();
t.insert(p3);
t.ascent();
t.insert(p4);
t.ascent();
t.insert(p5);
t.ascent();
t.insert(p6);
t.ascent();
t.insert(p7);
t.ascent();
t.insert(p8);
t.ascent();
t.insert(p9);
t.ascent();
t.insert(p10);
t.ascent();
t.erase(16);
t.ascent();
return 0;
}
/*
输出完美符合
10 ;
10 12 ;
10 12 30 ;
10 12 24 30 ;
10 12 24 30 35 ;
10 12 16 24 30 35 ;
10 12 16 24 27 30 35 ;
10 12 16 24 27 30 35 81 ;
10 12 16 24 27 29 30 35 81 ;
10 12 16 20 24 27 29 30 35 81 ;
10 12 20 24 27 29 30 35 81 ;
*/

View File

@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.31)
project(winnerTree)
set(CMAKE_CXX_STANDARD 20)
add_executable(winnerTree main.cpp)

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

View File

@@ -0,0 +1,41 @@
#include "winnerTree.h"
using namespace std;
int main() {
//输入的时候d反转一下好像没必要哈
deque<int> q;
q.push_back(1);
q.push_back(2);
q.push_back(3);
q.push_back(4);
q.push_back(5);
q.push_back(6);
q.push_back(7);
q.push_back(8);
q.push_back(9);
q.push_back(10);
winnerTree<int> t(q, 10);
t.init();
cout << t.winner() << endl;
t.display();
cout << endl;
t.replay(6, 11);
cout << t.winner() << endl;
t.display();
return 0;
}
/*
非常正确的竞赛树
10
10
10 8
4 10 8 6
4 2 10 9 8 7 6 5
4 3 2 1
11
11
10 11
4 10 8 11
4 2 10 9 8 7 11 5
4 3 2 1
*/

View File

@@ -0,0 +1,95 @@
//
// Created by PC on 25-8-11.
//
#ifndef WINNERTREE_H
#define WINNERTREE_H
#include <algorithm>
#include <iostream>
#include <deque>
template <class T>
class winnerTree {
private:
std::deque<T> data;
int playerNum;
bool isInit = false;
public:
winnerTree() : playerNum(0) {}
winnerTree(const std::deque<T>& d,int playerNum) : playerNum(playerNum), data(d) {}
~winnerTree() = default;
void init();
int winner();
void replay(const T& player, const T& alter);
void display();
};
template<class T>
void winnerTree<T>::init() {
for (int i = 0; i < 2 * playerNum - 2; i+=2) {
T max = std::min(data.at(i), data.at(i + 1));
data.push_back(max);
}
std::reverse(data.begin(), data.end());
isInit = true;
}
template<class T>
int winnerTree<T>::winner() {
if (!isInit) {
std::cout << "Uninitialized!" << std::endl;
return -1;
}
return data.front();
}
template<class T>
void winnerTree<T>::replay(const T &player, const T &alter) {
int index = -1;
// 查找叶子节点可改为用player id更稳妥
for (int i = data.size() - playerNum; i < data.size(); i++) {
if (data[i] == player) {
data[i] = alter;
index = i;
break;
}
}
if (index == -1) {
std::cout << "No such player!" << std::endl;
return;
}
// 向上调整父节点
while (index > 0) {
int parent = (index - 1) / 2;
int against = (index % 2 == 0) ? index - 1 : index + 1;
// 判断against是否越界
if (against >= data.size()) against = index; // 没对手时自己跟自己比
T maxVal = std::min(data.at(index), data.at(against));
if (data.at(parent) == maxVal) {
break; // 父节点胜者不变,提前结束
}
data.at(parent) = maxVal;
index = parent;
}
}
template<class T>
void winnerTree<T>::display() {
int cnt = 1;
int jmp = 1;
for (int i = 0; i < data.size(); ++i) {
cnt--;
std::cout << data.at(i) << " ";
if (cnt == 0) {
std::cout << std::endl;
jmp *= 2;
cnt = jmp;
}
}
}
#endif //WINNERTREE_H