Altered
This commit is contained in:
9
BinaryTree/BinaryTreeRealise/CMakeLists.txt
Normal file
9
BinaryTree/BinaryTreeRealise/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
cmake_minimum_required(VERSION 3.31)
|
||||
project(BTree)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(BTree main.cpp
|
||||
binaryTreeNode.h
|
||||
binaryTree.h
|
||||
linkedBinaryTree.h)
|
0
BinaryTree/BinaryTreeRealise/CMakeList的具体写法
Normal file
0
BinaryTree/BinaryTreeRealise/CMakeList的具体写法
Normal file
22
BinaryTree/BinaryTreeRealise/binaryTree.h
Normal file
22
BinaryTree/BinaryTreeRealise/binaryTree.h
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by PC on 25-8-6.
|
||||
//
|
||||
|
||||
#ifndef BINARYTREE_H
|
||||
#define BINARYTREE_H
|
||||
#include "binaryTreeNode.h"
|
||||
|
||||
template <class T>
|
||||
class binaryTree {
|
||||
public:
|
||||
virtual ~binaryTree() = default;
|
||||
virtual bool empty() const = 0;
|
||||
virtual int size() const = 0;
|
||||
virtual void preOrder(void(*) (binaryTreeNode<T>*) ) const = 0;
|
||||
virtual void inOrder(void(*) (binaryTreeNode<T>*) ) const = 0;
|
||||
virtual void postOrder(void(*) (binaryTreeNode<T>*) ) const = 0;
|
||||
virtual void levelOrder(void(*) (binaryTreeNode<T>*) ) const = 0;
|
||||
};
|
||||
|
||||
|
||||
#endif //BINARYTREE_H
|
22
BinaryTree/BinaryTreeRealise/binaryTreeNode.h
Normal file
22
BinaryTree/BinaryTreeRealise/binaryTreeNode.h
Normal file
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by PC on 25-8-6.
|
||||
//
|
||||
|
||||
#ifndef BINARYTREENODE_H
|
||||
#define BINARYTREENODE_H
|
||||
|
||||
template<class T>
|
||||
class binaryTreeNode {
|
||||
public:
|
||||
T element;
|
||||
binaryTreeNode<T>* left;
|
||||
binaryTreeNode<T>* right;
|
||||
public:
|
||||
binaryTreeNode() {left = right = nullptr;}
|
||||
explicit binaryTreeNode(const T& e) : element(e), left(nullptr), right(nullptr) {}
|
||||
binaryTreeNode(const T& e, binaryTreeNode<T>* l, binaryTreeNode<T>* r) : element(e), left(l), right(r) {}
|
||||
~binaryTreeNode() = default;
|
||||
|
||||
};
|
||||
|
||||
#endif //BINARYTREENODE_H
|
119
BinaryTree/BinaryTreeRealise/linkedBinaryTree.h
Normal file
119
BinaryTree/BinaryTreeRealise/linkedBinaryTree.h
Normal file
@@ -0,0 +1,119 @@
|
||||
//
|
||||
// Created by PC on 25-8-6.
|
||||
//
|
||||
|
||||
#ifndef LINKEDBINARYTREE_H
|
||||
#define LINKEDBINARYTREE_H
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
|
||||
#include "binaryTree.h"
|
||||
#include "binaryTreeNode.h"
|
||||
template <class T>
|
||||
class linkedBinaryTree : public binaryTree<T> {
|
||||
private:
|
||||
static std::deque<binaryTreeNode<T> *> nodes;
|
||||
binaryTreeNode<T>* root;
|
||||
int tsize;
|
||||
static void (*visit)(binaryTreeNode<T>* node);//存储visit
|
||||
static void preOrder(binaryTreeNode<T>* node) {
|
||||
if (node == nullptr) return;
|
||||
|
||||
bool isLeaf = (node->left == nullptr && node->right == nullptr);
|
||||
if (!isLeaf) std::cout << "(";
|
||||
|
||||
visit(node);
|
||||
preOrder(node->left);
|
||||
preOrder(node->right);
|
||||
|
||||
if (!isLeaf) std::cout << ")";
|
||||
}
|
||||
|
||||
static void inOrder(binaryTreeNode<T>* node) {
|
||||
if (node == nullptr) return;
|
||||
|
||||
// 对非叶子节点加括号
|
||||
bool isLeaf = (node->left == nullptr && node->right == nullptr);
|
||||
if (!isLeaf) std::cout << "(";
|
||||
|
||||
inOrder(node->left);
|
||||
visit(node);
|
||||
inOrder(node->right);
|
||||
|
||||
if (!isLeaf) std::cout << ")";
|
||||
}
|
||||
|
||||
static void postOrder(binaryTreeNode<T>* node) {
|
||||
if (node == nullptr) return;
|
||||
|
||||
bool isLeaf = (node->left == nullptr && node->right == nullptr);
|
||||
if (!isLeaf) std::cout << "(";
|
||||
|
||||
postOrder(node->left);
|
||||
postOrder(node->right);
|
||||
visit(node);
|
||||
|
||||
if (!isLeaf) std::cout << ")";
|
||||
}
|
||||
|
||||
//层序遍历levelOrder,使用deque,这人好聪明!
|
||||
static void levelOrder(binaryTreeNode<T>* node) {
|
||||
std::deque<binaryTreeNode<T>*> tmp;
|
||||
tmp.push_back(node);
|
||||
nodes.push_back(node);
|
||||
//开始
|
||||
while (!tmp.empty()) {
|
||||
binaryTreeNode<T>* current = tmp.front();
|
||||
tmp.pop_front();
|
||||
|
||||
if (current->left != nullptr) {
|
||||
nodes.push_back(current->left);
|
||||
tmp.push_back(current->left);
|
||||
}
|
||||
if (current->right != nullptr) {
|
||||
nodes.push_back(current->right);
|
||||
tmp.push_back(current->right);
|
||||
}
|
||||
}
|
||||
for (binaryTreeNode<T>* x : nodes) {
|
||||
std::cout << x->element;
|
||||
}
|
||||
}
|
||||
|
||||
static void dispose(binaryTreeNode<T>* node) {
|
||||
if (node == nullptr) return;
|
||||
dispose(node->left);
|
||||
dispose(node->right);
|
||||
std::cout << "disposing " << node->element << std::endl;
|
||||
delete node;
|
||||
}
|
||||
public:
|
||||
linkedBinaryTree() {root = nullptr;tsize = 0;}
|
||||
explicit linkedBinaryTree(binaryTreeNode<T>* x ) {root = x;tsize = 1;}
|
||||
~linkedBinaryTree() override {erase();}
|
||||
bool empty() const override {return tsize == 0;}
|
||||
void preOrder(void(*Ivisit) (binaryTreeNode<T>*)) const override {visit = Ivisit; preOrder(root);}
|
||||
//传入的是一个函数,在private里有void (*visit)来储存函数入口,从而在遍历的时候进行操作(输出,计算等)
|
||||
//操作的函数是visit,即访问到该node之后进行什么操作
|
||||
void inOrder(void(*Ivisit) (binaryTreeNode<T>*)) const override{visit = Ivisit; inOrder(root);}
|
||||
void postOrder(void(*Ivisit) (binaryTreeNode<T>*)) const override {visit = Ivisit; postOrder(root);}
|
||||
void levelOrder(void(*Ivisit) (binaryTreeNode<T>*)) const override {visit = Ivisit; levelOrder(root);}
|
||||
int size() const override {return tsize;}
|
||||
void erase() {
|
||||
//postOrder(dispose);
|
||||
dispose(root);
|
||||
root = nullptr;
|
||||
tsize = 0;
|
||||
}
|
||||
};
|
||||
|
||||
//Remember to initiate this Definition by denifying it outside the class but inside the .h file
|
||||
|
||||
template<typename T>
|
||||
void (*linkedBinaryTree<T>::visit)(binaryTreeNode<T>*) = nullptr;
|
||||
|
||||
template<typename T>
|
||||
std::deque<binaryTreeNode<T> *> linkedBinaryTree<T>::nodes;
|
||||
|
||||
#endif //LINKEDBINARYTREE_H
|
55
BinaryTree/BinaryTreeRealise/main.cpp
Normal file
55
BinaryTree/BinaryTreeRealise/main.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "linkedBinaryTree.h"
|
||||
using namespace std;
|
||||
vector<string> m;
|
||||
void func(binaryTreeNode<string>* node) {
|
||||
cout << node->element;
|
||||
}
|
||||
|
||||
int main() {
|
||||
/*
|
||||
* x
|
||||
* + -
|
||||
* a b c d
|
||||
*/
|
||||
//构建要从下往上构建,注意如果是 new了一个[],必须delete []而不能直接delete t[i]
|
||||
auto t0 = new binaryTreeNode<string>("a");
|
||||
auto t1 = new binaryTreeNode<string>("b");
|
||||
auto t2 = new binaryTreeNode<string>("c");
|
||||
auto t3 = new binaryTreeNode<string>("d");
|
||||
auto t4 = new binaryTreeNode<string>("+", t0, t1);
|
||||
auto t5 = new binaryTreeNode<string>("-", t2, t3);
|
||||
auto t6 = new binaryTreeNode<string>("x", t4, t5);
|
||||
linkedBinaryTree<string> tree(t6);
|
||||
|
||||
cout << endl;
|
||||
tree.inOrder(func);
|
||||
cout << endl;
|
||||
|
||||
tree.preOrder(func);
|
||||
cout << endl;
|
||||
|
||||
tree.postOrder(func);
|
||||
cout << endl;
|
||||
|
||||
tree.levelOrder(func);
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Output:
|
||||
((a+b)x(c-d))
|
||||
(x(+ab)(-cd))
|
||||
((ab+)(cd-)x)
|
||||
x+-abcd
|
||||
disposing a
|
||||
disposing b
|
||||
disposing +
|
||||
disposing c
|
||||
disposing d
|
||||
disposing -
|
||||
disposing x
|
||||
*/
|
6
BinaryTree/priorityQueue/maxHeap/CMakeLists.txt
Normal file
6
BinaryTree/priorityQueue/maxHeap/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.31)
|
||||
project(maxHeap)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(maxHeap main.cpp)
|
78
BinaryTree/priorityQueue/maxHeap/main.cpp
Normal file
78
BinaryTree/priorityQueue/maxHeap/main.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "maxHeap.h"
|
||||
using namespace std;
|
||||
int main() {
|
||||
maxHeap<int> h;
|
||||
h.push(9);
|
||||
h.push(8);
|
||||
h.push(7);
|
||||
h.push(6);
|
||||
h.push(5);
|
||||
h.push(4);
|
||||
h.push(3);
|
||||
h.push(2);
|
||||
h.push(1);
|
||||
h.display();
|
||||
/*
|
||||
* 9
|
||||
* 8 7
|
||||
* 6 5 4 3
|
||||
* 2 1
|
||||
*/
|
||||
h.push(10);
|
||||
h.display();
|
||||
h.push(11);
|
||||
h.display();
|
||||
h.push(12);
|
||||
h.display();
|
||||
/*
|
||||
* 12
|
||||
* 10 11
|
||||
* 6 9 7 3
|
||||
* 2 1 5 8 4
|
||||
*/
|
||||
h.pop();
|
||||
h.display();
|
||||
h.pop();
|
||||
h.display();
|
||||
h.pop();
|
||||
h.display();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
输出结果与预测的一致
|
||||
9
|
||||
8 7
|
||||
6 5 4 3
|
||||
2 1
|
||||
|
||||
10
|
||||
9 7
|
||||
6 8 4 3
|
||||
2 1 5
|
||||
|
||||
11
|
||||
10 7
|
||||
6 9 4 3
|
||||
2 1 5 8
|
||||
|
||||
12
|
||||
10 11
|
||||
6 9 7 3
|
||||
2 1 5 8 4
|
||||
|
||||
11
|
||||
10 7
|
||||
6 9 4 3
|
||||
2 1 5 8
|
||||
|
||||
10
|
||||
9 7
|
||||
6 8 4 3
|
||||
2 1 5
|
||||
|
||||
9
|
||||
8 7
|
||||
6 5 4 3
|
||||
2 1
|
||||
*/
|
110
BinaryTree/priorityQueue/maxHeap/maxHeap.h
Normal file
110
BinaryTree/priorityQueue/maxHeap/maxHeap.h
Normal file
@@ -0,0 +1,110 @@
|
||||
//
|
||||
// Created by PC on 25-8-9.
|
||||
//
|
||||
|
||||
#ifndef MAXHEAP_H
|
||||
#define MAXHEAP_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <bits/ostream.tcc>
|
||||
|
||||
template<class T>
|
||||
class maxHeap {
|
||||
private:
|
||||
std::vector<T> data;
|
||||
int dsize;
|
||||
public:
|
||||
maxHeap() : dsize(0) {}
|
||||
~maxHeap() = default;
|
||||
|
||||
bool empty() const;
|
||||
int size() const;
|
||||
void pop();
|
||||
void push(const T& x);
|
||||
T& top();
|
||||
void display();
|
||||
};
|
||||
|
||||
template<class T>
|
||||
bool maxHeap<T>::empty() const {
|
||||
return data.size() == 0;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
int maxHeap<T>::size() const {
|
||||
return dsize;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void maxHeap<T>::pop() {
|
||||
if (data.empty()) {
|
||||
std::cerr << "Empty Heap!" << std::endl;
|
||||
return;
|
||||
}
|
||||
data.at(0) = data.at(data.size() - 1);
|
||||
dsize--;
|
||||
data.pop_back();
|
||||
//正确的下沉逻辑:(记住如下写法,短小精悍
|
||||
//先找左节点(如果有)再找右节点(如果有)比较左右节点,选择更大的那个作为可能交换的目标
|
||||
//如果当前节点已经比这个目标大 → 停 ;否则 → 交换并继续下沉
|
||||
int index = 0;
|
||||
int left = 2 * index + 1;
|
||||
int right = 2 * index + 2;
|
||||
while (left < data.size()) {
|
||||
int largest = index;
|
||||
if (data[left] > data[largest]) largest = left;
|
||||
if (data[right] > data[largest]) largest = right;
|
||||
if (largest == index) break;
|
||||
|
||||
std::swap(data[index], data[largest]);
|
||||
index = largest;
|
||||
left = 2 * index + 1;
|
||||
right = 2 * index + 2;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void maxHeap<T>::push(const T &x) {
|
||||
data.push_back(x); // 新元素放到末尾
|
||||
dsize++;
|
||||
|
||||
int idx = dsize - 1; // 当前节点下标
|
||||
while (idx > 0) {
|
||||
int parent = (idx - 1) / 2;
|
||||
if (data[idx] > data[parent]) { // 如果比父节点大,就交换
|
||||
std::swap(data[idx], data[parent]);
|
||||
idx = parent; // 继续向上
|
||||
} else {
|
||||
break; // 满足堆性质,结束
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
T & maxHeap<T>::top() {
|
||||
if (data.empty()) {
|
||||
std::cerr << "Empty Heap" << std::endl;
|
||||
}
|
||||
else {
|
||||
return data[0];
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void maxHeap<T>::display() {
|
||||
int jmp = 1;
|
||||
int cnt = jmp;
|
||||
for (int i = 0; i < data.size(); ++i) {
|
||||
std::cout << data.at(i) << " ";
|
||||
cnt--;
|
||||
if (cnt == 0) {
|
||||
jmp *= 2;
|
||||
cnt = jmp;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl << std::endl;
|
||||
}
|
||||
|
||||
#endif //MAXHEAP_H
|
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.31)
|
||||
project(priorityQueue)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(priorityQueue main.cpp
|
||||
maxPriorityQueue.h
|
||||
maxPriorityQueueRealise.h)
|
57
BinaryTree/priorityQueue/priorityQueueArray/main.cpp
Normal file
57
BinaryTree/priorityQueue/priorityQueueArray/main.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
#include "maxPriorityQueueRealise.h"
|
||||
using namespace std;
|
||||
int main() {
|
||||
maxPriorityQueueRealise<string> q;
|
||||
vector<pair<string, int>> v;
|
||||
v.push_back(make_pair("first", 9));
|
||||
v.push_back(make_pair("second", 8));
|
||||
v.push_back(make_pair("third", 7));
|
||||
v.push_back(make_pair("fourth", 6));
|
||||
v.push_back(make_pair("fifth", 5));
|
||||
v.push_back(make_pair("sixth", 4));
|
||||
v.push_back(make_pair("seventh", 1));
|
||||
v.push_back(make_pair("eighth", 2));
|
||||
v.push_back(make_pair("ninth", 3));
|
||||
q.push(v.at(4));
|
||||
q.push(v.at(3));
|
||||
q.push(v.at(2));
|
||||
q.push(v.at(1));
|
||||
q.push(v.at(0));
|
||||
q.push(v.at(5));
|
||||
q.push(v.at(6));
|
||||
q.push(v.at(7));
|
||||
q.push(v.at(8));
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
q.pop();
|
||||
cout << q.top() << endl;
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
*发现789的三个顺序倒过来了,类的设计正确
|
||||
*本类设计的是maxpriority,如果有重复的priority,优先处理先进入的,也就是FIFO
|
||||
*如果有需求修改可以把比较的时候加入等号,变成FILO
|
||||
first
|
||||
second
|
||||
third
|
||||
fourth
|
||||
fifth
|
||||
sixth
|
||||
ninth
|
||||
eighth
|
||||
seventh
|
||||
*/
|
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by PC on 25-8-9.
|
||||
//
|
||||
|
||||
#ifndef MAXPRIORITYQUEUE_H
|
||||
#define MAXPRIORITYQUEUE_H
|
||||
|
||||
template <class T>
|
||||
class maxPriorityQueue {
|
||||
public:
|
||||
virtual ~maxPriorityQueue() = default;
|
||||
virtual bool empty() const = 0;
|
||||
virtual int size() const = 0;
|
||||
virtual T& top() = 0;
|
||||
virtual void pop() = 0;
|
||||
virtual void push(const std::pair<T, int>& x) = 0;
|
||||
};
|
||||
|
||||
#endif //MAXPRIORITYQUEUE_H
|
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// Created by PC on 25-8-9.
|
||||
//
|
||||
|
||||
#ifndef MAXPRIORITYQUEUEREALISE_H
|
||||
#define MAXPRIORITYQUEUEREALISE_H
|
||||
#include "maxPriorityQueue.h"
|
||||
#include <vector>
|
||||
|
||||
template <class T>
|
||||
class maxPriorityQueueRealise : public maxPriorityQueue<T> {
|
||||
private:
|
||||
std::vector<std::pair<T, int>> queue; //T是元素,int是优先等级
|
||||
int qsize;
|
||||
public:
|
||||
maxPriorityQueueRealise() : qsize(0) {}
|
||||
|
||||
bool empty() const {return qsize == 0;}
|
||||
int size() const {return qsize;}
|
||||
T& top();
|
||||
void pop();
|
||||
void push(const std::pair<T, int>& x);
|
||||
|
||||
};
|
||||
|
||||
template<class T>
|
||||
T& maxPriorityQueueRealise<T>::top() {
|
||||
int max = -1;
|
||||
int pos = 0;
|
||||
int index = 0;
|
||||
for ( std::pair<T, int> x : queue ) {
|
||||
if (x.second > max) {
|
||||
max = x.second;
|
||||
pos = index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return queue[pos].first;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void maxPriorityQueueRealise<T>::pop() {
|
||||
int max = -1;
|
||||
int pos = 0;
|
||||
int index = 0;
|
||||
for ( std::pair<T, int> x : queue ) {
|
||||
if (x.second > max) {
|
||||
max = x.second;
|
||||
pos = index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
queue.erase(queue.begin() + pos);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void maxPriorityQueueRealise<T>::push(const std::pair<T, int>& x) {
|
||||
queue.push_back(x);
|
||||
}
|
||||
#endif //MAXPRIORITYQUEUEREALISE_H
|
Reference in New Issue
Block a user