mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-11 16:32:00 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -9,93 +9,93 @@
|
||||
/* AVL tree */
|
||||
class AVLTree {
|
||||
private:
|
||||
/* Update node height */
|
||||
/* Update the node height */
|
||||
void updateHeight(TreeNode *node) {
|
||||
// Node height equals the height of the tallest subtree + 1
|
||||
// The node height equals the height of its taller subtree + 1
|
||||
node->height = max(height(node->left), height(node->right)) + 1;
|
||||
}
|
||||
|
||||
/* Right rotation operation */
|
||||
/* Right rotation */
|
||||
TreeNode *rightRotate(TreeNode *node) {
|
||||
TreeNode *child = node->left;
|
||||
TreeNode *grandChild = child->right;
|
||||
// Using child as pivot, rotate node to the right
|
||||
// Rotate node to the right around child
|
||||
child->right = node;
|
||||
node->left = grandChild;
|
||||
// Update node height
|
||||
// Update the node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
// Return the root of the rotated subtree
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Left rotation operation */
|
||||
/* Left rotation */
|
||||
TreeNode *leftRotate(TreeNode *node) {
|
||||
TreeNode *child = node->right;
|
||||
TreeNode *grandChild = child->left;
|
||||
// Using child as pivot, rotate node to the left
|
||||
// Rotate node to the left around child
|
||||
child->left = node;
|
||||
node->right = grandChild;
|
||||
// Update node height
|
||||
// Update the node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
// Return the root of the rotated subtree
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Perform rotation operation to restore balance to this subtree */
|
||||
/* Perform rotations to restore balance in this subtree */
|
||||
TreeNode *rotate(TreeNode *node) {
|
||||
// Get balance factor of node
|
||||
// Get the balance factor of node
|
||||
int _balanceFactor = balanceFactor(node);
|
||||
// Left-leaning tree
|
||||
// Left-heavy tree
|
||||
if (_balanceFactor > 1) {
|
||||
if (balanceFactor(node->left) >= 0) {
|
||||
// Right rotation
|
||||
// Rotate right
|
||||
return rightRotate(node);
|
||||
} else {
|
||||
// First left rotation then right rotation
|
||||
// Rotate left, then right
|
||||
node->left = leftRotate(node->left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// Right-leaning tree
|
||||
// Right-heavy tree
|
||||
if (_balanceFactor < -1) {
|
||||
if (balanceFactor(node->right) <= 0) {
|
||||
// Left rotation
|
||||
// Rotate left
|
||||
return leftRotate(node);
|
||||
} else {
|
||||
// First right rotation then left rotation
|
||||
// Rotate right, then left
|
||||
node->right = rightRotate(node->right);
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// Balanced tree, no rotation needed, return directly
|
||||
// The tree is balanced; return without rotating
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Recursively insert node (helper method) */
|
||||
/* Insert a node recursively (helper method) */
|
||||
TreeNode *insertHelper(TreeNode *node, int val) {
|
||||
if (node == nullptr)
|
||||
return new TreeNode(val);
|
||||
/* 1. Find insertion position and insert node */
|
||||
/* 1. Find the insertion position and insert the node */
|
||||
if (val < node->val)
|
||||
node->left = insertHelper(node->left, val);
|
||||
else if (val > node->val)
|
||||
node->right = insertHelper(node->right, val);
|
||||
else
|
||||
return node; // Duplicate node not inserted, return directly
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
return node; // Return without inserting a duplicate node
|
||||
updateHeight(node); // Update the node height
|
||||
/* 2. Perform rotations to restore balance in this subtree */
|
||||
node = rotate(node);
|
||||
// Return root node of subtree
|
||||
// Return the root of the subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Recursively delete node (helper method) */
|
||||
/* Remove a node recursively (helper method) */
|
||||
TreeNode *removeHelper(TreeNode *node, int val) {
|
||||
if (node == nullptr)
|
||||
return nullptr;
|
||||
/* 1. Find node and delete */
|
||||
/* 1. Find and remove the node */
|
||||
if (val < node->val)
|
||||
node->left = removeHelper(node->left, val);
|
||||
else if (val > node->val)
|
||||
@@ -103,18 +103,18 @@ class AVLTree {
|
||||
else {
|
||||
if (node->left == nullptr || node->right == nullptr) {
|
||||
TreeNode *child = node->left != nullptr ? node->left : node->right;
|
||||
// Number of child nodes = 0, delete node directly and return
|
||||
// Number of children = 0: remove node and return
|
||||
if (child == nullptr) {
|
||||
delete node;
|
||||
return nullptr;
|
||||
}
|
||||
// Number of child nodes = 1, delete node directly
|
||||
// Number of children = 1: remove node directly
|
||||
else {
|
||||
delete node;
|
||||
node = child;
|
||||
}
|
||||
} else {
|
||||
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
|
||||
// Number of children = 2: remove the inorder successor and use its value to replace the current node value
|
||||
TreeNode *temp = node->right;
|
||||
while (temp->left != nullptr) {
|
||||
temp = temp->left;
|
||||
@@ -124,57 +124,57 @@ class AVLTree {
|
||||
node->val = tempVal;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
updateHeight(node); // Update the node height
|
||||
/* 2. Perform rotations to restore balance in this subtree */
|
||||
node = rotate(node);
|
||||
// Return root node of subtree
|
||||
// Return the root of the subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
public:
|
||||
TreeNode *root; // Root node
|
||||
|
||||
/* Get node height */
|
||||
/* Get the node height */
|
||||
int height(TreeNode *node) {
|
||||
// Empty node height is -1, leaf node height is 0
|
||||
// A null node has height -1; a leaf has height 0
|
||||
return node == nullptr ? -1 : node->height;
|
||||
}
|
||||
|
||||
/* Get balance factor */
|
||||
/* Get the balance factor */
|
||||
int balanceFactor(TreeNode *node) {
|
||||
// Empty node balance factor is 0
|
||||
// A null node has balance factor 0
|
||||
if (node == nullptr)
|
||||
return 0;
|
||||
// Node balance factor = left subtree height - right subtree height
|
||||
return height(node->left) - height(node->right);
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
/* Insert a node */
|
||||
void insert(int val) {
|
||||
root = insertHelper(root, val);
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
/* Remove a node */
|
||||
void remove(int val) {
|
||||
root = removeHelper(root, val);
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
/* Find a node */
|
||||
TreeNode *search(int val) {
|
||||
TreeNode *cur = root;
|
||||
// Loop search, exit after passing leaf node
|
||||
// Continue searching while the current node pointer is not null
|
||||
while (cur != nullptr) {
|
||||
// Target node is in cur's right subtree
|
||||
// The target node lies in the right subtree of cur
|
||||
if (cur->val < val)
|
||||
cur = cur->right;
|
||||
// Target node is in cur's left subtree
|
||||
// The target node lies in the left subtree of cur
|
||||
else if (cur->val > val)
|
||||
cur = cur->left;
|
||||
// Found target node, exit loop
|
||||
// The target node was found; exit the loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Return target node
|
||||
// Return the target node, or nullptr if it was not found
|
||||
return cur;
|
||||
}
|
||||
|
||||
@@ -190,23 +190,23 @@ class AVLTree {
|
||||
|
||||
void testInsert(AVLTree &tree, int val) {
|
||||
tree.insert(val);
|
||||
cout << "\nAfter inserting node " << val << ", AVL tree is" << endl;
|
||||
cout << "\nAfter inserting node " << val << " , the AVL tree is" << endl;
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
void testRemove(AVLTree &tree, int val) {
|
||||
tree.remove(val);
|
||||
cout << "\nAfter removing node " << val << ", AVL tree is" << endl;
|
||||
cout << "\nAfter removing node " << val << " , the AVL tree is" << endl;
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
|
||||
/* Initialize an empty AVL tree */
|
||||
AVLTree avlTree;
|
||||
|
||||
/* Insert node */
|
||||
// Delete nodes
|
||||
/* Insert a node */
|
||||
// Observe how the AVL tree maintains balance after a node is inserted
|
||||
testInsert(avlTree, 1);
|
||||
testInsert(avlTree, 2);
|
||||
testInsert(avlTree, 3);
|
||||
@@ -218,16 +218,16 @@ int main() {
|
||||
testInsert(avlTree, 10);
|
||||
testInsert(avlTree, 6);
|
||||
|
||||
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
|
||||
/* Insert a duplicate node */
|
||||
testInsert(avlTree, 7);
|
||||
|
||||
/* Remove node */
|
||||
// Delete node with degree 1
|
||||
testRemove(avlTree, 8); // Delete node with degree 2
|
||||
testRemove(avlTree, 5); // Remove node with degree 1
|
||||
testRemove(avlTree, 4); // Remove node with degree 2
|
||||
/* Remove a node */
|
||||
// Observe how the AVL tree maintains balance after a node is removed
|
||||
testRemove(avlTree, 8); // Remove a node with 0 children
|
||||
testRemove(avlTree, 5); // Remove a node with 1 child
|
||||
testRemove(avlTree, 4); // Remove a node with 2 children
|
||||
|
||||
/* Search node */
|
||||
/* Look up a node */
|
||||
TreeNode *node = avlTree.search(7);
|
||||
cout << "\nFound node object is " << node << ", node value = " << node->val << endl;
|
||||
cout << "\nFound node address: " << node << ", node value = " << node->val << endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user