Format C++ codes in Clang-Format Style: Microsoft
This commit is contained in:
parent
f8513455b5
commit
9c9c8b7574
@ -65,18 +65,17 @@ int find(int* nums, int size, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化数组 */
|
||||
int size = 5;
|
||||
int *arr = new int[size];
|
||||
cout << "数组 arr = ";
|
||||
PrintUtil::printArray(arr, size);
|
||||
printArray(arr, size);
|
||||
|
||||
int *nums = new int[size]{1, 3, 2, 5, 4};
|
||||
cout << "数组 nums = ";
|
||||
PrintUtil::printArray(nums, size);
|
||||
printArray(nums, size);
|
||||
|
||||
/* 随机访问 */
|
||||
int randomNum = randomAccess(nums, size);
|
||||
@ -87,17 +86,17 @@ int main() {
|
||||
nums = extend(nums, size, enlarge);
|
||||
size += enlarge;
|
||||
cout << "将数组长度扩展至 8 ,得到 nums = ";
|
||||
PrintUtil::printArray(nums, size);
|
||||
printArray(nums, size);
|
||||
|
||||
/* 插入元素 */
|
||||
insert(nums, size, 6, 3);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 nums = ";
|
||||
PrintUtil::printArray(nums, size);
|
||||
printArray(nums, size);
|
||||
|
||||
/* 删除元素 */
|
||||
remove(nums, size, 2);
|
||||
cout << "删除索引 2 处的元素,得到 nums = ";
|
||||
PrintUtil::printArray(nums, size);
|
||||
printArray(nums, size);
|
||||
|
||||
/* 遍历数组 */
|
||||
traverse(nums, size);
|
||||
|
@ -47,7 +47,6 @@ int find(ListNode* head, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化链表 */
|
||||
@ -63,17 +62,17 @@ int main() {
|
||||
n2->next = n3;
|
||||
n3->next = n4;
|
||||
cout << "初始化的链表为" << endl;
|
||||
PrintUtil::printLinkedList(n0);
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 插入节点 */
|
||||
insert(n0, new ListNode(0));
|
||||
cout << "插入节点后的链表为" << endl;
|
||||
PrintUtil::printLinkedList(n0);
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 删除节点 */
|
||||
remove(n0);
|
||||
cout << "删除节点后的链表为" << endl;
|
||||
PrintUtil::printLinkedList(n0);
|
||||
printLinkedList(n0);
|
||||
|
||||
/* 访问节点 */
|
||||
ListNode *node = access(n0, 3);
|
||||
|
@ -6,13 +6,12 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
vector<int> list = {1, 3, 2, 5, 4};
|
||||
cout << "列表 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list[1];
|
||||
@ -21,12 +20,12 @@ int main() {
|
||||
/* 更新元素 */
|
||||
list[1] = 0;
|
||||
cout << "将索引 1 处的元素更新为 0 ,得到 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 清空列表 */
|
||||
list.clear();
|
||||
cout << "清空列表后 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 尾部添加元素 */
|
||||
list.push_back(1);
|
||||
@ -35,17 +34,17 @@ int main() {
|
||||
list.push_back(5);
|
||||
list.push_back(4);
|
||||
cout << "添加元素后 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 中间插入元素 */
|
||||
list.insert(list.begin() + 3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 删除元素 */
|
||||
list.erase(list.begin() + 3);
|
||||
cout << "删除索引 3 处的元素,得到 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 通过索引遍历列表 */
|
||||
int count = 0;
|
||||
@ -63,12 +62,12 @@ int main() {
|
||||
vector<int> list1 = {6, 8, 7, 10, 9};
|
||||
list.insert(list.end(), list1.begin(), list1.end());
|
||||
cout << "将列表 list1 拼接到 list 之后,得到 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
/* 排序列表 */
|
||||
sort(list.begin(), list.end());
|
||||
cout << "排序列表后 list = ";
|
||||
PrintUtil::printVector(list);
|
||||
printVector(list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -117,7 +117,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化列表 */
|
||||
@ -130,20 +129,20 @@ int main() {
|
||||
list->add(4);
|
||||
cout << "列表 list = ";
|
||||
vector<int> vec = list->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
cout << "容量 = " << list->capacity() << " ,长度 = " << list->size() << endl;
|
||||
|
||||
/* 中间插入元素 */
|
||||
list->insert(3, 6);
|
||||
cout << "在索引 3 处插入数字 6 ,得到 list = ";
|
||||
vec = list->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
/* 删除元素 */
|
||||
list->remove(3);
|
||||
cout << "删除索引 3 处的元素,得到 list = ";
|
||||
vec = list->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
/* 访问元素 */
|
||||
int num = list->get(1);
|
||||
@ -153,7 +152,7 @@ int main() {
|
||||
list->set(1, 0);
|
||||
cout << "将索引 1 处的元素更新为 0 ,得到 list = ";
|
||||
vec = list->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
/* 测试扩容机制 */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
@ -162,7 +161,7 @@ int main() {
|
||||
}
|
||||
cout << "扩容后的列表 list = ";
|
||||
vec = list->toVector();
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
cout << "容量 = " << list->capacity() << " ,长度 = " << list->size() << endl;
|
||||
|
||||
// 释放内存
|
||||
|
@ -34,7 +34,6 @@ vector<int> twoSumHashTable(vector<int>& nums, int target) {
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
// ======= Test Case =======
|
||||
vector<int> nums = {2, 7, 11, 15};
|
||||
@ -44,11 +43,11 @@ int main() {
|
||||
// 方法一
|
||||
vector<int> res = twoSumBruteForce(nums, target);
|
||||
cout << "方法一 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
printVector(res);
|
||||
// 方法二
|
||||
res = twoSumHashTable(nums, target);
|
||||
cout << "方法二 res = ";
|
||||
PrintUtil::printVector(res);
|
||||
printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -48,7 +48,8 @@ void linear(int n) {
|
||||
/* 线性阶(递归实现) */
|
||||
void linearRecur(int n) {
|
||||
cout << "递归 n = " << n << endl;
|
||||
if (n == 1) return;
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
@ -67,7 +68,8 @@ void quadratic(int n) {
|
||||
|
||||
/* 平方阶(递归实现) */
|
||||
int quadraticRecur(int n) {
|
||||
if (n <= 0) return 0;
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
vector<int> nums(n);
|
||||
cout << "递归 n = " << n << " 中的 nums 长度 = " << nums.size() << endl;
|
||||
return quadraticRecur(n - 1);
|
||||
@ -75,14 +77,14 @@ int quadraticRecur(int n) {
|
||||
|
||||
/* 指数阶(建立满二叉树) */
|
||||
TreeNode *buildTree(int n) {
|
||||
if (n == 0) return nullptr;
|
||||
if (n == 0)
|
||||
return nullptr;
|
||||
TreeNode *root = new TreeNode(0);
|
||||
root->left = buildTree(n - 1);
|
||||
root->right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 5;
|
||||
@ -96,7 +98,7 @@ int main() {
|
||||
quadraticRecur(n);
|
||||
// 指数阶
|
||||
TreeNode *root = buildTree(n);
|
||||
PrintUtil::printTree(root);
|
||||
printTree(root);
|
||||
|
||||
// 释放内存
|
||||
freeMemoryTree(root);
|
||||
|
@ -80,7 +80,8 @@ int exponential(int n) {
|
||||
|
||||
/* 指数阶(递归实现) */
|
||||
int expRecur(int n) {
|
||||
if (n == 1) return 1;
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
@ -96,15 +97,16 @@ int logarithmic(float n) {
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
int logRecur(float n) {
|
||||
if (n <= 1) return 0;
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
int linearLogRecur(float n) {
|
||||
if (n <= 1) return 1;
|
||||
int count = linearLogRecur(n / 2) +
|
||||
linearLogRecur(n / 2);
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
@ -113,7 +115,8 @@ int linearLogRecur(float n) {
|
||||
|
||||
/* 阶乘阶(递归实现) */
|
||||
int factorialRecur(int n) {
|
||||
if (n == 0) return 1;
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// 从 1 个分裂出 n 个
|
||||
for (int i = 0; i < n; i++) {
|
||||
@ -122,7 +125,6 @@ int factorialRecur(int n) {
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
|
@ -31,7 +31,6 @@ int findOne(vector<int>& nums) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
@ -39,7 +38,7 @@ int main() {
|
||||
vector<int> nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
cout << "\n数组 [ 1, 2, ..., n ] 被打乱后 = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
cout << "数字 1 的索引为 " << index << endl;
|
||||
}
|
||||
return 0;
|
||||
|
@ -7,19 +7,15 @@
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList
|
||||
{
|
||||
class GraphAdjList {
|
||||
public:
|
||||
// 邻接表,key: 顶点,value:该顶点的所有邻接顶点
|
||||
unordered_map<Vertex *, vector<Vertex *>> adjList;
|
||||
|
||||
/* 在 vector 中删除指定节点 */
|
||||
void remove(vector<Vertex *> &vec, Vertex *vet)
|
||||
{
|
||||
for (int i = 0; i < vec.size(); i++)
|
||||
{
|
||||
if (vec[i] == vet)
|
||||
{
|
||||
void remove(vector<Vertex *> &vec, Vertex *vet) {
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == vet) {
|
||||
vec.erase(vec.begin() + i);
|
||||
break;
|
||||
}
|
||||
@ -27,11 +23,9 @@ public:
|
||||
}
|
||||
|
||||
/* 构造方法 */
|
||||
GraphAdjList(const vector<vector<Vertex *>> &edges)
|
||||
{
|
||||
GraphAdjList(const vector<vector<Vertex *>> &edges) {
|
||||
// 添加所有顶点和边
|
||||
for (const vector<Vertex *> &edge : edges)
|
||||
{
|
||||
for (const vector<Vertex *> &edge : edges) {
|
||||
addVertex(edge[0]);
|
||||
addVertex(edge[1]);
|
||||
addEdge(edge[0], edge[1]);
|
||||
@ -39,11 +33,12 @@ public:
|
||||
}
|
||||
|
||||
/* 获取顶点数量 */
|
||||
int size() { return adjList.size(); }
|
||||
int size() {
|
||||
return adjList.size();
|
||||
}
|
||||
|
||||
/* 添加边 */
|
||||
void addEdge(Vertex *vet1, Vertex *vet2)
|
||||
{
|
||||
void addEdge(Vertex *vet1, Vertex *vet2) {
|
||||
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
||||
throw invalid_argument("不存在顶点");
|
||||
// 添加边 vet1 - vet2
|
||||
@ -52,8 +47,7 @@ public:
|
||||
}
|
||||
|
||||
/* 删除边 */
|
||||
void removeEdge(Vertex *vet1, Vertex *vet2)
|
||||
{
|
||||
void removeEdge(Vertex *vet1, Vertex *vet2) {
|
||||
if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)
|
||||
throw invalid_argument("不存在顶点");
|
||||
// 删除边 vet1 - vet2
|
||||
@ -62,8 +56,7 @@ public:
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
void addVertex(Vertex *vet)
|
||||
{
|
||||
void addVertex(Vertex *vet) {
|
||||
if (adjList.count(vet))
|
||||
return;
|
||||
// 在邻接表中添加一个新链表
|
||||
@ -71,29 +64,25 @@ public:
|
||||
}
|
||||
|
||||
/* 删除顶点 */
|
||||
void removeVertex(Vertex *vet)
|
||||
{
|
||||
void removeVertex(Vertex *vet) {
|
||||
if (!adjList.count(vet))
|
||||
throw invalid_argument("不存在顶点");
|
||||
// 在邻接表中删除顶点 vet 对应的链表
|
||||
adjList.erase(vet);
|
||||
// 遍历其他顶点的链表,删除所有包含 vet 的边
|
||||
for (auto &[key, vec] : adjList)
|
||||
{
|
||||
remove(vec, vet);
|
||||
for (auto &adj : adjList) {
|
||||
remove(adj.second, vet);
|
||||
}
|
||||
}
|
||||
|
||||
/* 打印邻接表 */
|
||||
void print()
|
||||
{
|
||||
void print() {
|
||||
cout << "邻接表 =" << endl;
|
||||
for (auto &adj : adjList)
|
||||
{
|
||||
for (auto &adj : adjList) {
|
||||
const auto &key = adj.first;
|
||||
const auto &vec = adj.second;
|
||||
cout << key->val << ": ";
|
||||
PrintUtil::printVector(vetsToVals(vec));
|
||||
printVector(vetsToVals(vec));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -84,9 +84,9 @@ public:
|
||||
/* 打印邻接矩阵 */
|
||||
void print() {
|
||||
cout << "顶点列表 = ";
|
||||
PrintUtil::printVector(vertices);
|
||||
printVector(vertices);
|
||||
cout << "邻接矩阵 =" << endl;
|
||||
PrintUtil::printVectorMatrix(adjMat);
|
||||
printVectorMatrix(adjMat);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -48,7 +48,7 @@ int main() {
|
||||
/* 广度优先遍历 BFS */
|
||||
vector<Vertex *> res = graphBFS(graph, v[0]);
|
||||
cout << "\n广度优先遍历(BFS)顶点序列为" << endl;
|
||||
PrintUtil::printVector(vetsToVals(res));
|
||||
printVector(vetsToVals(res));
|
||||
|
||||
// 释放内存
|
||||
for (Vertex *vet : v) {
|
||||
|
@ -44,7 +44,7 @@ int main() {
|
||||
/* 深度优先遍历 DFS */
|
||||
vector<Vertex *> res = graphDFS(graph, v[0]);
|
||||
cout << "\n深度优先遍历(DFS)顶点序列为" << endl;
|
||||
PrintUtil::printVector(vetsToVals(res));
|
||||
printVector(vetsToVals(res));
|
||||
|
||||
// 释放内存
|
||||
for (Vertex *vet : v) {
|
||||
|
@ -21,6 +21,7 @@ public:
|
||||
class ArrayHashMap {
|
||||
private:
|
||||
vector<Entry *> buckets;
|
||||
|
||||
public:
|
||||
ArrayHashMap() {
|
||||
// 初始化数组,包含 100 个桶
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化哈希表 */
|
||||
@ -20,7 +19,7 @@ int main() {
|
||||
map[13276] = "小法";
|
||||
map[10583] = "小鸭";
|
||||
cout << "\n添加完成后,哈希表为\nKey -> Value" << endl;
|
||||
PrintUtil::printHashMap(map);
|
||||
printHashMap(map);
|
||||
|
||||
/* 查询操作 */
|
||||
// 向哈希表输入键 key ,得到值 value
|
||||
@ -31,7 +30,7 @@ int main() {
|
||||
// 在哈希表中删除键值对 (key, value)
|
||||
map.erase(10583);
|
||||
cout << "\n删除 10583 后,哈希表为\nKey -> Value" << endl;
|
||||
PrintUtil::printHashMap(map);
|
||||
printHashMap(map);
|
||||
|
||||
/* 遍历哈希表 */
|
||||
cout << "\n遍历键值对 Key->Value" << endl;
|
||||
|
@ -6,23 +6,20 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
void testPush(priority_queue<int> &heap, int val)
|
||||
{
|
||||
void testPush(priority_queue<int> &heap, int val) {
|
||||
heap.push(val); // 元素入堆
|
||||
cout << "\n元素 " << val << " 入堆后" << endl;
|
||||
PrintUtil::printHeap(heap);
|
||||
printHeap(heap);
|
||||
}
|
||||
|
||||
void testPop(priority_queue<int> &heap)
|
||||
{
|
||||
void testPop(priority_queue<int> &heap) {
|
||||
int val = heap.top();
|
||||
heap.pop();
|
||||
cout << "\n堆顶元素 " << val << " 出堆后" << endl;
|
||||
PrintUtil::printHeap(heap);
|
||||
printHeap(heap);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
// priority_queue<int, vector<int>, greater<int>> minHeap;
|
||||
@ -62,6 +59,6 @@ int main()
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
cout << "输入列表并建立小顶堆后" << endl;
|
||||
PrintUtil::printHeap(minHeap);
|
||||
printHeap(minHeap);
|
||||
return 0;
|
||||
}
|
@ -112,15 +112,14 @@ public:
|
||||
/* 打印堆(二叉树)*/
|
||||
void print() {
|
||||
cout << "堆的数组表示:";
|
||||
PrintUtil::printVector(maxHeap);
|
||||
printVector(maxHeap);
|
||||
cout << "堆的树状表示:" << endl;
|
||||
TreeNode *root = vecToTree(maxHeap);
|
||||
PrintUtil::printTree(root);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化大顶堆 */
|
||||
|
@ -42,7 +42,6 @@ int binarySearch1(vector<int>& nums, int target) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 6;
|
||||
|
@ -24,7 +24,6 @@ ListNode* hashingSearchLinkedList(unordered_map<int, ListNode*> map, int target)
|
||||
return map[target];
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 3;
|
||||
|
@ -31,7 +31,6 @@ ListNode* linearSearchLinkedList(ListNode* head, int target) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int target = 3;
|
||||
|
@ -35,22 +35,22 @@ void bubbleSortWithFlag(vector<int>& nums) {
|
||||
flag = true; // 记录交换元素
|
||||
}
|
||||
}
|
||||
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
if (!flag)
|
||||
break; // 此轮冒泡未交换任何元素,直接跳出
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||
bubbleSort(nums);
|
||||
cout << "冒泡排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
vector<int> nums1 = {4, 1, 3, 1, 5, 2};
|
||||
bubbleSortWithFlag(nums1);
|
||||
cout << "冒泡排序完成后 nums1 = ";
|
||||
PrintUtil::printVector(nums1);
|
||||
printVector(nums1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -7,44 +7,38 @@
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 桶排序 */
|
||||
void bucketSort(vector<float> &nums)
|
||||
{
|
||||
void bucketSort(vector<float> &nums) {
|
||||
// 初始化 k = n/2 个桶,预期向每个桶分配 2 个元素
|
||||
int k = nums.size() / 2;
|
||||
vector<vector<float>> buckets(k);
|
||||
// 1. 将数组元素分配到各个桶中
|
||||
for (float num : nums)
|
||||
{
|
||||
for (float num : nums) {
|
||||
// 输入数据范围 [0, 1),使用 num * k 映射到索引范围 [0, k-1]
|
||||
int i = num * k;
|
||||
// 将 num 添加进桶 bucket_idx
|
||||
buckets[i].push_back(num);
|
||||
}
|
||||
// 2. 对各个桶执行排序
|
||||
for (vector<float> &bucket : buckets)
|
||||
{
|
||||
for (vector<float> &bucket : buckets) {
|
||||
// 使用内置排序函数,也可以替换成其他排序算法
|
||||
sort(bucket.begin(), bucket.end());
|
||||
}
|
||||
// 3. 遍历桶合并结果
|
||||
int i = 0;
|
||||
for (vector<float> &bucket : buckets)
|
||||
{
|
||||
for (float num : bucket)
|
||||
{
|
||||
for (vector<float> &bucket : buckets) {
|
||||
for (float num : bucket) {
|
||||
nums[i++] = num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// 设输入数据为浮点数,范围为 [0, 1)
|
||||
vector<float> nums = {0.49f, 0.96f, 0.82f, 0.09f, 0.57f, 0.43f, 0.91f, 0.75f, 0.15f, 0.37f};
|
||||
bucketSort(nums);
|
||||
cout << "桶排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -66,12 +66,12 @@ int main() {
|
||||
vector<int> nums = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
|
||||
countingSortNaive(nums);
|
||||
cout << "计数排序(无法排序对象)完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
vector<int> nums1 = {1, 0, 1, 2, 0, 4, 0, 2, 2, 4};
|
||||
countingSort(nums1);
|
||||
cout << "计数排序完成后 nums1 = ";
|
||||
PrintUtil::printVector(nums1);
|
||||
printVector(nums1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -20,13 +20,12 @@ void insertionSort(vector<int>& nums) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||||
insertionSort(nums);
|
||||
cout << "插入排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -35,7 +35,8 @@ void merge(vector<int>& nums, int left, int mid, int right) {
|
||||
/* 归并排序 */
|
||||
void mergeSort(vector<int> &nums, int left, int right) {
|
||||
// 终止条件
|
||||
if (left >= right) return; // 当子数组长度为 1 时终止递归
|
||||
if (left >= right)
|
||||
return; // 当子数组长度为 1 时终止递归
|
||||
// 划分阶段
|
||||
int mid = (left + right) / 2; // 计算中点
|
||||
mergeSort(nums, left, mid); // 递归左子数组
|
||||
@ -44,14 +45,13 @@ void mergeSort(vector<int>& nums, int left, int right) {
|
||||
merge(nums, left, mid, right);
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 归并排序 */
|
||||
vector<int> nums = {7, 3, 2, 6, 0, 1, 5, 4};
|
||||
mergeSort(nums, 0, nums.size() - 1);
|
||||
cout << "归并排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -144,26 +144,25 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 快速排序 */
|
||||
vector<int> nums{2, 4, 1, 0, 3, 5};
|
||||
QuickSort::quickSort(nums, 0, nums.size() - 1);
|
||||
cout << "快速排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
/* 快速排序(中位基准数优化) */
|
||||
vector<int> nums1 = {2, 4, 1, 0, 3, 5};
|
||||
QuickSortMedian::quickSort(nums1, 0, nums1.size() - 1);
|
||||
cout << "快速排序(中位基准数优化)完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
/* 快速排序(尾递归优化) */
|
||||
vector<int> nums2 = {2, 4, 1, 0, 3, 5};
|
||||
QuickSortTailCall::quickSort(nums2, 0, nums2.size() - 1);
|
||||
cout << "快速排序(尾递归优化)完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ int main() {
|
||||
88906420, 72429244, 30524779, 82060337, 63832996};
|
||||
radixSort(nums);
|
||||
cout << "基数排序完成后 nums = ";
|
||||
PrintUtil::printVector(nums);
|
||||
printVector(nums);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* 基于环形数组实现的双向队列 */
|
||||
class ArrayDeque {
|
||||
private:
|
||||
@ -122,7 +121,7 @@ int main() {
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "双向队列 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque->peekFirst();
|
||||
@ -133,18 +132,18 @@ int main() {
|
||||
/* 元素入队 */
|
||||
deque->pushLast(4);
|
||||
cout << "元素 4 队尾入队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "元素 1 队首入队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 元素出队 */
|
||||
int popLast = deque->popLast();
|
||||
cout << "队尾出队元素 = " << popLast << ",队尾出队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "队首出队元素 = " << popFirst << ",队首出队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque->size();
|
||||
|
@ -81,7 +81,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
@ -95,7 +94,7 @@ int main() {
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "队列 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue->peek();
|
||||
@ -104,7 +103,7 @@ int main() {
|
||||
/* 元素出队 */
|
||||
queue->pop();
|
||||
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue->size();
|
||||
@ -119,7 +118,7 @@ int main() {
|
||||
queue->push(i);
|
||||
queue->pop();
|
||||
cout << "第 " << i << " 轮入队 + 出队后 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
printVector(queue->toVector());
|
||||
}
|
||||
|
||||
// 释放内存
|
||||
|
@ -46,7 +46,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化栈 */
|
||||
@ -59,7 +58,7 @@ int main() {
|
||||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "栈 stack = ";
|
||||
PrintUtil::printVector(stack->toVector());
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top = stack->top();
|
||||
@ -68,7 +67,7 @@ int main() {
|
||||
/* 元素出栈 */
|
||||
stack->pop();
|
||||
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
||||
PrintUtil::printVector(stack->toVector());
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack->size();
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化双向队列 */
|
||||
@ -19,7 +18,7 @@ int main() {
|
||||
deque.push_front(3);
|
||||
deque.push_front(1);
|
||||
cout << "双向队列 deque = ";
|
||||
PrintUtil::printDeque(deque);
|
||||
printDeque(deque);
|
||||
|
||||
/* 访问元素 */
|
||||
int front = deque.front();
|
||||
@ -30,10 +29,10 @@ int main() {
|
||||
/* 元素出队 */
|
||||
deque.pop_front();
|
||||
cout << "队首出队元素 popFront = " << front << ",队首出队后 deque = ";
|
||||
PrintUtil::printDeque(deque);
|
||||
printDeque(deque);
|
||||
deque.pop_back();
|
||||
cout << "队尾出队元素 popLast = " << back << ",队尾出队后 deque = ";
|
||||
PrintUtil::printDeque(deque);
|
||||
printDeque(deque);
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque.size();
|
||||
|
@ -6,13 +6,13 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* 双向链表节点 */
|
||||
struct DoublyListNode {
|
||||
int val; // 节点值
|
||||
DoublyListNode *next; // 后继节点指针
|
||||
DoublyListNode *prev; // 前驱节点指针
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {}
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
@ -23,7 +23,8 @@ private:
|
||||
|
||||
public:
|
||||
/* 构造方法 */
|
||||
LinkedListDeque() : front(nullptr), rear(nullptr) {}
|
||||
LinkedListDeque() : front(nullptr), rear(nullptr) {
|
||||
}
|
||||
|
||||
/* 析构方法 */
|
||||
~LinkedListDeque() {
|
||||
@ -151,7 +152,7 @@ int main() {
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "双向队列 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 访问元素 */
|
||||
int peekFirst = deque->peekFirst();
|
||||
@ -162,18 +163,18 @@ int main() {
|
||||
/* 元素入队 */
|
||||
deque->pushLast(4);
|
||||
cout << "元素 4 队尾入队后 deque =";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "元素 1 队首入队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 元素出队 */
|
||||
int popLast = deque->popLast();
|
||||
cout << "队尾出队元素 = " << popLast << ",队尾出队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "队首出队元素 = " << popFirst << ",队首出队后 deque = ";
|
||||
PrintUtil::printVector(deque->toVector());
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
int size = deque->size();
|
||||
|
@ -81,7 +81,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
@ -94,7 +93,7 @@ int main() {
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "队列 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue->peek();
|
||||
@ -103,7 +102,7 @@ int main() {
|
||||
/* 元素出队 */
|
||||
queue->pop();
|
||||
cout << "出队元素 pop = " << peek << ",出队后 queue = ";
|
||||
PrintUtil::printVector(queue->toVector());
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue->size();
|
||||
|
@ -70,7 +70,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化栈 */
|
||||
@ -83,7 +82,7 @@ int main() {
|
||||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "栈 stack = ";
|
||||
PrintUtil::printVector(stack->toVector());
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top = stack->top();
|
||||
@ -92,7 +91,7 @@ int main() {
|
||||
/* 元素出栈 */
|
||||
stack->pop();
|
||||
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
||||
PrintUtil::printVector(stack->toVector());
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack->size();
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化队列 */
|
||||
@ -19,7 +18,7 @@ int main(){
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
cout << "队列 queue = ";
|
||||
PrintUtil::printQueue(queue);
|
||||
printQueue(queue);
|
||||
|
||||
/* 访问队首元素 */
|
||||
int front = queue.front();
|
||||
@ -28,7 +27,7 @@ int main(){
|
||||
/* 元素出队 */
|
||||
queue.pop();
|
||||
cout << "出队元素 front = " << front << ",出队后 queue = ";
|
||||
PrintUtil::printQueue(queue);
|
||||
printQueue(queue);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.size();
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化栈 */
|
||||
@ -19,7 +18,7 @@ int main() {
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
cout << "栈 stack = ";
|
||||
PrintUtil::printStack(stack);
|
||||
printStack(stack);
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
int top = stack.top();
|
||||
@ -28,7 +27,7 @@ int main() {
|
||||
/* 元素出栈 */
|
||||
stack.pop(); // 无返回值
|
||||
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
|
||||
PrintUtil::printStack(stack);
|
||||
printStack(stack);
|
||||
|
||||
/* 获取栈的长度 */
|
||||
int size = stack.size();
|
||||
|
@ -151,7 +151,8 @@ public:
|
||||
/* 获取平衡因子 */
|
||||
int balanceFactor(TreeNode *node) {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == nullptr) return 0;
|
||||
if (node == nullptr)
|
||||
return 0;
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node->left) - height(node->right);
|
||||
}
|
||||
@ -188,7 +189,8 @@ public:
|
||||
}
|
||||
|
||||
/*构造方法*/
|
||||
AVLTree() : root(nullptr) {}
|
||||
AVLTree() : root(nullptr) {
|
||||
}
|
||||
|
||||
/*析构方法*/
|
||||
~AVLTree() {
|
||||
@ -199,13 +201,13 @@ public:
|
||||
void testInsert(AVLTree &tree, int val) {
|
||||
tree.insert(val);
|
||||
cout << "\n插入节点 " << val << " 后,AVL 树为" << endl;
|
||||
PrintUtil::printTree(tree.root);
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
void testRemove(AVLTree &tree, int val) {
|
||||
tree.remove(val);
|
||||
cout << "\n删除节点 " << val << " 后,AVL 树为" << endl;
|
||||
PrintUtil::printTree(tree.root);
|
||||
printTree(tree.root);
|
||||
}
|
||||
int main() {
|
||||
/* 初始化空 AVL 树 */
|
||||
|
@ -28,7 +28,8 @@ public:
|
||||
|
||||
/* 构建二叉搜索树 */
|
||||
TreeNode *buildTree(vector<int> nums, int i, int j) {
|
||||
if (i > j) return nullptr;
|
||||
if (i > j)
|
||||
return nullptr;
|
||||
// 将数组中间节点作为根节点
|
||||
int mid = (i + j) / 2;
|
||||
TreeNode *root = new TreeNode(nums[mid]);
|
||||
@ -44,11 +45,14 @@ public:
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 目标节点在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// 目标节点在 cur 的左子树中
|
||||
else if (cur->val > num) cur = cur->left;
|
||||
else if (cur->val > num)
|
||||
cur = cur->left;
|
||||
// 找到目标节点,跳出循环
|
||||
else break;
|
||||
else
|
||||
break;
|
||||
}
|
||||
// 返回目标节点
|
||||
return cur;
|
||||
@ -57,49 +61,62 @@ public:
|
||||
/* 插入节点 */
|
||||
TreeNode *insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == nullptr) return nullptr;
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 找到重复节点,直接返回
|
||||
if (cur->val == num) return nullptr;
|
||||
if (cur->val == num)
|
||||
return nullptr;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else cur = cur->left;
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// 插入节点 val
|
||||
TreeNode *node = new TreeNode(num);
|
||||
if (pre->val < num) pre->right = node;
|
||||
else pre->left = node;
|
||||
if (pre->val < num)
|
||||
pre->right = node;
|
||||
else
|
||||
pre->left = node;
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 删除节点 */
|
||||
TreeNode *remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == nullptr) return nullptr;
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 找到待删除节点,跳出循环
|
||||
if (cur->val == num) break;
|
||||
if (cur->val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// 待删除节点在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// 待删除节点在 cur 的左子树中
|
||||
else cur = cur->left;
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// 若无待删除节点,则直接返回
|
||||
if (cur == nullptr) return nullptr;
|
||||
if (cur == nullptr)
|
||||
return nullptr;
|
||||
// 子节点数量 = 0 or 1
|
||||
if (cur->left == nullptr || cur->right == nullptr) {
|
||||
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
||||
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
|
||||
// 删除节点 cur
|
||||
if (pre->left == cur) pre->left = child;
|
||||
else pre->right = child;
|
||||
if (pre->left == cur)
|
||||
pre->left = child;
|
||||
else
|
||||
pre->right = child;
|
||||
// 释放内存
|
||||
delete cur;
|
||||
}
|
||||
@ -118,7 +135,8 @@ public:
|
||||
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
TreeNode *getInOrderNext(TreeNode *root) {
|
||||
if (root == nullptr) return root;
|
||||
if (root == nullptr)
|
||||
return root;
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
while (root->left != nullptr) {
|
||||
root = root->left;
|
||||
@ -127,14 +145,13 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化二叉搜索树 */
|
||||
vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
|
||||
BinarySearchTree *bst = new BinarySearchTree(nums);
|
||||
cout << endl << "初始化的二叉树为\n" << endl;
|
||||
PrintUtil::printTree(bst->getRoot());
|
||||
printTree(bst->getRoot());
|
||||
|
||||
/* 查找节点 */
|
||||
TreeNode *node = bst->search(7);
|
||||
@ -143,18 +160,18 @@ int main() {
|
||||
/* 插入节点 */
|
||||
node = bst->insert(16);
|
||||
cout << endl << "插入节点 16 后,二叉树为\n" << endl;
|
||||
PrintUtil::printTree(bst->getRoot());
|
||||
printTree(bst->getRoot());
|
||||
|
||||
/* 删除节点 */
|
||||
bst->remove(1);
|
||||
cout << endl << "删除节点 1 后,二叉树为\n" << endl;
|
||||
PrintUtil::printTree(bst->getRoot());
|
||||
printTree(bst->getRoot());
|
||||
bst->remove(2);
|
||||
cout << endl << "删除节点 2 后,二叉树为\n" << endl;
|
||||
PrintUtil::printTree(bst->getRoot());
|
||||
printTree(bst->getRoot());
|
||||
bst->remove(4);
|
||||
cout << endl << "删除节点 4 后,二叉树为\n" << endl;
|
||||
PrintUtil::printTree(bst->getRoot());
|
||||
printTree(bst->getRoot());
|
||||
|
||||
// 释放内存
|
||||
delete bst;
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
@ -22,7 +21,7 @@ int main() {
|
||||
n2->left = n4;
|
||||
n2->right = n5;
|
||||
cout << endl << "初始化二叉树\n" << endl;
|
||||
PrintUtil::printTree(n1);
|
||||
printTree(n1);
|
||||
|
||||
/* 插入与删除节点 */
|
||||
TreeNode *P = new TreeNode(0);
|
||||
@ -30,12 +29,12 @@ int main() {
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
cout << endl << "插入节点 P 后\n" << endl;
|
||||
PrintUtil::printTree(n1);
|
||||
printTree(n1);
|
||||
// 删除节点 P
|
||||
n1->left = n2;
|
||||
delete P; // 释放内存
|
||||
cout << endl << "删除节点 P 后\n" << endl;
|
||||
PrintUtil::printTree(n1);
|
||||
printTree(n1);
|
||||
|
||||
// 释放内存
|
||||
freeMemoryTree(n1);
|
||||
|
@ -25,19 +25,18 @@ vector<int> levelOrder(TreeNode* root) {
|
||||
return vec;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode *root = vecToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
cout << endl << "初始化二叉树\n" << endl;
|
||||
PrintUtil::printTree(root);
|
||||
printTree(root);
|
||||
|
||||
/* 层序遍历 */
|
||||
vector<int> vec = levelOrder(root);
|
||||
cout << endl << "层序遍历的节点打印序列 = ";
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -11,7 +11,8 @@ vector<int> vec;
|
||||
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == nullptr) return;
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
vec.push_back(root->val);
|
||||
preOrder(root->left);
|
||||
@ -20,7 +21,8 @@ void preOrder(TreeNode* root) {
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode *root) {
|
||||
if (root == nullptr) return;
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root->left);
|
||||
vec.push_back(root->val);
|
||||
@ -29,39 +31,39 @@ void inOrder(TreeNode* root) {
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode *root) {
|
||||
if (root == nullptr) return;
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root->left);
|
||||
postOrder(root->right);
|
||||
vec.push_back(root->val);
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode *root = vecToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
cout << endl << "初始化二叉树\n" << endl;
|
||||
PrintUtil::printTree(root);
|
||||
printTree(root);
|
||||
|
||||
/* 前序遍历 */
|
||||
vec.clear();
|
||||
preOrder(root);
|
||||
cout << endl << "前序遍历的节点打印序列 = ";
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
/* 中序遍历 */
|
||||
vec.clear();
|
||||
inOrder(root);
|
||||
cout << endl << "中序遍历的节点打印序列 = ";
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
/* 后序遍历 */
|
||||
vec.clear();
|
||||
postOrder(root);
|
||||
cout << endl << "后序遍历的节点打印序列 = ";
|
||||
PrintUtil::printVector(vec);
|
||||
printVector(vec);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -9,22 +9,15 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* @brief Definition for a singly-linked list node
|
||||
*
|
||||
*/
|
||||
/* Definition for a singly-linked list node */
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a linked list with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return ListNode*
|
||||
*/
|
||||
/* Generate a linked list with a vector */
|
||||
ListNode *vecToLinkedList(vector<int> list) {
|
||||
ListNode *dum = new ListNode(0);
|
||||
ListNode *head = dum;
|
||||
@ -35,13 +28,7 @@ ListNode* vecToLinkedList(vector<int> list) {
|
||||
return dum->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a list node with specific value from a linked list
|
||||
*
|
||||
* @param head
|
||||
* @param val
|
||||
* @return ListNode*
|
||||
*/
|
||||
/* Get a list node with specific value from a linked list */
|
||||
ListNode *getListNode(ListNode *head, int val) {
|
||||
while (head != nullptr && head->val != val) {
|
||||
head = head->next;
|
||||
@ -49,11 +36,7 @@ ListNode* getListNode(ListNode *head, int val) {
|
||||
return head;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Free the memory allocated to a linked list
|
||||
*
|
||||
* @param cur
|
||||
*/
|
||||
/* Free the memory allocated to a linked list */
|
||||
void freeMemoryLinkedList(ListNode *cur) {
|
||||
// 释放内存
|
||||
ListNode *pre;
|
||||
|
@ -6,46 +6,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <climits>
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* @brief Expose the underlying storage of the priority_queue container
|
||||
*
|
||||
* @tparam T
|
||||
* @tparam S
|
||||
* @tparam C
|
||||
* @param pq
|
||||
* @return S
|
||||
*/
|
||||
template <typename T, typename S, typename C>
|
||||
S &Container(priority_queue<T, S, C> &pq) {
|
||||
struct HackedQueue : private priority_queue<T, S, C>
|
||||
{
|
||||
static S &Container(priority_queue<T, S, C> &pq)
|
||||
{
|
||||
/* Expose the underlying storage of the priority_queue container */
|
||||
template <typename T, typename S, typename C> S &Container(priority_queue<T, S, C> &pq) {
|
||||
struct HackedQueue : private priority_queue<T, S, C> {
|
||||
static S &Container(priority_queue<T, S, C> &pq) {
|
||||
return pq.*&HackedQueue::c;
|
||||
}
|
||||
};
|
||||
return HackedQueue::Container(pq);
|
||||
}
|
||||
|
||||
class PrintUtil {
|
||||
public:
|
||||
/**
|
||||
* @brief Find an element in a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param vec
|
||||
* @param ele
|
||||
* @return int
|
||||
*/
|
||||
template <typename T>
|
||||
static int vecFind(const vector<T>& vec, T ele) {
|
||||
/* Find an element in a vector */
|
||||
template <typename T> int vecFind(const vector<T> &vec, T ele) {
|
||||
int j = INT_MAX;
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == ele) {
|
||||
@ -55,16 +34,8 @@ class PrintUtil {
|
||||
return j;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Concatenate a vector with a delim
|
||||
*
|
||||
* @tparam T
|
||||
* @param delim
|
||||
* @param vec
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string strJoin(const string& delim, const T& vec) {
|
||||
/* Concatenate a vector with a delim */
|
||||
template <typename T> string strJoin(const string &delim, const T &vec) {
|
||||
ostringstream s;
|
||||
for (const auto &i : vec) {
|
||||
if (&i != &vec[0]) {
|
||||
@ -75,29 +46,16 @@ class PrintUtil {
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Repeat a string for n times
|
||||
*
|
||||
* @param str
|
||||
* @param n
|
||||
* @return string
|
||||
*/
|
||||
static string strRepeat(string str, int n) {
|
||||
/* Repeat a string for n times */
|
||||
string strRepeat(string str, int n) {
|
||||
ostringstream os;
|
||||
for (int i = 0; i < n; i++)
|
||||
os << str;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print an Array
|
||||
*
|
||||
* @tparam T
|
||||
* @tparam n
|
||||
*/
|
||||
template<typename T>
|
||||
static void printArray(T* arr, int n)
|
||||
{
|
||||
/* Print an Array */
|
||||
template <typename T> void printArray(T *arr, int n) {
|
||||
cout << "[";
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
cout << arr[i] << ", ";
|
||||
@ -108,49 +66,26 @@ class PrintUtil {
|
||||
cout << "]" << endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the Vector String object
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string getVectorString(vector<T> &list) {
|
||||
/* Get the Vector String object */
|
||||
template <typename T> string getVectorString(vector<T> &list) {
|
||||
return "[" + strJoin(", ", list) + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVector(vector<T> list) {
|
||||
/* Print a vector */
|
||||
template <typename T> void printVector(vector<T> list) {
|
||||
cout << getVectorString(list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector matrix
|
||||
*
|
||||
* @tparam T
|
||||
* @param matrix
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
/* Print a vector matrix */
|
||||
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "[" << '\n';
|
||||
for (vector<T> &list : matrix)
|
||||
cout << " " + getVectorString(list) + "," << '\n';
|
||||
cout << "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a linked list
|
||||
*
|
||||
* @param head
|
||||
*/
|
||||
static void printLinkedList(ListNode *head) {
|
||||
/* Print a linked list */
|
||||
void printLinkedList(ListNode *head) {
|
||||
vector<int> list;
|
||||
while (head != nullptr) {
|
||||
list.push_back(head->val);
|
||||
@ -161,7 +96,7 @@ class PrintUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This tree printer is borrowed from TECHIE DELIGHT
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
struct Trunk {
|
||||
@ -173,12 +108,8 @@ class PrintUtil {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper function to print branches of the binary tree
|
||||
*
|
||||
* @param p
|
||||
*/
|
||||
static void showTrunks(Trunk *p) {
|
||||
/* Helper function to print branches of the binary tree */
|
||||
void showTrunks(Trunk *p) {
|
||||
if (p == nullptr) {
|
||||
return;
|
||||
}
|
||||
@ -187,23 +118,8 @@ class PrintUtil {
|
||||
cout << p->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The interface of the tree printer
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void printTree(TreeNode *root) {
|
||||
printTree(root, nullptr, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
static void printTree(TreeNode *root, Trunk *prev, bool isLeft) {
|
||||
/* Print a binary tree */
|
||||
void printTree(TreeNode *root, Trunk *prev, bool isLeft) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
}
|
||||
@ -215,12 +131,10 @@ class PrintUtil {
|
||||
|
||||
if (!prev) {
|
||||
trunk.str = "———";
|
||||
}
|
||||
else if (isLeft) {
|
||||
} else if (isLeft) {
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
trunk.str = "\\———";
|
||||
prev->str = prev_str;
|
||||
}
|
||||
@ -236,14 +150,13 @@ class PrintUtil {
|
||||
printTree(root->left, &trunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a stack
|
||||
*
|
||||
* @tparam T
|
||||
* @param stk
|
||||
*/
|
||||
template <typename T>
|
||||
static void printStack(stack<T> stk) {
|
||||
/* The interface of the tree printer */
|
||||
void printTree(TreeNode *root) {
|
||||
printTree(root, nullptr, false);
|
||||
}
|
||||
|
||||
/* Print a stack */
|
||||
template <typename T> void printStack(stack<T> stk) {
|
||||
// Reverse the input stack
|
||||
stack<T> tmp;
|
||||
while (!stk.empty()) {
|
||||
@ -257,22 +170,15 @@ class PrintUtil {
|
||||
if (flag) {
|
||||
s << tmp.top();
|
||||
flag = false;
|
||||
}
|
||||
else s << ", " << tmp.top();
|
||||
} else
|
||||
s << ", " << tmp.top();
|
||||
tmp.pop();
|
||||
}
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief
|
||||
*
|
||||
* @tparam T
|
||||
* @param queue
|
||||
*/
|
||||
template <typename T>
|
||||
static void printQueue(queue<T> queue)
|
||||
{
|
||||
/* Print a queue */
|
||||
template <typename T> void printQueue(queue<T> queue) {
|
||||
// Generate the string to print
|
||||
ostringstream s;
|
||||
bool flag = true;
|
||||
@ -280,15 +186,15 @@ class PrintUtil {
|
||||
if (flag) {
|
||||
s << queue.front();
|
||||
flag = false;
|
||||
}
|
||||
else s << ", " << queue.front();
|
||||
} else
|
||||
s << ", " << queue.front();
|
||||
queue.pop();
|
||||
}
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void printDeque(deque<T> deque) {
|
||||
/* Print a deque */
|
||||
template <typename T> void printDeque(deque<T> deque) {
|
||||
// Generate the string to print
|
||||
ostringstream s;
|
||||
bool flag = true;
|
||||
@ -296,38 +202,23 @@ class PrintUtil {
|
||||
if (flag) {
|
||||
s << deque.front();
|
||||
flag = false;
|
||||
}
|
||||
else s << ", " << deque.front();
|
||||
} else
|
||||
s << ", " << deque.front();
|
||||
deque.pop_front();
|
||||
}
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a HashMap
|
||||
*
|
||||
* @tparam TKey
|
||||
* @tparam TValue
|
||||
* @param map
|
||||
*/
|
||||
/* Print a HashMap */
|
||||
// 定义模板参数 TKey 和 TValue,用于指定键值对的类型
|
||||
template <typename TKey, typename TValue>
|
||||
static void printHashMap(unordered_map<TKey,TValue> map) {
|
||||
template <typename TKey, typename TValue> void printHashMap(unordered_map<TKey, TValue> map) {
|
||||
for (auto kv : map) {
|
||||
cout << kv.first << " -> " << kv.second << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a Heap (PriorityQueue)
|
||||
*
|
||||
* @tparam T
|
||||
* @tparam S
|
||||
* @tparam C
|
||||
* @param heap
|
||||
*/
|
||||
template <typename T, typename S, typename C>
|
||||
static void printHeap(priority_queue<T, S, C> &heap) {
|
||||
/* Print a Heap (PriorityQueue) */
|
||||
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
|
||||
vector<T> vec = Container(heap);
|
||||
cout << "堆的数组表示:";
|
||||
printVector(vec);
|
||||
@ -336,4 +227,3 @@ class PrintUtil {
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
};
|
||||
|
@ -6,10 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Definition for a binary tree node
|
||||
*
|
||||
*/
|
||||
/* Definition for a binary tree node */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
@ -17,15 +14,11 @@ struct TreeNode {
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {}
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a binary tree with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return TreeNode*
|
||||
*/
|
||||
/* Generate a binary tree with a vector */
|
||||
TreeNode *vecToTree(vector<int> list) {
|
||||
if (list.empty())
|
||||
return nullptr;
|
||||
@ -37,12 +30,14 @@ TreeNode *vecToTree(vector<int> list) {
|
||||
while (!que.empty()) {
|
||||
auto node = que.front();
|
||||
que.pop();
|
||||
if (++index >= n) break;
|
||||
if (++index >= n)
|
||||
break;
|
||||
if (index < n) {
|
||||
node->left = new TreeNode(list[index]);
|
||||
que.emplace(node->left);
|
||||
}
|
||||
if (++index >= n) break;
|
||||
if (++index >= n)
|
||||
break;
|
||||
if (index < n) {
|
||||
node->right = new TreeNode(list[index]);
|
||||
que.emplace(node->right);
|
||||
@ -52,13 +47,7 @@ TreeNode *vecToTree(vector<int> list) {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a tree node with specific value in a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param val
|
||||
* @return TreeNode*
|
||||
*/
|
||||
/* Get a tree node with specific value in a binary tree */
|
||||
TreeNode *getTreeNode(TreeNode *root, int val) {
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
@ -69,13 +58,10 @@ TreeNode *getTreeNode(TreeNode *root, int val) {
|
||||
return left != nullptr ? left : right;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Free the memory allocated to a tree
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
/* Free the memory allocated to a tree */
|
||||
void freeMemoryTree(TreeNode *root) {
|
||||
if (root == nullptr) return;
|
||||
if (root == nullptr)
|
||||
return;
|
||||
freeMemoryTree(root->left);
|
||||
freeMemoryTree(root->right);
|
||||
// 释放内存
|
||||
|
@ -12,7 +12,8 @@ using namespace std;
|
||||
/* 顶点类 */
|
||||
struct Vertex {
|
||||
int val;
|
||||
Vertex(int x) : val(x) {}
|
||||
Vertex(int x) : val(x) {
|
||||
}
|
||||
};
|
||||
|
||||
/* 输入值列表 vals ,返回顶点列表 vets */
|
||||
|
@ -6,23 +6,23 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <stack>
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <set>
|
||||
#include <random>
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "ListNode.hpp"
|
||||
#include "PrintUtil.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
#include "Vertex.hpp"
|
||||
#include "PrintUtil.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
Loading…
x
Reference in New Issue
Block a user