mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-21 19:23:17 +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:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Binary tree class represented by array */
|
||||
/* Binary tree class using an array representation */
|
||||
class ArrayBinaryTree {
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -14,30 +14,30 @@ class ArrayBinaryTree {
|
||||
tree = arr;
|
||||
}
|
||||
|
||||
/* List capacity */
|
||||
/* Number of positions in the array */
|
||||
int size() {
|
||||
return tree.size();
|
||||
}
|
||||
|
||||
/* Get value of node at index i */
|
||||
/* Get the value of the node at index i */
|
||||
int val(int i) {
|
||||
// Return INT_MAX if index out of bounds, representing empty position
|
||||
// If the index is out of bounds, return INT_MAX to represent an empty position
|
||||
if (i < 0 || i >= size())
|
||||
return INT_MAX;
|
||||
return tree[i];
|
||||
}
|
||||
|
||||
/* Get index of left child node of node at index i */
|
||||
/* Get the left child index of the node at index i */
|
||||
int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node of node at index i */
|
||||
/* Get the right child index of the node at index i */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node of node at index i */
|
||||
/* Get the parent index of the node at index i */
|
||||
int parent(int i) {
|
||||
return (i - 1) / 2;
|
||||
}
|
||||
@@ -45,7 +45,7 @@ class ArrayBinaryTree {
|
||||
/* Level-order traversal */
|
||||
vector<int> levelOrder() {
|
||||
vector<int> res;
|
||||
// Traverse array directly
|
||||
// Traverse the array directly
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != INT_MAX)
|
||||
res.push_back(val(i));
|
||||
@@ -79,7 +79,7 @@ class ArrayBinaryTree {
|
||||
|
||||
/* Depth-first traversal */
|
||||
void dfs(int i, string order, vector<int> &res) {
|
||||
// If empty position, return
|
||||
// Return if this position is empty
|
||||
if (val(i) == INT_MAX)
|
||||
return;
|
||||
// Preorder traversal
|
||||
@@ -96,41 +96,41 @@ class ArrayBinaryTree {
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
// Initialize binary tree
|
||||
// Use INT_MAX to represent empty position nullptr
|
||||
// Initialize the binary tree
|
||||
// Use INT_MAX to represent an empty position (nullptr)
|
||||
vector<int> arr = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
|
||||
TreeNode *root = vectorToTree(arr);
|
||||
cout << "\nInitialize binary tree\n";
|
||||
cout << "Array representation of binary tree:\n";
|
||||
cout << "\nInitialize the binary tree\n";
|
||||
cout << "Array representation of the binary tree:\n";
|
||||
printVector(arr);
|
||||
cout << "Linked list representation of binary tree:\n";
|
||||
cout << "Linked representation of the binary tree:\n";
|
||||
printTree(root);
|
||||
|
||||
// Binary tree class represented by array
|
||||
// Binary tree class using an array representation
|
||||
ArrayBinaryTree abt(arr);
|
||||
|
||||
// Access node
|
||||
// Access a node
|
||||
int i = 1;
|
||||
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
|
||||
cout << "\nCurrent node index is " << i << ", value is " << abt.val(i) << "\n";
|
||||
cout << "Its left child node index is " << l << ", value is " << (abt.val(l) != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
|
||||
cout << "Its right child node index is " << r << ", value is " << (abt.val(r) != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
|
||||
cout << "Its parent node index is " << p << ", value is " << (abt.val(p) != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
|
||||
cout << "\nCurrent node index: " << i << ", value: " << abt.val(i) << "\n";
|
||||
cout << "Left child index: " << l << ", value: " << (abt.val(l) != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
|
||||
cout << "Right child index: " << r << ", value: " << (abt.val(r) != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
|
||||
cout << "Parent index: " << p << ", value: " << (abt.val(p) != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
|
||||
|
||||
// Traverse tree
|
||||
// Traverse the tree
|
||||
vector<int> res = abt.levelOrder();
|
||||
cout << "\nLevel-order traversal: ";
|
||||
printVector(res);
|
||||
res = abt.preOrder();
|
||||
cout << "Pre-order traversal: ";
|
||||
cout << "Preorder traversal: ";
|
||||
printVector(res);
|
||||
res = abt.inOrder();
|
||||
cout << "In-order traversal: ";
|
||||
cout << "Inorder traversal: ";
|
||||
printVector(res);
|
||||
res = abt.postOrder();
|
||||
cout << "Post-order traversal: ";
|
||||
cout << "Postorder traversal: ";
|
||||
printVector(res);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ class BinarySearchTree {
|
||||
public:
|
||||
/* Constructor */
|
||||
BinarySearchTree() {
|
||||
// Initialize empty tree
|
||||
// Initialize an empty tree
|
||||
root = nullptr;
|
||||
}
|
||||
|
||||
@@ -23,52 +23,52 @@ class BinarySearchTree {
|
||||
freeMemoryTree(root);
|
||||
}
|
||||
|
||||
/* Get binary tree root node */
|
||||
/* Get the root of the binary tree */
|
||||
TreeNode *getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
/* Find a node */
|
||||
TreeNode *search(int num) {
|
||||
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 < num)
|
||||
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 > num)
|
||||
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;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
/* Insert a node */
|
||||
void insert(int num) {
|
||||
// If tree is empty, initialize root node
|
||||
// If the tree is empty, initialize the root node
|
||||
if (root == nullptr) {
|
||||
root = new TreeNode(num);
|
||||
return;
|
||||
}
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// Loop search, exit after passing leaf node
|
||||
// Continue searching while the current node pointer is not null
|
||||
while (cur != nullptr) {
|
||||
// Found duplicate node, return directly
|
||||
// Return immediately if a duplicate node is found
|
||||
if (cur->val == num)
|
||||
return;
|
||||
pre = cur;
|
||||
// Insertion position is in cur's right subtree
|
||||
// The insertion position lies in the right subtree of cur
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// Insertion position is in cur's left subtree
|
||||
// The insertion position lies in the left subtree of cur
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// Insert node
|
||||
// Insert a node
|
||||
TreeNode *node = new TreeNode(num);
|
||||
if (pre->val < num)
|
||||
pre->right = node;
|
||||
@@ -76,91 +76,91 @@ class BinarySearchTree {
|
||||
pre->left = node;
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
/* Remove a node */
|
||||
void remove(int num) {
|
||||
// If tree is empty, return directly
|
||||
// Return immediately if the tree is empty
|
||||
if (root == nullptr)
|
||||
return;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// Loop search, exit after passing leaf node
|
||||
// Continue searching while the current node pointer is not null
|
||||
while (cur != nullptr) {
|
||||
// Found node to delete, exit loop
|
||||
// The node to remove was found; exit the loop
|
||||
if (cur->val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// Node to delete is in cur's right subtree
|
||||
// The node to remove lies in the right subtree of cur
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// Node to delete is in cur's left subtree
|
||||
// The node to remove lies in the left subtree of cur
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// If no node to delete, return directly
|
||||
// Return immediately if the node to remove does not exist
|
||||
if (cur == nullptr)
|
||||
return;
|
||||
// Number of child nodes = 0 or 1
|
||||
// Number of children = 0 or 1
|
||||
if (cur->left == nullptr || cur->right == nullptr) {
|
||||
// When number of child nodes = 0 / 1, child = nullptr / that child node
|
||||
// With 0 or 1 child, child is nullptr or that child, respectively
|
||||
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
|
||||
// Delete node cur
|
||||
// Remove node cur
|
||||
if (cur != root) {
|
||||
if (pre->left == cur)
|
||||
pre->left = child;
|
||||
else
|
||||
pre->right = child;
|
||||
} else {
|
||||
// If deleted node is root node, reassign root node
|
||||
// If the removed node is the root, assign the new root
|
||||
root = child;
|
||||
}
|
||||
// Free memory
|
||||
delete cur;
|
||||
}
|
||||
// Number of child nodes = 2
|
||||
// Number of children = 2
|
||||
else {
|
||||
// Get next node of cur in inorder traversal
|
||||
// Get the inorder successor of cur
|
||||
TreeNode *tmp = cur->right;
|
||||
while (tmp->left != nullptr) {
|
||||
tmp = tmp->left;
|
||||
}
|
||||
int tmpVal = tmp->val;
|
||||
// Recursively delete node tmp
|
||||
// Recursively remove node tmp
|
||||
remove(tmp->val);
|
||||
// Replace cur with tmp
|
||||
// Replace the value of cur with the value of tmp
|
||||
cur->val = tmpVal;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Initialize binary search tree */
|
||||
/* Initialize the binary search tree */
|
||||
BinarySearchTree *bst = new BinarySearchTree();
|
||||
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
|
||||
// Different insertion orders produce different binary trees; this sequence produces a perfect binary tree
|
||||
vector<int> nums = {8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15};
|
||||
for (int num : nums) {
|
||||
bst->insert(num);
|
||||
}
|
||||
cout << endl << "Initialized binary tree is\n" << endl;
|
||||
cout << endl << "Binary tree after initialization:\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
|
||||
/* Search node */
|
||||
/* Find a node */
|
||||
TreeNode *node = bst->search(7);
|
||||
cout << endl << "Found node object is " << node << ", node value = " << node->val << endl;
|
||||
cout << endl << "Found node address: " << node << ", node value = " << node->val << endl;
|
||||
|
||||
/* Insert node */
|
||||
/* Insert a node */
|
||||
bst->insert(16);
|
||||
cout << endl << "After inserting node 16, binary tree is\n" << endl;
|
||||
cout << endl << "Binary tree after inserting node 16:\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
|
||||
/* Remove node */
|
||||
/* Remove a node */
|
||||
bst->remove(1);
|
||||
cout << endl << "After removing node 1, binary tree is\n" << endl;
|
||||
cout << endl << "Binary tree after removing node 1:\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
bst->remove(2);
|
||||
cout << endl << "After removing node 2, binary tree is\n" << endl;
|
||||
cout << endl << "Binary tree after removing node 2:\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
bst->remove(4);
|
||||
cout << endl << "After removing node 4, binary tree is\n" << endl;
|
||||
cout << endl << "Binary tree after removing node 4:\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
|
||||
// Free memory
|
||||
|
||||
@@ -6,34 +6,34 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Initialize binary tree */
|
||||
// Initialize nodes
|
||||
/* Initialize the binary tree */
|
||||
// Initialize the nodes
|
||||
TreeNode *n1 = new TreeNode(1);
|
||||
TreeNode *n2 = new TreeNode(2);
|
||||
TreeNode *n3 = new TreeNode(3);
|
||||
TreeNode *n4 = new TreeNode(4);
|
||||
TreeNode *n5 = new TreeNode(5);
|
||||
// Build references (pointers) between nodes
|
||||
// Set up references (pointers) between the nodes
|
||||
n1->left = n2;
|
||||
n1->right = n3;
|
||||
n2->left = n4;
|
||||
n2->right = n5;
|
||||
cout << endl << "Initialize binary tree\n" << endl;
|
||||
cout << endl << "Initialize the binary tree\n" << endl;
|
||||
printTree(n1);
|
||||
|
||||
/* Insert node P between n1 -> n2 */
|
||||
/* Insert and remove nodes */
|
||||
TreeNode *P = new TreeNode(0);
|
||||
// Delete node
|
||||
// Insert node P between n1 and n2 in n1 -> n2
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
cout << endl << "After inserting node P\n" << endl;
|
||||
cout << endl << "After inserting node P:\n" << endl;
|
||||
printTree(n1);
|
||||
// Remove node P
|
||||
n1->left = n2;
|
||||
delete P; // Free memory
|
||||
cout << endl << "After removing node P\n" << endl;
|
||||
cout << endl << "After removing node P:\n" << endl;
|
||||
printTree(n1);
|
||||
|
||||
// Free memory
|
||||
|
||||
@@ -8,34 +8,34 @@
|
||||
|
||||
/* Level-order traversal */
|
||||
vector<int> levelOrder(TreeNode *root) {
|
||||
// Initialize queue, add root node
|
||||
// Initialize a queue and enqueue the root
|
||||
queue<TreeNode *> queue;
|
||||
queue.push(root);
|
||||
// Initialize a list to save the traversal sequence
|
||||
// Initialize a list to store the traversal sequence
|
||||
vector<int> vec;
|
||||
while (!queue.empty()) {
|
||||
TreeNode *node = queue.front();
|
||||
queue.pop(); // Dequeue
|
||||
vec.push_back(node->val); // Save node value
|
||||
queue.pop(); // Dequeue from the queue
|
||||
vec.push_back(node->val); // Store the node value
|
||||
if (node->left != nullptr)
|
||||
queue.push(node->left); // Left child node enqueue
|
||||
queue.push(node->left); // Enqueue the left child
|
||||
if (node->right != nullptr)
|
||||
queue.push(node->right); // Right child node enqueue
|
||||
queue.push(node->right); // Enqueue the right child
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
/* Initialize the binary tree */
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
cout << endl << "Initialize binary tree\n" << endl;
|
||||
cout << endl << "Initialize the binary tree\n" << endl;
|
||||
printTree(root);
|
||||
|
||||
/* Level-order traversal */
|
||||
vector<int> vec = levelOrder(root);
|
||||
cout << endl << "Level-order traversal node print sequence = ";
|
||||
cout << endl << "Node sequence from level-order traversal = ";
|
||||
printVector(vec);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
// Initialize list for storing traversal sequence
|
||||
// Initialize a list to store the traversal sequence
|
||||
vector<int> vec;
|
||||
|
||||
/* Preorder traversal */
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// Visit priority: root node -> left subtree -> right subtree
|
||||
// Visit order: root -> left subtree -> right subtree
|
||||
vec.push_back(root->val);
|
||||
preOrder(root->left);
|
||||
preOrder(root->right);
|
||||
@@ -23,7 +23,7 @@ void preOrder(TreeNode *root) {
|
||||
void inOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// Visit priority: left subtree -> root node -> right subtree
|
||||
// Visit order: left subtree -> root -> right subtree
|
||||
inOrder(root->left);
|
||||
vec.push_back(root->val);
|
||||
inOrder(root->right);
|
||||
@@ -33,36 +33,36 @@ void inOrder(TreeNode *root) {
|
||||
void postOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
// Visit priority: left subtree -> right subtree -> root node
|
||||
// Visit order: left subtree -> right subtree -> root
|
||||
postOrder(root->left);
|
||||
postOrder(root->right);
|
||||
vec.push_back(root->val);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Driver code */
|
||||
int main() {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
/* Initialize the binary tree */
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
TreeNode *root = vectorToTree(vector<int>{1, 2, 3, 4, 5, 6, 7});
|
||||
cout << endl << "Initialize binary tree\n" << endl;
|
||||
cout << endl << "Initialize the binary tree\n" << endl;
|
||||
printTree(root);
|
||||
|
||||
/* Preorder traversal */
|
||||
vec.clear();
|
||||
preOrder(root);
|
||||
cout << endl << "Pre-order traversal node print sequence = ";
|
||||
cout << endl << "Node sequence from preorder traversal = ";
|
||||
printVector(vec);
|
||||
|
||||
/* Inorder traversal */
|
||||
vec.clear();
|
||||
inOrder(root);
|
||||
cout << endl << "In-order traversal node print sequence = ";
|
||||
cout << endl << "Node sequence from inorder traversal = ";
|
||||
printVector(vec);
|
||||
|
||||
/* Postorder traversal */
|
||||
vec.clear();
|
||||
postOrder(root);
|
||||
cout << endl << "Post-order traversal node print sequence = ";
|
||||
cout << endl << "Node sequence from postorder traversal = ";
|
||||
printVector(vec);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user