From 17105e9e9e9263c40a7f46715b77bdd32808a99a Mon Sep 17 00:00:00 2001 From: e2hang <2099307493@qq.com> Date: Sat, 9 Aug 2025 19:23:37 +0800 Subject: [PATCH] Altered --- .../BinaryTreeRealise}/CMakeLists.txt | 0 .../BinaryTreeRealise}/CMakeList的具体写法 | 0 .../BinaryTreeRealise}/binaryTree.h | 0 .../BinaryTreeRealise}/binaryTreeNode.h | 0 .../BinaryTreeRealise}/linkedBinaryTree.h | 0 .../BinaryTreeRealise}/main.cpp | 0 .../priorityQueue/maxHeap/CMakeLists.txt | 6 + BinaryTree/priorityQueue/maxHeap/main.cpp | 78 +++++++++++++ BinaryTree/priorityQueue/maxHeap/maxHeap.h | 110 ++++++++++++++++++ .../priorityQueueArray}/CMakeLists.txt | 0 .../priorityQueueArray}/main.cpp | 0 .../priorityQueueArray}/maxPriorityQueue.h | 0 .../maxPriorityQueueRealise.h | 0 13 files changed, 194 insertions(+) rename {LinearList/BinaryTree => BinaryTree/BinaryTreeRealise}/CMakeLists.txt (100%) rename {LinearList/BinaryTree => BinaryTree/BinaryTreeRealise}/CMakeList的具体写法 (100%) rename {LinearList/BinaryTree => BinaryTree/BinaryTreeRealise}/binaryTree.h (100%) rename {LinearList/BinaryTree => BinaryTree/BinaryTreeRealise}/binaryTreeNode.h (100%) rename {LinearList/BinaryTree => BinaryTree/BinaryTreeRealise}/linkedBinaryTree.h (100%) rename {LinearList/BinaryTree => BinaryTree/BinaryTreeRealise}/main.cpp (100%) create mode 100644 BinaryTree/priorityQueue/maxHeap/CMakeLists.txt create mode 100644 BinaryTree/priorityQueue/maxHeap/main.cpp create mode 100644 BinaryTree/priorityQueue/maxHeap/maxHeap.h rename {LinearList/priorityQueue => BinaryTree/priorityQueue/priorityQueueArray}/CMakeLists.txt (100%) rename {LinearList/priorityQueue => BinaryTree/priorityQueue/priorityQueueArray}/main.cpp (100%) rename {LinearList/priorityQueue => BinaryTree/priorityQueue/priorityQueueArray}/maxPriorityQueue.h (100%) rename {LinearList/priorityQueue => BinaryTree/priorityQueue/priorityQueueArray}/maxPriorityQueueRealise.h (100%) diff --git a/LinearList/BinaryTree/CMakeLists.txt b/BinaryTree/BinaryTreeRealise/CMakeLists.txt similarity index 100% rename from LinearList/BinaryTree/CMakeLists.txt rename to BinaryTree/BinaryTreeRealise/CMakeLists.txt diff --git a/LinearList/BinaryTree/CMakeList的具体写法 b/BinaryTree/BinaryTreeRealise/CMakeList的具体写法 similarity index 100% rename from LinearList/BinaryTree/CMakeList的具体写法 rename to BinaryTree/BinaryTreeRealise/CMakeList的具体写法 diff --git a/LinearList/BinaryTree/binaryTree.h b/BinaryTree/BinaryTreeRealise/binaryTree.h similarity index 100% rename from LinearList/BinaryTree/binaryTree.h rename to BinaryTree/BinaryTreeRealise/binaryTree.h diff --git a/LinearList/BinaryTree/binaryTreeNode.h b/BinaryTree/BinaryTreeRealise/binaryTreeNode.h similarity index 100% rename from LinearList/BinaryTree/binaryTreeNode.h rename to BinaryTree/BinaryTreeRealise/binaryTreeNode.h diff --git a/LinearList/BinaryTree/linkedBinaryTree.h b/BinaryTree/BinaryTreeRealise/linkedBinaryTree.h similarity index 100% rename from LinearList/BinaryTree/linkedBinaryTree.h rename to BinaryTree/BinaryTreeRealise/linkedBinaryTree.h diff --git a/LinearList/BinaryTree/main.cpp b/BinaryTree/BinaryTreeRealise/main.cpp similarity index 100% rename from LinearList/BinaryTree/main.cpp rename to BinaryTree/BinaryTreeRealise/main.cpp diff --git a/BinaryTree/priorityQueue/maxHeap/CMakeLists.txt b/BinaryTree/priorityQueue/maxHeap/CMakeLists.txt new file mode 100644 index 0000000..483240a --- /dev/null +++ b/BinaryTree/priorityQueue/maxHeap/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.31) +project(maxHeap) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(maxHeap main.cpp) diff --git a/BinaryTree/priorityQueue/maxHeap/main.cpp b/BinaryTree/priorityQueue/maxHeap/main.cpp new file mode 100644 index 0000000..ad73043 --- /dev/null +++ b/BinaryTree/priorityQueue/maxHeap/main.cpp @@ -0,0 +1,78 @@ +#include "maxHeap.h" +using namespace std; +int main() { + maxHeap 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 +*/ \ No newline at end of file diff --git a/BinaryTree/priorityQueue/maxHeap/maxHeap.h b/BinaryTree/priorityQueue/maxHeap/maxHeap.h new file mode 100644 index 0000000..efdffed --- /dev/null +++ b/BinaryTree/priorityQueue/maxHeap/maxHeap.h @@ -0,0 +1,110 @@ +// +// Created by PC on 25-8-9. +// + +#ifndef MAXHEAP_H +#define MAXHEAP_H +#include +#include +#include + +template +class maxHeap { +private: + std::vector 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 +bool maxHeap::empty() const { + return data.size() == 0; +} + +template +int maxHeap::size() const { + return dsize; +} + +template +void maxHeap::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 +void maxHeap::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 +T & maxHeap::top() { + if (data.empty()) { + std::cerr << "Empty Heap" << std::endl; + } + else { + return data[0]; + } +} + +template +void maxHeap::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 diff --git a/LinearList/priorityQueue/CMakeLists.txt b/BinaryTree/priorityQueue/priorityQueueArray/CMakeLists.txt similarity index 100% rename from LinearList/priorityQueue/CMakeLists.txt rename to BinaryTree/priorityQueue/priorityQueueArray/CMakeLists.txt diff --git a/LinearList/priorityQueue/main.cpp b/BinaryTree/priorityQueue/priorityQueueArray/main.cpp similarity index 100% rename from LinearList/priorityQueue/main.cpp rename to BinaryTree/priorityQueue/priorityQueueArray/main.cpp diff --git a/LinearList/priorityQueue/maxPriorityQueue.h b/BinaryTree/priorityQueue/priorityQueueArray/maxPriorityQueue.h similarity index 100% rename from LinearList/priorityQueue/maxPriorityQueue.h rename to BinaryTree/priorityQueue/priorityQueueArray/maxPriorityQueue.h diff --git a/LinearList/priorityQueue/maxPriorityQueueRealise.h b/BinaryTree/priorityQueue/priorityQueueArray/maxPriorityQueueRealise.h similarity index 100% rename from LinearList/priorityQueue/maxPriorityQueueRealise.h rename to BinaryTree/priorityQueue/priorityQueueArray/maxPriorityQueueRealise.h