Update JavaScript and TypeScript codes for all chapters, rename JavaScript and TypeScript import folder to modules (#402)

* Update JavaScript and TypeScript codes

* Rename JavaScript and TypeScript import folder to modules
This commit is contained in:
Justin Tse
2023-03-03 01:34:53 +08:00
committed by GitHub
parent 7b41e6c2f0
commit e4a98bc9c5
61 changed files with 324 additions and 290 deletions

View File

@@ -0,0 +1,51 @@
/**
* File: ListNode.js
* Created Time: 2022-12-12
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/**
* Definition for a singly-linked list node
*/
class ListNode {
val;
next;
constructor(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
}
/**
* Generate a linked list with an array
* @param arr
* @return
*/
function arrToLinkedList(arr) {
const dum = new ListNode(0);
let head = dum;
for (const val of arr) {
head.next = new ListNode(val);
head = head.next;
}
return dum.next;
}
/**
* Get a list node with specific value from a linked list
* @param head
* @param val
* @return
*/
function getListNode(head, val) {
while (head !== null && head.val !== val) {
head = head.next;
}
return head;
}
module.exports = {
ListNode,
arrToLinkedList,
getListNode
};

View File

@@ -0,0 +1,102 @@
/**
* File: PrintUtil.js
* Created Time: 2022-12-04
* Author: IsChristina (christinaxia77@foxmail.com)
*/
const { arrToTree } = require("./TreeNode");
/**
* Print a linked list
* @param head
*/
function printLinkedList(head) {
let list = [];
while (head !== null) {
list.push(head.val.toString());
head = head.next;
}
console.log(list.join(" -> "));
}
function Trunk(prev, str) {
this.prev = prev;
this.str = str;
}
/**
* The interface of the tree printer
* This tree printer is borrowed from TECHIE DELIGHT
* https://www.techiedelight.com/c-program-print-binary-tree/
* @param root
*/
function printTree(root) {
printTree(root, null, false);
}
/**
* Print a binary tree
* @param root
* @param prev
* @param isLeft
*/
function printTree(root, prev, isLeft) {
if (root === null) {
return;
}
let prev_str = " ";
let trunk = new Trunk(prev, prev_str);
printTree(root.right, trunk, true);
if (!prev) {
trunk.str = "———";
} else if (isLeft) {
trunk.str = "/———";
prev_str = " |";
} else {
trunk.str = "\\———";
prev.str = prev_str;
}
showTrunks(trunk);
console.log(" " + root.val);
if (prev) {
prev.str = prev_str;
}
trunk.str = " |";
printTree(root.left, trunk, false);
}
/**
* Helper function to print branches of the binary tree
* @param p
*/
function showTrunks(p) {
if (!p) {
return;
}
showTrunks(p.prev);
process.stdout.write(p.str);
}
/**
* Print a heap
* @param arr
*/
function printHeap(arr) {
console.log("堆的数组表示:");
console.log(arr);
console.log("堆的树状表示:");
printTree(arrToTree(arr));
}
module.exports = {
printLinkedList,
printTree,
printHeap
};

View File

@@ -0,0 +1,54 @@
/**
* File: TreeNode.js
* Created Time: 2022-12-04
* Author: IsChristina (christinaxia77@foxmail.com)
*/
/**
* Definition for a binary tree node.
*/
class TreeNode {
val; // 结点值
left; // 左子结点指针
right; // 右子结点指针
height; //结点高度
constructor(val, left, right, height) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
this.height = height === undefined ? 0 : height;
}
}
/**
* Generate a binary tree given an array
* @param arr
* @return
*/
function arrToTree(arr) {
if (arr.length === 0) return null;
let root = new TreeNode(arr[0]);
let queue = [root];
let i = 0;
while (queue.length) {
let node = queue.shift();
if (++i >= arr.length) break;
if (arr[i] !== null) {
node.left = new TreeNode(arr[i]);
queue.push(node.left);
}
if (++i >= arr.length) break;
if (arr[i] !== null) {
node.right = new TreeNode(arr[i]);
queue.push(node.right);
}
}
return root;
}
module.exports = {
TreeNode,
arrToTree
};

View File

@@ -0,0 +1,36 @@
/**
* File: Vertex.js
* Created Time: 2023-02-15
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
/* 顶点类 */
class Vertex {
val;
constructor(val) {
this.val = val;
}
/* 输入值列表 vals ,返回顶点列表 vets */
static valsToVets(vals) {
const vets = [];
for (let i = 0; i < vals.length; i++) {
vets[i] = new Vertex(vals[i]);
}
return vets;
}
/* 输入顶点列表 vets ,返回值列表 vals */
static vetsToVals(vets) {
const vals = [];
for (const vet of vets) {
vals.push(vet.val);
}
return vals;
}
}
module.exports = {
Vertex
};