mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-09 05:56:06 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* Array-based binary tree class */
|
||||
/* Binary tree class represented by array */
|
||||
class ArrayBinaryTree {
|
||||
public:
|
||||
/* Constructor */
|
||||
@@ -19,25 +19,25 @@ class ArrayBinaryTree {
|
||||
return tree.size();
|
||||
}
|
||||
|
||||
/* Get the value of the node at index i */
|
||||
/* Get value of node at index i */
|
||||
int val(int i) {
|
||||
// If index is out of bounds, return INT_MAX, representing a null
|
||||
// Return INT_MAX if index out of bounds, representing empty position
|
||||
if (i < 0 || i >= size())
|
||||
return INT_MAX;
|
||||
return tree[i];
|
||||
}
|
||||
|
||||
/* Get the index of the left child of the node at index i */
|
||||
/* Get index of left child node of node at index i */
|
||||
int left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get the index of the right child of the node at index i */
|
||||
/* Get index of right child node of node at index i */
|
||||
int right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get the index of the parent of the node at index i */
|
||||
/* Get index of parent node of 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
|
||||
// Traverse array directly
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (val(i) != INT_MAX)
|
||||
res.push_back(val(i));
|
||||
@@ -53,21 +53,21 @@ class ArrayBinaryTree {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Pre-order traversal */
|
||||
/* Preorder traversal */
|
||||
vector<int> preOrder() {
|
||||
vector<int> res;
|
||||
dfs(0, "pre", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* In-order traversal */
|
||||
/* Inorder traversal */
|
||||
vector<int> inOrder() {
|
||||
vector<int> res;
|
||||
dfs(0, "in", res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Post-order traversal */
|
||||
/* Postorder traversal */
|
||||
vector<int> postOrder() {
|
||||
vector<int> res;
|
||||
dfs(0, "post", res);
|
||||
@@ -79,18 +79,18 @@ class ArrayBinaryTree {
|
||||
|
||||
/* Depth-first traversal */
|
||||
void dfs(int i, string order, vector<int> &res) {
|
||||
// If it is an empty spot, return
|
||||
// If empty position, return
|
||||
if (val(i) == INT_MAX)
|
||||
return;
|
||||
// Pre-order traversal
|
||||
// Preorder traversal
|
||||
if (order == "pre")
|
||||
res.push_back(val(i));
|
||||
dfs(left(i), order, res);
|
||||
// In-order traversal
|
||||
// Inorder traversal
|
||||
if (order == "in")
|
||||
res.push_back(val(i));
|
||||
dfs(right(i), order, res);
|
||||
// Post-order traversal
|
||||
// Postorder traversal
|
||||
if (order == "post")
|
||||
res.push_back(val(i));
|
||||
}
|
||||
@@ -99,38 +99,38 @@ class ArrayBinaryTree {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// Initialize binary tree
|
||||
// Use INT_MAX to represent an empty spot nullptr
|
||||
// Use INT_MAX to represent 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 << "Binary tree in array representation:\n";
|
||||
cout << "Array representation of binary tree:\n";
|
||||
printVector(arr);
|
||||
cout << "Binary tree in linked list representation:\n";
|
||||
cout << "Linked list representation of binary tree:\n";
|
||||
printTree(root);
|
||||
|
||||
// Array-based binary tree class
|
||||
// Binary tree class represented by array
|
||||
ArrayBinaryTree abt(arr);
|
||||
|
||||
// Access node
|
||||
int i = 1;
|
||||
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
|
||||
cout << "\nCurrent node's index is " << i << ", value = " << abt.val(i) << "\n";
|
||||
cout << "Its left child's index is " << l << ", value = " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
|
||||
cout << "Its right child's index is " << r << ", value = " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
|
||||
cout << "Its parent's index is " << p << ", value = " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
|
||||
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";
|
||||
|
||||
// Traverse tree
|
||||
vector<int> res = abt.levelOrder();
|
||||
cout << "\nLevel-order traversal is:";
|
||||
cout << "\nLevel-order traversal: ";
|
||||
printVector(res);
|
||||
res = abt.preOrder();
|
||||
cout << "Pre-order traversal is:";
|
||||
cout << "Pre-order traversal: ";
|
||||
printVector(res);
|
||||
res = abt.inOrder();
|
||||
cout << "In-order traversal is:";
|
||||
cout << "In-order traversal: ";
|
||||
printVector(res);
|
||||
res = abt.postOrder();
|
||||
cout << "Post-order traversal is:";
|
||||
cout << "Post-order traversal: ";
|
||||
printVector(res);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -19,13 +19,13 @@ class AVLTree {
|
||||
TreeNode *rightRotate(TreeNode *node) {
|
||||
TreeNode *child = node->left;
|
||||
TreeNode *grandChild = child->right;
|
||||
// Rotate node to the right around child
|
||||
// Using child as pivot, rotate node to the right
|
||||
child->right = node;
|
||||
node->left = grandChild;
|
||||
// Update node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return the root of the subtree after rotation
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
@@ -33,19 +33,19 @@ class AVLTree {
|
||||
TreeNode *leftRotate(TreeNode *node) {
|
||||
TreeNode *child = node->right;
|
||||
TreeNode *grandChild = child->left;
|
||||
// Rotate node to the left around child
|
||||
// Using child as pivot, rotate node to the left
|
||||
child->left = node;
|
||||
node->right = grandChild;
|
||||
// Update node height
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// Return the root of the subtree after rotation
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Perform rotation operation to restore balance to the subtree */
|
||||
/* Perform rotation operation to restore balance to this subtree */
|
||||
TreeNode *rotate(TreeNode *node) {
|
||||
// Get the balance factor of node
|
||||
// Get balance factor of node
|
||||
int _balanceFactor = balanceFactor(node);
|
||||
// Left-leaning tree
|
||||
if (_balanceFactor > 1) {
|
||||
@@ -69,7 +69,7 @@ class AVLTree {
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// Balanced tree, no rotation needed, return
|
||||
// Balanced tree, no rotation needed, return directly
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -83,19 +83,19 @@ class AVLTree {
|
||||
else if (val > node->val)
|
||||
node->right = insertHelper(node->right, val);
|
||||
else
|
||||
return node; // Do not insert duplicate nodes, return
|
||||
return node; // Duplicate node not inserted, return directly
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to the subtree */
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = rotate(node);
|
||||
// Return the root node of the subtree
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Recursively remove node (helper method) */
|
||||
/* Recursively delete node (helper method) */
|
||||
TreeNode *removeHelper(TreeNode *node, int val) {
|
||||
if (node == nullptr)
|
||||
return nullptr;
|
||||
/* 1. Find and remove the node */
|
||||
/* 1. Find node and delete */
|
||||
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, remove node and return
|
||||
// Number of child nodes = 0, delete node directly and return
|
||||
if (child == nullptr) {
|
||||
delete node;
|
||||
return nullptr;
|
||||
}
|
||||
// Number of child nodes = 1, remove node
|
||||
// Number of child nodes = 1, delete node directly
|
||||
else {
|
||||
delete node;
|
||||
node = child;
|
||||
}
|
||||
} else {
|
||||
// Number of child nodes = 2, remove the next node in in-order traversal and replace the current node with it
|
||||
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
|
||||
TreeNode *temp = node->right;
|
||||
while (temp->left != nullptr) {
|
||||
temp = temp->left;
|
||||
@@ -125,9 +125,9 @@ class AVLTree {
|
||||
}
|
||||
}
|
||||
updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to the subtree */
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = rotate(node);
|
||||
// Return the root node of the subtree
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ class AVLTree {
|
||||
/* Search node */
|
||||
TreeNode *search(int val) {
|
||||
TreeNode *cur = root;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != nullptr) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur->val < val)
|
||||
@@ -170,7 +170,7 @@ class AVLTree {
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur->val > val)
|
||||
cur = cur->left;
|
||||
// Found target node, break loop
|
||||
// Found target node, exit loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -190,23 +190,23 @@ class AVLTree {
|
||||
|
||||
void testInsert(AVLTree &tree, int val) {
|
||||
tree.insert(val);
|
||||
cout << "\nAfter inserting node " << val << ", the AVL tree is" << endl;
|
||||
cout << "\nAfter inserting node " << val << ", AVL tree is" << endl;
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
void testRemove(AVLTree &tree, int val) {
|
||||
tree.remove(val);
|
||||
cout << "\nAfter removing node " << val << ", the AVL tree is" << endl;
|
||||
cout << "\nAfter removing node " << val << ", AVL tree is" << endl;
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize empty AVL tree */
|
||||
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
|
||||
AVLTree avlTree;
|
||||
|
||||
/* Insert node */
|
||||
// Notice how the AVL tree maintains balance after inserting nodes
|
||||
// Delete nodes
|
||||
testInsert(avlTree, 1);
|
||||
testInsert(avlTree, 2);
|
||||
testInsert(avlTree, 3);
|
||||
@@ -218,16 +218,16 @@ int main() {
|
||||
testInsert(avlTree, 10);
|
||||
testInsert(avlTree, 6);
|
||||
|
||||
/* Insert duplicate node */
|
||||
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
|
||||
testInsert(avlTree, 7);
|
||||
|
||||
/* Remove node */
|
||||
// Notice how the AVL tree maintains balance after removing nodes
|
||||
testRemove(avlTree, 8); // Remove node with degree 0
|
||||
// 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
|
||||
|
||||
/* Search node */
|
||||
TreeNode *node = avlTree.search(7);
|
||||
cout << "\nThe found node object is " << node << ", node value =" << node->val << endl;
|
||||
cout << "\nFound node object is " << node << ", node value = " << node->val << endl;
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class BinarySearchTree {
|
||||
/* Search node */
|
||||
TreeNode *search(int num) {
|
||||
TreeNode *cur = root;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != nullptr) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur->val < num)
|
||||
@@ -39,7 +39,7 @@ class BinarySearchTree {
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur->val > num)
|
||||
cur = cur->left;
|
||||
// Found target node, break loop
|
||||
// Found target node, exit loop
|
||||
else
|
||||
break;
|
||||
}
|
||||
@@ -55,9 +55,9 @@ class BinarySearchTree {
|
||||
return;
|
||||
}
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != nullptr) {
|
||||
// Found duplicate node, thus return
|
||||
// Found duplicate node, return directly
|
||||
if (cur->val == num)
|
||||
return;
|
||||
pre = cur;
|
||||
@@ -78,38 +78,38 @@ class BinarySearchTree {
|
||||
|
||||
/* Remove node */
|
||||
void remove(int num) {
|
||||
// If tree is empty, return
|
||||
// If tree is empty, return directly
|
||||
if (root == nullptr)
|
||||
return;
|
||||
TreeNode *cur = root, *pre = nullptr;
|
||||
// Loop find, break after passing leaf nodes
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur != nullptr) {
|
||||
// Found node to be removed, break loop
|
||||
// Found node to delete, exit loop
|
||||
if (cur->val == num)
|
||||
break;
|
||||
pre = cur;
|
||||
// Node to be removed is in cur's right subtree
|
||||
// Node to delete is in cur's right subtree
|
||||
if (cur->val < num)
|
||||
cur = cur->right;
|
||||
// Node to be removed is in cur's left subtree
|
||||
// Node to delete is in cur's left subtree
|
||||
else
|
||||
cur = cur->left;
|
||||
}
|
||||
// If no node to be removed, return
|
||||
// If no node to delete, return directly
|
||||
if (cur == nullptr)
|
||||
return;
|
||||
// Number of child nodes = 0 or 1
|
||||
if (cur->left == nullptr || cur->right == nullptr) {
|
||||
// When the number of child nodes = 0 / 1, child = nullptr / that child node
|
||||
// When number of child nodes = 0 / 1, child = nullptr / that child node
|
||||
TreeNode *child = cur->left != nullptr ? cur->left : cur->right;
|
||||
// Remove node cur
|
||||
// Delete node cur
|
||||
if (cur != root) {
|
||||
if (pre->left == cur)
|
||||
pre->left = child;
|
||||
else
|
||||
pre->right = child;
|
||||
} else {
|
||||
// If the removed node is the root, reassign the root
|
||||
// If deleted node is root node, reassign root node
|
||||
root = child;
|
||||
}
|
||||
// Free memory
|
||||
@@ -117,13 +117,13 @@ class BinarySearchTree {
|
||||
}
|
||||
// Number of child nodes = 2
|
||||
else {
|
||||
// Get the next node in in-order traversal of cur
|
||||
// Get next node of cur in inorder traversal
|
||||
TreeNode *tmp = cur->right;
|
||||
while (tmp->left != nullptr) {
|
||||
tmp = tmp->left;
|
||||
}
|
||||
int tmpVal = tmp->val;
|
||||
// Recursively remove node tmp
|
||||
// Recursively delete node tmp
|
||||
remove(tmp->val);
|
||||
// Replace cur with tmp
|
||||
cur->val = tmpVal;
|
||||
@@ -135,32 +135,32 @@ class BinarySearchTree {
|
||||
int main() {
|
||||
/* Initialize binary search tree */
|
||||
BinarySearchTree *bst = new BinarySearchTree();
|
||||
// Note that different insertion orders can result in various tree structures. This particular sequence creates a perfect binary tree
|
||||
// Please note that different insertion orders will generate different binary trees, this sequence can generate 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 << "The initialized binary tree is\n" << endl;
|
||||
cout << endl << "Initialized binary tree is\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
|
||||
/* Search node */
|
||||
TreeNode *node = bst->search(7);
|
||||
cout << endl << "The found node object is " << node << ", node value =" << node->val << endl;
|
||||
cout << endl << "Found node object is " << node << ", node value = " << node->val << endl;
|
||||
|
||||
/* Insert node */
|
||||
bst->insert(16);
|
||||
cout << endl << "After inserting node 16, the binary tree is\n" << endl;
|
||||
cout << endl << "After inserting node 16, binary tree is\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
|
||||
/* Remove node */
|
||||
bst->remove(1);
|
||||
cout << endl << "After removing node 1, the binary tree is\n" << endl;
|
||||
cout << endl << "After removing node 1, binary tree is\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
bst->remove(2);
|
||||
cout << endl << "After removing node 2, the binary tree is\n" << endl;
|
||||
cout << endl << "After removing node 2, binary tree is\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
bst->remove(4);
|
||||
cout << endl << "After removing node 4, the binary tree is\n" << endl;
|
||||
cout << endl << "After removing node 4, binary tree is\n" << endl;
|
||||
printTree(bst->getRoot());
|
||||
|
||||
// Free memory
|
||||
|
||||
@@ -9,13 +9,13 @@
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize binary tree */
|
||||
// Initialize node
|
||||
// Initialize 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);
|
||||
// Construct node references (pointers)
|
||||
// Build references (pointers) between nodes
|
||||
n1->left = n2;
|
||||
n1->right = n3;
|
||||
n2->left = n4;
|
||||
@@ -23,9 +23,9 @@ int main() {
|
||||
cout << endl << "Initialize binary tree\n" << endl;
|
||||
printTree(n1);
|
||||
|
||||
/* Insert and remove nodes */
|
||||
/* Insert node P between n1 -> n2 */
|
||||
TreeNode *P = new TreeNode(0);
|
||||
// Insert node P between n1 -> n2
|
||||
// Delete node
|
||||
n1->left = P;
|
||||
P->left = n2;
|
||||
cout << endl << "After inserting node P\n" << endl;
|
||||
|
||||
@@ -11,16 +11,16 @@ vector<int> levelOrder(TreeNode *root) {
|
||||
// Initialize queue, add root node
|
||||
queue<TreeNode *> queue;
|
||||
queue.push(root);
|
||||
// Initialize a list to store the traversal sequence
|
||||
// Initialize a list to save the traversal sequence
|
||||
vector<int> vec;
|
||||
while (!queue.empty()) {
|
||||
TreeNode *node = queue.front();
|
||||
queue.pop(); // Queue dequeues
|
||||
queue.pop(); // Dequeue
|
||||
vec.push_back(node->val); // Save node value
|
||||
if (node->left != nullptr)
|
||||
queue.push(node->left); // Left child node enqueues
|
||||
queue.push(node->left); // Left child node enqueue
|
||||
if (node->right != nullptr)
|
||||
queue.push(node->right); // Right child node enqueues
|
||||
queue.push(node->right); // Right child node enqueue
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
@@ -28,14 +28,14 @@ vector<int> levelOrder(TreeNode *root) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize binary tree */
|
||||
// Use a specific function to convert an array into a binary tree
|
||||
// Here we use a function to generate 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;
|
||||
printTree(root);
|
||||
|
||||
/* Level-order traversal */
|
||||
vector<int> vec = levelOrder(root);
|
||||
cout << endl << "Sequence of nodes in level-order traversal = ";
|
||||
cout << endl << "Level-order traversal node print sequence = ";
|
||||
printVector(vec);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
// Initialize the list for storing traversal sequences
|
||||
// Initialize list for storing traversal sequence
|
||||
vector<int> vec;
|
||||
|
||||
/* Pre-order traversal */
|
||||
/* Preorder traversal */
|
||||
void preOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
@@ -19,7 +19,7 @@ void preOrder(TreeNode *root) {
|
||||
preOrder(root->right);
|
||||
}
|
||||
|
||||
/* In-order traversal */
|
||||
/* Inorder traversal */
|
||||
void inOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
@@ -29,7 +29,7 @@ void inOrder(TreeNode *root) {
|
||||
inOrder(root->right);
|
||||
}
|
||||
|
||||
/* Post-order traversal */
|
||||
/* Postorder traversal */
|
||||
void postOrder(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
@@ -42,27 +42,27 @@ void postOrder(TreeNode *root) {
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* Initialize binary tree */
|
||||
// Use a specific function to convert an array into a binary tree
|
||||
// Here we use a function to generate 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;
|
||||
printTree(root);
|
||||
|
||||
/* Pre-order traversal */
|
||||
/* Preorder traversal */
|
||||
vec.clear();
|
||||
preOrder(root);
|
||||
cout << endl << "Sequence of nodes in pre-order traversal = ";
|
||||
cout << endl << "Pre-order traversal node print sequence = ";
|
||||
printVector(vec);
|
||||
|
||||
/* In-order traversal */
|
||||
/* Inorder traversal */
|
||||
vec.clear();
|
||||
inOrder(root);
|
||||
cout << endl << "Sequence of nodes in in-order traversal = ";
|
||||
cout << endl << "In-order traversal node print sequence = ";
|
||||
printVector(vec);
|
||||
|
||||
/* Post-order traversal */
|
||||
/* Postorder traversal */
|
||||
vec.clear();
|
||||
postOrder(root);
|
||||
cout << endl << "Sequence of nodes in post-order traversal = ";
|
||||
cout << endl << "Post-order traversal node print sequence = ";
|
||||
printVector(vec);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user