mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-04 03:14:09 +08:00
* 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
148 lines
3.4 KiB
JavaScript
148 lines
3.4 KiB
JavaScript
/**
|
|
* 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);
|