Merge branch 'master' into binary_search_tree

This commit is contained in:
Yudong Jin
2023-01-10 13:30:38 +08:00
committed by GitHub
215 changed files with 5618 additions and 1642 deletions

View File

@@ -1,4 +1,4 @@
/*
/**
* File: array.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: linked_list.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -21,14 +21,16 @@ void remove(ListNode* n0) {
ListNode* P = n0->next;
ListNode* n1 = P->next;
n0->next = n1;
// 释放内存
delete P;
}
/* 访问链表中索引为 index 的结点 */
ListNode* access(ListNode* head, int index) {
for (int i = 0; i < index; i++) {
head = head->next;
if (head == nullptr)
return nullptr;
head = head->next;
}
return head;
}

View File

@@ -1,4 +1,4 @@
/*
/**
* File: list.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: my_list.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: leetcode_two_sum.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: space_complexity.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: time_complexity.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: worst_best_time_complexity.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: array_hash_map.cpp
* Created Time: 2022-12-14
* Author: msk397 (machangxinq@gmail.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: hash_map.cpp
* Created Time: 2022-12-14
* Author: msk397 (machangxinq@gmail.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: binary_search.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: hashing_search.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: linear_search.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: bubble_sort.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: insertion_sort.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: merge_sort.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -25,10 +25,10 @@ void merge(vector<int>& nums, int left, int mid, int right) {
// 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++
if (i > leftEnd)
nums[k] = tmp[j++];
// 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++
// 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++
else if (j > rightEnd || tmp[i] <= tmp[j])
nums[k] = tmp[i++];
// 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
// 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++
else
nums[k] = tmp[j++];
}

View File

@@ -1,4 +1,4 @@
/*
/**
* File: quick_sort.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: array_queue.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -50,11 +50,10 @@ public:
}
/* 出队 */
int poll() {
void poll() {
int num = peek();
// 队头指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
return num;
}
/* 访问队首元素 */
@@ -98,8 +97,8 @@ int main() {
cout << "队首元素 peek = " << peek << endl;
/* 元素出队 */
int poll = queue->poll();
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
queue->poll();
cout << "出队元素 poll = " << peek << ",出队后 queue = ";
PrintUtil::printVector(queue->toVector());
/* 获取队列的长度 */

View File

@@ -1,4 +1,4 @@
/*
/**
* File: array_stack.cpp
* Created Time: 2022-11-28
* Author: qualifier1024 (2539244001@qq.com)
@@ -28,10 +28,9 @@ public:
}
/* 出栈 */
int pop() {
void pop() {
int oldTop = top();
stack.pop_back();
return oldTop;
}
/* 访问栈顶元素 */
@@ -67,8 +66,8 @@ int main() {
cout << "栈顶元素 top = " << top << endl;
/* 元素出栈 */
int pop = stack->pop();
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
stack->pop();
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
PrintUtil::printVector(stack->toVector());
/* 获取栈的长度 */

View File

@@ -1,4 +1,4 @@
/*
/**
* File: deque.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: linkedlist_queue.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -47,12 +47,14 @@ public:
}
/* 出队 */
int poll() {
void poll() {
int num = peek();
// 删除头结点
ListNode *tmp = front;
front = front->next;
// 释放内存
delete tmp;
queSize--;
return num;
}
/* 访问队首元素 */
@@ -94,8 +96,8 @@ int main() {
cout << "队首元素 peek = " << peek << endl;
/* 元素出队 */
int poll = queue->poll();
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
queue->poll();
cout << "出队元素 poll = " << peek << ",出队后 queue = ";
PrintUtil::printVector(queue->toVector());
/* 获取队列的长度 */

View File

@@ -1,4 +1,4 @@
/*
/**
* File: linkedlist_stack.cpp
* Created Time: 2022-11-28
* Author: qualifier1024 (2539244001@qq.com)
@@ -37,11 +37,13 @@ public:
}
/* 出栈 */
int pop() {
void pop() {
int num = top();
ListNode *tmp = stackTop;
stackTop = stackTop->next;
// 释放内存
delete tmp;
stkSize--;
return num;
}
/* 访问栈顶元素 */
@@ -83,8 +85,8 @@ int main() {
cout << "栈顶元素 top = " << top << endl;
/* 元素出栈 */
int pop = stack->pop();
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
stack->pop();
cout << "出栈元素 pop = " << top << ",出栈后 stack = ";
PrintUtil::printVector(stack->toVector());
/* 获取栈的长度 */

View File

@@ -1,4 +1,4 @@
/*
/**
* File: queue.cpp
* Created Time: 2022-11-28
* Author: qualifier1024 (2539244001@qq.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: stack.cpp
* Created Time: 2022-11-28
* Author: qualifier1024 (2539244001@qq.com)

View File

@@ -1,228 +0,0 @@
/*
* File: avl_tree.cpp
* Created Time: 2022-12-2
* Author: mgisr (maguagua0706@gmail.com)
*/
#include "../include/include.hpp"
class AvlTree {
private:
TreeNode *root{};
static bool isBalance(const TreeNode *p);
static int getBalanceFactor(const TreeNode *p);
static void updateHeight(TreeNode *p);
void fixBalance(TreeNode *p);
static bool isLeftChild(const TreeNode *p);
static TreeNode *&fromParentTo(TreeNode *node);
public:
AvlTree() = default;
AvlTree(const AvlTree &p) = default;
const TreeNode *search(int val);
bool insert(int val);
bool remove(int val);
void printTree();
};
// 判断该结点是否平衡
bool AvlTree::isBalance(const TreeNode *p) {
int balance_factor = getBalanceFactor(p);
if (-1 <= balance_factor && balance_factor <= 1) { return true; }
else { return false; }
}
// 获取当前结点的平衡因子
int AvlTree::getBalanceFactor(const TreeNode *p) {
if (p->left == nullptr && p->right == nullptr) { return 0; }
else if (p->left == nullptr) { return (-1 - p->right->height); }
else if (p->right == nullptr) { return p->left->height + 1; }
else { return p->left->height - p->right->height; }
}
// 更新结点高度
void AvlTree::updateHeight(TreeNode *p) {
if (p->left == nullptr && p->right == nullptr) { p->height = 0; }
else if (p->left == nullptr) { p->height = p->right->height + 1; }
else if (p->right == nullptr) { p->height = p->left->height + 1; }
else { p->height = std::max(p->left->height, p->right->height) + 1; }
}
void AvlTree::fixBalance(TreeNode *p) {
// 左旋操作
auto rotate_left = [&](TreeNode *node) -> TreeNode * {
TreeNode *temp = node->right;
temp->parent = p->parent;
node->right = temp->left;
if (temp->left != nullptr) {
temp->left->parent = node;
}
temp->left = node;
node->parent = temp;
updateHeight(node);
updateHeight(temp);
return temp;
};
// 右旋操作
auto rotate_right = [&](TreeNode *node) -> TreeNode * {
TreeNode *temp = node->left;
temp->parent = p->parent;
node->left = temp->right;
if (temp->right != nullptr) {
temp->right->parent = node;
}
temp->right = node;
node->parent = temp;
updateHeight(node);
updateHeight(temp);
return temp;
};
// 根据规则选取旋转方式
if (getBalanceFactor(p) > 1) {
if (getBalanceFactor(p->left) > 0) {
if (p->parent == nullptr) { root = rotate_right(p); }
else { fromParentTo(p) = rotate_right(p); }
} else {
p->left = rotate_left(p->left);
if (p->parent == nullptr) { root = rotate_right(p); }
else { fromParentTo(p) = rotate_right(p); }
}
} else {
if (getBalanceFactor(p->right) < 0) {
if (p->parent == nullptr) { root = rotate_left(p); }
else { fromParentTo(p) = rotate_left(p); }
} else {
p->right = rotate_right(p->right);
if (p->parent == nullptr) { root = rotate_left(p); }
else { fromParentTo(p) = rotate_left(p); }
}
}
}
// 判断当前结点是否为其父节点的左孩子
bool AvlTree::isLeftChild(const TreeNode *p) {
if (p->parent == nullptr) { return false; }
return (p->parent->left == p);
}
// 返回父节点指向当前结点指针的引用
TreeNode *&AvlTree::fromParentTo(TreeNode *node) {
if (isLeftChild(node)) { return node->parent->left; }
else { return node->parent->right; }
}
const TreeNode *AvlTree::search(int val) {
TreeNode *p = root;
while (p != nullptr) {
if (p->val == val) { return p; }
else if (p->val > val) { p = p->left; }
else { p = p->right; }
}
return nullptr;
}
bool AvlTree::insert(int val) {
TreeNode *p = root;
if (p == nullptr) {
root = new TreeNode(val);
return true;
}
for (;;) {
if (p->val == val) { return false; }
else if (p->val > val) {
if (p->left == nullptr) {
p->left = new TreeNode(val, p);
break;
} else {
p = p->left;
}
} else {
if (p->right == nullptr) {
p->right = new TreeNode(val, p);
break;
} else {
p = p->right;
}
}
}
for (; p != nullptr; p = p->parent) {
if (!isBalance(p)) {
fixBalance(p);
break;
} else { updateHeight(p); }
}
return true;
}
bool AvlTree::remove(int val) {
TreeNode *p = root;
if (p == nullptr) { return false; }
while (p != nullptr) {
if (p->val == val) {
TreeNode *real_delete_node = p;
TreeNode *next_node;
if (p->left == nullptr) {
next_node = p->right;
if (p->parent == nullptr) { root = next_node; }
else { fromParentTo(p) = next_node; }
} else if (p->right == nullptr) {
next_node = p->left;
if (p->parent == nullptr) { root = next_node; }
else { fromParentTo(p) = next_node; }
} else {
while (real_delete_node->left != nullptr) {
real_delete_node = real_delete_node->left;
}
std::swap(p->val, real_delete_node->val);
next_node = real_delete_node->right;
if (real_delete_node->parent == p) { p->right = next_node; }
else { real_delete_node->parent->left = next_node; }
}
if (next_node != nullptr) {
next_node->parent = real_delete_node->parent;
}
for (p = real_delete_node; p != nullptr; p = p->parent) {
if (!isBalance(p)) { fixBalance(p); }
updateHeight(p);
}
delete real_delete_node;
return true;
} else if (p->val > val) {
p = p->left;
} else {
p = p->right;
}
}
return false;
}
void inOrder(const TreeNode *root) {
if (root == nullptr) return;
inOrder(root->left);
cout << root->val << ' ';
inOrder(root->right);
}
void AvlTree::printTree() {
inOrder(root);
cout << endl;
}
int main() {
AvlTree tree = AvlTree();
// tree.insert(13);
// tree.insert(24);
// tree.insert(37);
// tree.insert(90);
// tree.insert(53);
tree.insert(53);
tree.insert(90);
tree.insert(37);
tree.insert(24);
tree.insert(13);
tree.remove(90);
tree.printTree();
const TreeNode *p = tree.search(37);
cout << p->val;
return 0;
}

View File

@@ -1,4 +1,4 @@
/*
/**
* File: binary_search_tree.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -96,11 +96,13 @@ public:
// 删除结点 cur
if (pre->left == cur) pre->left = child;
else pre->right = child;
// 释放内存
delete cur;
}
// 子结点数量 = 2
else {
// 获取中序遍历中 cur 的下一个结点
TreeNode* nex = min(cur->right);
TreeNode* nex = getInOrderNext(cur->right);
int tmp = nex->val;
// 递归删除结点 nex
remove(nex->val);
@@ -110,8 +112,8 @@ public:
return cur;
}
/* 获取最小结点 */
TreeNode* min(TreeNode* root) {
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
TreeNode* getInOrderNext(TreeNode* root) {
if (root == nullptr) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root->left != nullptr) {

View File

@@ -1,4 +1,4 @@
/*
/**
* File: binary_tree.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -33,6 +33,7 @@ int main() {
PrintUtil::printTree(n1);
// 删除结点 P
n1->left = n2;
delete P; // 释放内存
cout << endl << "删除结点 P 后\n" << endl;
PrintUtil::printTree(n1);

View File

@@ -1,4 +1,4 @@
/*
/**
* File: binary_tree_bfs.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -30,8 +30,7 @@ vector<int> hierOrder(TreeNode* root) {
int main() {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
TreeNode* root = vecToTree(vector<int>
{ 1, 2, 3, 4, 5, 6, 7, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX });
TreeNode* root = vecToTree(vector<int> { 1, 2, 3, 4, 5, 6, 7 });
cout << endl << "初始化二叉树\n" << endl;
PrintUtil::printTree(root);

View File

@@ -1,4 +1,4 @@
/*
/**
* File: binary_tree_dfs.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -41,8 +41,7 @@ void postOrder(TreeNode* root) {
int main() {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
TreeNode* root = vecToTree(vector<int>
{ 1, 2, 3, 4, 5, 6, 7, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX});
TreeNode* root = vecToTree(vector<int> { 1, 2, 3, 4, 5, 6, 7 });
cout << endl << "初始化二叉树\n" << endl;
PrintUtil::printTree(root);

View File

@@ -1,4 +1,4 @@
/*
/**
* File: PrintUtil.hpp
* Created Time: 2021-12-19
* Author: Krahets (krahets@163.com)

View File

@@ -1,4 +1,4 @@
/*
/**
* File: PrintUtil.hpp
* Created Time: 2021-12-19
* Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
@@ -9,6 +9,7 @@
#include <iostream>
#include <string>
#include <sstream>
#include <climits>
#include "ListNode.hpp"
#include "TreeNode.hpp"

View File

@@ -1,4 +1,4 @@
/*
/**
* File: PrintUtil.hpp
* Created Time: 2021-12-19
* Author: Krahets (krahets@163.com)
@@ -27,23 +27,24 @@ struct TreeNode {
* @return TreeNode*
*/
TreeNode *vecToTree(vector<int> list) {
if (list.empty()) {
if (list.empty())
return nullptr;
}
auto *root = new TreeNode(list[0]);
queue<TreeNode *> que;
size_t n = list.size(), index = 1;
while (index < n) {
que.emplace(root);
size_t n = list.size(), index = 0;
while (!que.empty()) {
auto node = que.front();
que.pop();
if (++index >= n) break;
if (index < n) {
node->left = new TreeNode(list[index++]);
node->left = new TreeNode(list[index]);
que.emplace(node->left);
}
if (++index >= n) break;
if (index < n) {
node->right = new TreeNode(list[index++]);
node->right = new TreeNode(list[index]);
que.emplace(node->right);
}
}

View File

@@ -1,4 +1,4 @@
/*
/**
* File: PrintUtil.hpp
* Created Time: 2021-12-19
* Author: Krahets (krahets@163.com)