mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Fix Repo
This commit is contained in:
@ -303,13 +303,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "C++"
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 获取平衡因子 */
|
||||
int balanceFactor(TreeNode* node) {
|
||||
// 空结点平衡因子为 0
|
||||
if (node == nullptr) return 0;
|
||||
// 结点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node->left) - height(node->right);
|
||||
}
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -436,19 +430,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C++"
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 右旋操作 */
|
||||
TreeNode* rightRotate(TreeNode* node) {
|
||||
TreeNode* child = node->left;
|
||||
TreeNode* grandChild = child->right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child->right = node;
|
||||
node->left = grandChild;
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -581,19 +563,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C++"
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 左旋操作 */
|
||||
TreeNode* leftRotate(TreeNode* node) {
|
||||
TreeNode* child = node->right;
|
||||
TreeNode* grandChild = child->left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child->left = node;
|
||||
node->right = grandChild;
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -750,35 +720,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "C++"
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
TreeNode* rotate(TreeNode* node) {
|
||||
// 获取结点 node 的平衡因子
|
||||
int _balanceFactor = balanceFactor(node);
|
||||
// 左偏树
|
||||
if (_balanceFactor > 1) {
|
||||
if (balanceFactor(node->left) >= 0) {
|
||||
// 右旋
|
||||
return rightRotate(node);
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node->left = leftRotate(node->left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (_balanceFactor < -1) {
|
||||
if (balanceFactor(node->right) <= 0) {
|
||||
// 左旋
|
||||
return leftRotate(node);
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node->right = rightRotate(node->right);
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
return node;
|
||||
}
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -44,21 +44,7 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_tree.cpp"
|
||||
/* 查找结点 */
|
||||
TreeNode* search(int num) {
|
||||
TreeNode* cur = root;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 目标结点在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
// 目标结点在 cur 的左子树中
|
||||
else if (cur->val > num) cur = cur->left;
|
||||
// 找到目标结点,跳出循环
|
||||
else break;
|
||||
}
|
||||
// 返回目标结点
|
||||
return cur;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -212,27 +198,7 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_tree.cpp"
|
||||
/* 插入结点 */
|
||||
TreeNode* insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == nullptr) return nullptr;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 找到重复结点,直接返回
|
||||
if (cur->val == num) return nullptr;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else cur = cur->left;
|
||||
}
|
||||
// 插入结点 val
|
||||
TreeNode* node = new TreeNode(num);
|
||||
if (pre->val < num) pre->right = node;
|
||||
else pre->left = node;
|
||||
return node;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -465,53 +431,9 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_search_tree.cpp"
|
||||
/* 删除结点 */
|
||||
TreeNode* remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == nullptr) return nullptr;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != nullptr) {
|
||||
// 找到待删除结点,跳出循环
|
||||
if (cur->val == num) break;
|
||||
pre = cur;
|
||||
// 待删除结点在 cur 的右子树中
|
||||
if (cur->val < num) cur = cur->right;
|
||||
// 待删除结点在 cur 的左子树中
|
||||
else cur = cur->left;
|
||||
}
|
||||
// 若无待删除结点,则直接返回
|
||||
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;
|
||||
}
|
||||
// 子结点数量 = 2
|
||||
else {
|
||||
// 获取中序遍历中 cur 的下一个结点
|
||||
TreeNode* nex = getInOrderNext(cur->right);
|
||||
int tmp = nex->val;
|
||||
// 递归删除结点 nex
|
||||
remove(nex->val);
|
||||
// 将 nex 的值复制给 cur
|
||||
cur->val = tmp;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
TreeNode* getInOrderNext(TreeNode* root) {
|
||||
if (root == nullptr) return root;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while (root->left != nullptr) {
|
||||
root = root->left;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -27,24 +27,7 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_tree_bfs.cpp"
|
||||
/* 层序遍历 */
|
||||
vector<int> hierOrder(TreeNode* root) {
|
||||
// 初始化队列,加入根结点
|
||||
queue<TreeNode*> queue;
|
||||
queue.push(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
vector<int> vec;
|
||||
while (!queue.empty()) {
|
||||
TreeNode* node = queue.front();
|
||||
queue.pop(); // 队列出队
|
||||
vec.push_back(node->val); // 保存结点值
|
||||
if (node->left != nullptr)
|
||||
queue.push(node->left); // 左子结点入队
|
||||
if (node->right != nullptr)
|
||||
queue.push(node->right); // 右子结点入队
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
[class]{}-[func]{hierOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
@ -218,32 +201,11 @@ comments: true
|
||||
=== "C++"
|
||||
|
||||
```cpp title="binary_tree_dfs.cpp"
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode* root) {
|
||||
if (root == nullptr) return;
|
||||
// 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
vec.push_back(root->val);
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode* root) {
|
||||
if (root == nullptr) return;
|
||||
// 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
inOrder(root->left);
|
||||
vec.push_back(root->val);
|
||||
inOrder(root->right);
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode* root) {
|
||||
if (root == nullptr) return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
postOrder(root->left);
|
||||
postOrder(root->right);
|
||||
vec.push_back(root->val);
|
||||
}
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "Python"
|
||||
|
||||
Reference in New Issue
Block a user