mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-05 12:14:20 +00:00
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:
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: ListNode.ts
|
||||
* Created Time: 2022-12-10
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Definition for a singly-linked list node
|
||||
*/
|
||||
class ListNode {
|
||||
val: number;
|
||||
next: ListNode | null;
|
||||
constructor(val?: number, next?: ListNode | null) {
|
||||
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: number[]): ListNode | null {
|
||||
const dum: ListNode = 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: ListNode | null, val: number): ListNode | null {
|
||||
while (head !== null && head.val !== val) {
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
export {
|
||||
ListNode,
|
||||
arrToLinkedList,
|
||||
getListNode
|
||||
};
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* File: PrintUtil.ts
|
||||
* Created Time: 2022-12-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
import { ListNode } from "./ListNode";
|
||||
import { TreeNode, arrToTree } from "./TreeNode";
|
||||
|
||||
/**
|
||||
* Print a linked list
|
||||
* @param head
|
||||
*/
|
||||
function printLinkedList(head: ListNode | null): void {
|
||||
const list: string[] = [];
|
||||
while (head !== null) {
|
||||
list.push(head.val.toString());
|
||||
head = head.next;
|
||||
}
|
||||
console.log(list.join(' -> '));
|
||||
}
|
||||
|
||||
class Trunk {
|
||||
prev: Trunk | null;
|
||||
str: string;
|
||||
|
||||
constructor(prev: Trunk | null, str: string) {
|
||||
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: TreeNode | null) {
|
||||
printTreeHelper(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
function printTreeHelper(root: TreeNode | null, prev: Trunk | null, isLeft: boolean) {
|
||||
if (root === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let prev_str = ' ';
|
||||
const trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTreeHelper(root.right, trunk, true);
|
||||
|
||||
if (prev === null) {
|
||||
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 = ' |';
|
||||
|
||||
printTreeHelper(root.left, trunk, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to print branches of the binary tree
|
||||
* @param p
|
||||
*/
|
||||
function showTrunks(p: Trunk | null) {
|
||||
if (p === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
process.stdout.write(p.str);
|
||||
// ts-node to execute, we need to install type definitions for node
|
||||
// solve: npm i --save-dev @types/node
|
||||
// restart the vscode
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a heap
|
||||
* @param arr
|
||||
*/
|
||||
function printHeap(arr: number[]): void {
|
||||
console.log("堆的数组表示:");
|
||||
console.log(arr);
|
||||
console.log("堆的树状表示:");
|
||||
const root = arrToTree(arr);
|
||||
printTree(root);
|
||||
}
|
||||
|
||||
export {
|
||||
printLinkedList,
|
||||
printTree,
|
||||
printHeap
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: TreeNode.ts
|
||||
* Created Time: 2022-12-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
*/
|
||||
class TreeNode {
|
||||
val: number; // 结点值
|
||||
height: number; // 结点高度
|
||||
left: TreeNode | null; // 左子结点指针
|
||||
right: TreeNode | null; // 右子结点指针
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
function arrToTree(arr: (number | null)[]): TreeNode | null {
|
||||
if (arr.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const root = new TreeNode(arr[0] as number);
|
||||
const queue = [root];
|
||||
let i = 0;
|
||||
while (queue.length) {
|
||||
const node = queue.shift() as TreeNode;
|
||||
if (++i >= arr.length) break;
|
||||
if (arr[i] !== null) {
|
||||
node.left = new TreeNode(arr[i] as number);
|
||||
queue.push(node.left);
|
||||
}
|
||||
if (++i >= arr.length) break;
|
||||
if (arr[i] !== null) {
|
||||
node.right = new TreeNode(arr[i] as number);
|
||||
queue.push(node.right);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
export {
|
||||
TreeNode,
|
||||
arrToTree
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* File: Vertex.ts
|
||||
* Created Time: 2023-02-15
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
|
||||
/* 顶点类 */
|
||||
class Vertex {
|
||||
val: number;
|
||||
constructor(val: number) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
/* 输入值列表 vals ,返回顶点列表 vets */
|
||||
public static valsToVets(vals: number[]): Vertex[] {
|
||||
const vets: Vertex[] = [];
|
||||
for (let i = 0; i < vals.length; i++) {
|
||||
vets[i] = new Vertex(vals[i]);
|
||||
}
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* 输入顶点列表 vets ,返回值列表 vals */
|
||||
public static vetsToVals(vets: Vertex[]): number[] {
|
||||
const vals: number[] = [];
|
||||
for (const vet of vets) {
|
||||
vals.push(vet.val);
|
||||
}
|
||||
return vals;
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
Vertex
|
||||
};
|
||||
Reference in New Issue
Block a user