mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 18:00:18 +08: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:
147
en/codes/javascript/chapter_tree/array_binary_tree.js
Normal file
147
en/codes/javascript/chapter_tree/array_binary_tree.js
Normal file
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* File: array_binary_tree.js
|
||||
* Created Time: 2023-08-06
|
||||
* Author: yuan0221 (yl1452491917@gmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Binary tree class represented by array */
|
||||
class ArrayBinaryTree {
|
||||
#tree;
|
||||
|
||||
/* Constructor */
|
||||
constructor(arr) {
|
||||
this.#tree = arr;
|
||||
}
|
||||
|
||||
/* List capacity */
|
||||
size() {
|
||||
return this.#tree.length;
|
||||
}
|
||||
|
||||
/* Get value of node at index i */
|
||||
val(i) {
|
||||
// If index out of bounds, return null to represent empty position
|
||||
if (i < 0 || i >= this.size()) return null;
|
||||
return this.#tree[i];
|
||||
}
|
||||
|
||||
/* Get index of left child node of node at index i */
|
||||
left(i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node of node at index i */
|
||||
right(i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node of node at index i */
|
||||
parent(i) {
|
||||
return Math.floor((i - 1) / 2); // Floor division
|
||||
}
|
||||
|
||||
/* Level-order traversal */
|
||||
levelOrder() {
|
||||
let res = [];
|
||||
// Traverse array directly
|
||||
for (let i = 0; i < this.size(); i++) {
|
||||
if (this.val(i) !== null) res.push(this.val(i));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Depth-first traversal */
|
||||
#dfs(i, order, res) {
|
||||
// If empty position, return
|
||||
if (this.val(i) === null) return;
|
||||
// Preorder traversal
|
||||
if (order === 'pre') res.push(this.val(i));
|
||||
this.#dfs(this.left(i), order, res);
|
||||
// Inorder traversal
|
||||
if (order === 'in') res.push(this.val(i));
|
||||
this.#dfs(this.right(i), order, res);
|
||||
// Postorder traversal
|
||||
if (order === 'post') res.push(this.val(i));
|
||||
}
|
||||
|
||||
/* Preorder traversal */
|
||||
preOrder() {
|
||||
const res = [];
|
||||
this.#dfs(0, 'pre', res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Inorder traversal */
|
||||
inOrder() {
|
||||
const res = [];
|
||||
this.#dfs(0, 'in', res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Postorder traversal */
|
||||
postOrder() {
|
||||
const res = [];
|
||||
this.#dfs(0, 'post', res);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
// Initialize binary tree
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
const arr = Array.of(
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
null,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
null,
|
||||
null,
|
||||
12,
|
||||
null,
|
||||
null,
|
||||
15
|
||||
);
|
||||
|
||||
const root = arrToTree(arr);
|
||||
console.log('\nInitialize binary tree\n');
|
||||
console.log('Array representation of binary tree:');
|
||||
console.log(arr);
|
||||
console.log('Linked list representation of binary tree:');
|
||||
printTree(root);
|
||||
|
||||
// Binary tree class represented by array
|
||||
const abt = new ArrayBinaryTree(arr);
|
||||
|
||||
// Access node
|
||||
const i = 1;
|
||||
const l = abt.left(i);
|
||||
const r = abt.right(i);
|
||||
const p = abt.parent(i);
|
||||
console.log('\nCurrent node index is ' + i + ', value is ' + abt.val(i));
|
||||
console.log(
|
||||
'Its left child node index is ' + l + ', value is ' + (l === null ? 'null' : abt.val(l))
|
||||
);
|
||||
console.log(
|
||||
'Its right child node index is ' + r + ', value is ' + (r === null ? 'null' : abt.val(r))
|
||||
);
|
||||
console.log(
|
||||
'Its parent node index is ' + p + ', value is ' + (p === null ? 'null' : abt.val(p))
|
||||
);
|
||||
|
||||
// Traverse tree
|
||||
let res = abt.levelOrder();
|
||||
console.log('\nLevel-order traversal is:' + res);
|
||||
res = abt.preOrder();
|
||||
console.log('Preorder traversal is:' + res);
|
||||
res = abt.inOrder();
|
||||
console.log('Inorder traversal is:' + res);
|
||||
res = abt.postOrder();
|
||||
console.log('Postorder traversal is:' + res);
|
||||
208
en/codes/javascript/chapter_tree/avl_tree.js
Normal file
208
en/codes/javascript/chapter_tree/avl_tree.js
Normal file
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* File: avl_tree.js
|
||||
* Created Time: 2023-02-05
|
||||
* Author: what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* AVL tree */
|
||||
class AVLTree {
|
||||
/* Constructor */
|
||||
constructor() {
|
||||
this.root = null; // Root node
|
||||
}
|
||||
|
||||
/* Get node height */
|
||||
height(node) {
|
||||
// Empty node height is -1, leaf node height is 0
|
||||
return node === null ? -1 : node.height;
|
||||
}
|
||||
|
||||
/* Update node height */
|
||||
#updateHeight(node) {
|
||||
// Node height equals the height of the tallest subtree + 1
|
||||
node.height =
|
||||
Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
}
|
||||
|
||||
/* Get balance factor */
|
||||
balanceFactor(node) {
|
||||
// Empty node balance factor is 0
|
||||
if (node === null) return 0;
|
||||
// Node balance factor = left subtree height - right subtree height
|
||||
return this.height(node.left) - this.height(node.right);
|
||||
}
|
||||
|
||||
/* Right rotation operation */
|
||||
#rightRotate(node) {
|
||||
const child = node.left;
|
||||
const grandChild = child.right;
|
||||
// Using child as pivot, rotate node to the right
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// Update node height
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Left rotation operation */
|
||||
#leftRotate(node) {
|
||||
const child = node.right;
|
||||
const grandChild = child.left;
|
||||
// Using child as pivot, rotate node to the left
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// Update node height
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// Return root node of subtree after rotation
|
||||
return child;
|
||||
}
|
||||
|
||||
/* Perform rotation operation to restore balance to this subtree */
|
||||
#rotate(node) {
|
||||
// Get balance factor of node
|
||||
const balanceFactor = this.balanceFactor(node);
|
||||
// Left-leaning tree
|
||||
if (balanceFactor > 1) {
|
||||
if (this.balanceFactor(node.left) >= 0) {
|
||||
// Right rotation
|
||||
return this.#rightRotate(node);
|
||||
} else {
|
||||
// First left rotation then right rotation
|
||||
node.left = this.#leftRotate(node.left);
|
||||
return this.#rightRotate(node);
|
||||
}
|
||||
}
|
||||
// Right-leaning tree
|
||||
if (balanceFactor < -1) {
|
||||
if (this.balanceFactor(node.right) <= 0) {
|
||||
// Left rotation
|
||||
return this.#leftRotate(node);
|
||||
} else {
|
||||
// First right rotation then left rotation
|
||||
node.right = this.#rightRotate(node.right);
|
||||
return this.#leftRotate(node);
|
||||
}
|
||||
}
|
||||
// Balanced tree, no rotation needed, return directly
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
insert(val) {
|
||||
this.root = this.#insertHelper(this.root, val);
|
||||
}
|
||||
|
||||
/* Recursively insert node (helper method) */
|
||||
#insertHelper(node, val) {
|
||||
if (node === null) return new TreeNode(val);
|
||||
/* 1. Find insertion position and insert node */
|
||||
if (val < node.val) node.left = this.#insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = this.#insertHelper(node.right, val);
|
||||
else return node; // Duplicate node not inserted, return directly
|
||||
this.#updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = this.#rotate(node);
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
remove(val) {
|
||||
this.root = this.#removeHelper(this.root, val);
|
||||
}
|
||||
|
||||
/* Recursively delete node (helper method) */
|
||||
#removeHelper(node, val) {
|
||||
if (node === null) return null;
|
||||
/* 1. Find node and delete */
|
||||
if (val < node.val) node.left = this.#removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = this.#removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left === null || node.right === null) {
|
||||
const child = node.left !== null ? node.left : node.right;
|
||||
// Number of child nodes = 0, delete node directly and return
|
||||
if (child === null) return null;
|
||||
// Number of child nodes = 1, delete node directly
|
||||
else node = child;
|
||||
} else {
|
||||
// Number of child nodes = 2, delete the next node in inorder traversal and replace current node with it
|
||||
let temp = node.right;
|
||||
while (temp.left !== null) {
|
||||
temp = temp.left;
|
||||
}
|
||||
node.right = this.#removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
this.#updateHeight(node); // Update node height
|
||||
/* 2. Perform rotation operation to restore balance to this subtree */
|
||||
node = this.#rotate(node);
|
||||
// Return root node of subtree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
search(val) {
|
||||
let cur = this.root;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur !== null) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur.val < val) cur = cur.right;
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur.val > val) cur = cur.left;
|
||||
// Found target node, exit loop
|
||||
else break;
|
||||
}
|
||||
// Return target node
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
|
||||
function testInsert(tree, val) {
|
||||
tree.insert(val);
|
||||
console.log('\nInsert node ' + val + ', AVL tree is');
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
function testRemove(tree, val) {
|
||||
tree.remove(val);
|
||||
console.log('\nRemove node ' + val + ', AVL tree is');
|
||||
printTree(tree.root);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Please pay attention to how the AVL tree maintains balance after inserting nodes */
|
||||
const avlTree = new AVLTree();
|
||||
/* Insert node */
|
||||
// Delete nodes
|
||||
testInsert(avlTree, 1);
|
||||
testInsert(avlTree, 2);
|
||||
testInsert(avlTree, 3);
|
||||
testInsert(avlTree, 4);
|
||||
testInsert(avlTree, 5);
|
||||
testInsert(avlTree, 8);
|
||||
testInsert(avlTree, 7);
|
||||
testInsert(avlTree, 9);
|
||||
testInsert(avlTree, 10);
|
||||
testInsert(avlTree, 6);
|
||||
|
||||
/* Please pay attention to how the AVL tree maintains balance after deleting nodes */
|
||||
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
|
||||
|
||||
/* Search node */
|
||||
const node = avlTree.search(7);
|
||||
console.log('\nFound node object is', node, ', node value = ' + node.val);
|
||||
139
en/codes/javascript/chapter_tree/binary_search_tree.js
Normal file
139
en/codes/javascript/chapter_tree/binary_search_tree.js
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* File: binary_search_tree.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Binary search tree */
|
||||
class BinarySearchTree {
|
||||
/* Constructor */
|
||||
constructor() {
|
||||
// Initialize empty tree
|
||||
this.root = null;
|
||||
}
|
||||
|
||||
/* Get binary tree root node */
|
||||
getRoot() {
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* Search node */
|
||||
search(num) {
|
||||
let cur = this.root;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur !== null) {
|
||||
// Target node is in cur's right subtree
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// Target node is in cur's left subtree
|
||||
else if (cur.val > num) cur = cur.left;
|
||||
// Found target node, exit loop
|
||||
else break;
|
||||
}
|
||||
// Return target node
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* Insert node */
|
||||
insert(num) {
|
||||
// If tree is empty, initialize root node
|
||||
if (this.root === null) {
|
||||
this.root = new TreeNode(num);
|
||||
return;
|
||||
}
|
||||
let cur = this.root,
|
||||
pre = null;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur !== null) {
|
||||
// Found duplicate node, return directly
|
||||
if (cur.val === num) return;
|
||||
pre = cur;
|
||||
// Insertion position is in cur's right subtree
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// Insertion position is in cur's left subtree
|
||||
else cur = cur.left;
|
||||
}
|
||||
// Insert node
|
||||
const node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
}
|
||||
|
||||
/* Remove node */
|
||||
remove(num) {
|
||||
// If tree is empty, return directly
|
||||
if (this.root === null) return;
|
||||
let cur = this.root,
|
||||
pre = null;
|
||||
// Loop search, exit after passing leaf node
|
||||
while (cur !== null) {
|
||||
// Found node to delete, exit loop
|
||||
if (cur.val === num) break;
|
||||
pre = cur;
|
||||
// Node to delete is in cur's right subtree
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// Node to delete is in cur's left subtree
|
||||
else cur = cur.left;
|
||||
}
|
||||
// If no node to delete, return directly
|
||||
if (cur === null) return;
|
||||
// Number of child nodes = 0 or 1
|
||||
if (cur.left === null || cur.right === null) {
|
||||
// When number of child nodes = 0 / 1, child = null / that child node
|
||||
const child = cur.left !== null ? cur.left : cur.right;
|
||||
// Delete node cur
|
||||
if (cur !== this.root) {
|
||||
if (pre.left === cur) pre.left = child;
|
||||
else pre.right = child;
|
||||
} else {
|
||||
// If deleted node is root node, reassign root node
|
||||
this.root = child;
|
||||
}
|
||||
}
|
||||
// Number of child nodes = 2
|
||||
else {
|
||||
// Get next node of cur in inorder traversal
|
||||
let tmp = cur.right;
|
||||
while (tmp.left !== null) {
|
||||
tmp = tmp.left;
|
||||
}
|
||||
// Recursively delete node tmp
|
||||
this.remove(tmp.val);
|
||||
// Replace cur with tmp
|
||||
cur.val = tmp.val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize binary search tree */
|
||||
const bst = new BinarySearchTree();
|
||||
// Please note that different insertion orders will generate different binary trees, this sequence can generate a perfect binary tree
|
||||
const nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
|
||||
for (const num of nums) {
|
||||
bst.insert(num);
|
||||
}
|
||||
console.log('\nInitialized binary tree is\n');
|
||||
printTree(bst.getRoot());
|
||||
|
||||
/* Search node */
|
||||
const node = bst.search(7);
|
||||
console.log('\nFound node object is ' + node + ', node value = ' + node.val);
|
||||
|
||||
/* Insert node */
|
||||
bst.insert(16);
|
||||
console.log('\nAfter inserting node 16, binary tree is\n');
|
||||
printTree(bst.getRoot());
|
||||
|
||||
/* Remove node */
|
||||
bst.remove(1);
|
||||
console.log('\nAfter removing node 1, binary tree is\n');
|
||||
printTree(bst.getRoot());
|
||||
bst.remove(2);
|
||||
console.log('\nAfter removing node 2, binary tree is\n');
|
||||
printTree(bst.getRoot());
|
||||
bst.remove(4);
|
||||
console.log('\nAfter removing node 4, binary tree is\n');
|
||||
printTree(bst.getRoot());
|
||||
35
en/codes/javascript/chapter_tree/binary_tree.js
Normal file
35
en/codes/javascript/chapter_tree/binary_tree.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: binary_tree.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Initialize binary tree */
|
||||
// Initialize nodes
|
||||
let n1 = new TreeNode(1),
|
||||
n2 = new TreeNode(2),
|
||||
n3 = new TreeNode(3),
|
||||
n4 = new TreeNode(4),
|
||||
n5 = new TreeNode(5);
|
||||
// Build references (pointers) between nodes
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
n2.left = n4;
|
||||
n2.right = n5;
|
||||
console.log('\nInitialize binary tree\n');
|
||||
printTree(n1);
|
||||
|
||||
/* Insert node P between n1 -> n2 */
|
||||
const P = new TreeNode(0);
|
||||
// Delete node
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
console.log('\nAfter inserting node P\n');
|
||||
printTree(n1);
|
||||
// Remove node P
|
||||
n1.left = n2;
|
||||
console.log('\nAfter removing node P\n');
|
||||
printTree(n1);
|
||||
34
en/codes/javascript/chapter_tree/binary_tree_bfs.js
Normal file
34
en/codes/javascript/chapter_tree/binary_tree_bfs.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* File: binary_tree_bfs.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Level-order traversal */
|
||||
function levelOrder(root) {
|
||||
// Initialize queue, add root node
|
||||
const queue = [root];
|
||||
// Initialize a list to save the traversal sequence
|
||||
const list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // Dequeue
|
||||
list.push(node.val); // Save node value
|
||||
if (node.left) queue.push(node.left); // Left child node enqueue
|
||||
if (node.right) queue.push(node.right); // Right child node enqueue
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree\n');
|
||||
printTree(root);
|
||||
|
||||
/* Level-order traversal */
|
||||
const list = levelOrder(root);
|
||||
console.log('\nLevel-order traversal node print sequence = ' + list);
|
||||
60
en/codes/javascript/chapter_tree/binary_tree_dfs.js
Normal file
60
en/codes/javascript/chapter_tree/binary_tree_dfs.js
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: binary_tree_dfs.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
// Initialize list for storing traversal sequence
|
||||
const list = [];
|
||||
|
||||
/* Preorder traversal */
|
||||
function preOrder(root) {
|
||||
if (root === null) return;
|
||||
// Visit priority: root node -> left subtree -> right subtree
|
||||
list.push(root.val);
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
/* Inorder traversal */
|
||||
function inOrder(root) {
|
||||
if (root === null) return;
|
||||
// Visit priority: left subtree -> root node -> right subtree
|
||||
inOrder(root.left);
|
||||
list.push(root.val);
|
||||
inOrder(root.right);
|
||||
}
|
||||
|
||||
/* Postorder traversal */
|
||||
function postOrder(root) {
|
||||
if (root === null) return;
|
||||
// Visit priority: left subtree -> right subtree -> root node
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
list.push(root.val);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree\n');
|
||||
printTree(root);
|
||||
|
||||
/* Preorder traversal */
|
||||
list.length = 0;
|
||||
preOrder(root);
|
||||
console.log('\nPreorder traversal node print sequence = ' + list);
|
||||
|
||||
/* Inorder traversal */
|
||||
list.length = 0;
|
||||
inOrder(root);
|
||||
console.log('\nInorder traversal node print sequence = ' + list);
|
||||
|
||||
/* Postorder traversal */
|
||||
list.length = 0;
|
||||
postOrder(root);
|
||||
console.log('\nPostorder traversal node print sequence = ' + list);
|
||||
Reference in New Issue
Block a user