Add build script for JS and TS codes.

This commit is contained in:
krahets
2023-02-08 19:45:06 +08:00
parent 22b7d65d20
commit 05f0054005
28 changed files with 227 additions and 2217 deletions

View File

@@ -329,25 +329,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "JavaScript"
```javascript title="avl_tree.js"
/* 获取平衡因子 */
balanceFactor(node) {
// 空结点平衡因子为 0
if (node === null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return this.height(node.left) - this.height(node.right);
}
[class]{AVLTree}-[func]{balanceFactor}
```
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 获取平衡因子 */
balanceFactor(node: TreeNode): number {
// 空结点平衡因子为 0
if (node === null) return 0;
// 结点平衡因子 = 左子树高度 - 右子树高度
return this.height(node.left) - this.height(node.right);
}
[class]{AVLTree}-[func]{balanceFactor}
```
=== "C"
@@ -460,37 +448,13 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "JavaScript"
```javascript title="avl_tree.js"
/* 右旋操作 */
rightRotate(node) {
const child = node.left;
const grandChild = child.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
[class]{AVLTree}-[func]{rightRotate}
```
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 右旋操作 */
rightRotate(node: TreeNode): TreeNode {
const child = node.left;
const grandChild = child.right;
// 以 child 为原点,将 node 向右旋转
child.right = node;
node.left = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
[class]{AVLTree}-[func]{rightRotate}
```
=== "C"
@@ -593,37 +557,13 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "JavaScript"
```javascript title="avl_tree.js"
/* 左旋操作 */
leftRotate(node) {
const child = node.right;
const grandChild = child.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
[class]{AVLTree}-[func]{leftRotate}
```
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 左旋操作 */
leftRotate(node: TreeNode): TreeNode {
const child = node.right;
const grandChild = child.left;
// 以 child 为原点,将 node 向左旋转
child.left = node;
node.right = grandChild;
// 更新结点高度
this.updateHeight(node);
this.updateHeight(child);
// 返回旋转后子树的根结点
return child;
}
[class]{AVLTree}-[func]{leftRotate}
```
=== "C"
@@ -767,69 +707,13 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "JavaScript"
```javascript title="avl_tree.js"
/* 执行旋转操作,使该子树重新恢复平衡 */
rotate(node) {
// 获取结点 node 的平衡因子
const balanceFactor = this.balanceFactor(node);
// 左偏树
if (balanceFactor > 1) {
if (this.balanceFactor(node.left) >= 0) {
// 右旋
return this.rightRotate(node);
} else {
// 先左旋后右旋
node.left = this.leftRotate(node.left);
return this.rightRotate(node);
}
}
// 右偏树
if (balanceFactor < -1) {
if (this.balanceFactor(node.right) <= 0) {
// 左旋
return this.leftRotate(node);
} else {
// 先右旋后左旋
node.right = this.rightRotate(node.right);
return this.leftRotate(node);
}
}
// 平衡树,无需旋转,直接返回
return node;
}
[class]{AVLTree}-[func]{rotate}
```
=== "TypeScript"
```typescript title="avl_tree.ts"
/* 执行旋转操作,使该子树重新恢复平衡 */
rotate(node: TreeNode): TreeNode {
// 获取结点 node 的平衡因子
const balanceFactor = this.balanceFactor(node);
// 左偏树
if (balanceFactor > 1) {
if (this.balanceFactor(node.left) >= 0) {
// 右旋
return this.rightRotate(node);
} else {
// 先左旋后右旋
node.left = this.leftRotate(node.left);
return this.rightRotate(node);
}
}
// 右偏树
if (balanceFactor < -1) {
if (this.balanceFactor(node.right) <= 0) {
// 左旋
return this.leftRotate(node);
} else {
// 先右旋后左旋
node.right = this.rightRotate(node.right);
return this.leftRotate(node);
}
}
// 平衡树,无需旋转,直接返回
return node;
}
[class]{AVLTree}-[func]{rotate}
```
=== "C"

View File

@@ -80,42 +80,13 @@ comments: true
=== "JavaScript"
```javascript title="binary_search_tree.js"
/* 查找结点 */
function search(num) {
let cur = root;
// 循环查找,越过叶结点后跳出
while (cur !== null) {
// 目标结点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 目标结点在 cur 的左子树中
else if (cur.val > num) cur = cur.left;
// 找到目标结点,跳出循环
else break;
}
// 返回目标结点
return cur;
}
[class]{}-[func]{search}
```
=== "TypeScript"
```typescript title="binary_search_tree.ts"
/* 查找结点 */
function search(num: number): TreeNode | null {
let cur = root;
// 循环查找,越过叶结点后跳出
while (cur !== null) {
if (cur.val < num) {
cur = cur.right; // 目标结点在 cur 的右子树中
} else if (cur.val > num) {
cur = cur.left; // 目标结点在 cur 的左子树中
} else {
break; // 找到目标结点,跳出循环
}
}
// 返回目标结点
return cur;
}
[class]{}-[func]{search}
```
=== "C"
@@ -245,61 +216,13 @@ comments: true
=== "JavaScript"
```javascript title="binary_search_tree.js"
/* 插入结点 */
function insert(num) {
// 若树为空,直接提前返回
if (root === null) return null;
let cur = root, pre = null;
// 循环查找,越过叶结点后跳出
while (cur !== null) {
// 找到重复结点,直接返回
if (cur.val === num) return null;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 插入位置在 cur 的左子树中
else cur = cur.left;
}
// 插入结点 val
let node = new Tree.TreeNode(num);
if (pre.val < num) pre.right = node;
else pre.left = node;
return node;
}
[class]{}-[func]{insert}
```
=== "TypeScript"
```typescript title="binary_search_tree.ts"
/* 插入结点 */
function insert(num: number): TreeNode | null {
// 若树为空,直接提前返回
if (root === null) {
return null;
}
let cur = root,
pre: TreeNode | null = null;
// 循环查找,越过叶结点后跳出
while (cur !== null) {
if (cur.val === num) {
return null; // 找到重复结点,直接返回
}
pre = cur;
if (cur.val < num) {
cur = cur.right as TreeNode; // 插入位置在 cur 的右子树中
} else {
cur = cur.left as TreeNode; // 插入位置在 cur 的左子树中
}
}
// 插入结点 val
let node = new TreeNode(num);
if (pre!.val < num) {
pre!.right = node;
} else {
pre!.left = node;
}
return node;
}
[class]{}-[func]{insert}
```
=== "C"
@@ -518,118 +441,17 @@ comments: true
=== "JavaScript"
```javascript title="binary_search_tree.js"
/* 删除结点 */
function remove(num) {
// 若树为空,直接提前返回
if (root === null) return null;
let cur = root, pre = null;
// 循环查找,越过叶结点后跳出
while (cur !== null) {
// 找到待删除结点,跳出循环
if (cur.val === num) break;
pre = cur;
// 待删除结点在 cur 的右子树中
if (cur.val < num) cur = cur.right;
// 待删除结点在 cur 的左子树中
else cur = cur.left;
}
// 若无待删除结点,则直接返回
if (cur === null) return null;
// 子结点数量 = 0 or 1
if (cur.left === null || cur.right === null) {
// 当子结点数量 = 0 / 1 时, child = null / 该子结点
let child = cur.left !== null ? cur.left : cur.right;
// 删除结点 cur
if (pre.left === cur) pre.left = child;
else pre.right = child;
}
// 子结点数量 = 2
else {
// 获取中序遍历中 cur 的下一个结点
let nex = getInOrderNext(cur.right);
let tmp = nex.val;
// 递归删除结点 nex
remove(nex.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
}
return cur;
}
[class]{}-[func]{remove}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
function getInOrderNext(root) {
if (root === null) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root.left !== null) {
root = root.left;
}
return root;
}
[class]{}-[func]{getInOrderNext}
```
=== "TypeScript"
```typescript title="binary_search_tree.ts"
/* 删除结点 */
function remove(num: number): TreeNode | null {
// 若树为空,直接提前返回
if (root === null) {
return null;
}
let cur = root,
pre: TreeNode | null = null;
// 循环查找,越过叶结点后跳出
while (cur !== null) {
// 找到待删除结点,跳出循环
if (cur.val === num) {
break;
}
pre = cur;
if (cur.val < num) {
cur = cur.right as TreeNode; // 待删除结点在 cur 的右子树中
} else {
cur = cur.left as TreeNode; // 待删除结点在 cur 的左子树中
}
}
// 若无待删除结点,则直接返回
if (cur === null) {
return null;
}
// 子结点数量 = 0 or 1
if (cur.left === null || cur.right === null) {
// 当子结点数量 = 0 / 1 时, child = null / 该子结点
let child = cur.left !== null ? cur.left : cur.right;
// 删除结点 cur
if (pre!.left === cur) {
pre!.left = child;
} else {
pre!.right = child;
}
}
// 子结点数量 = 2
else {
// 获取中序遍历中 cur 的下一个结点
let next = getInOrderNext(cur.right);
let tmp = next!.val;
// 递归删除结点 nex
remove(next!.val);
// 将 nex 的值复制给 cur
cur.val = tmp;
}
return cur;
}
[class]{}-[func]{remove}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
function getInOrderNext(root: TreeNode | null): TreeNode | null {
if (root === null) {
return null;
}
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root.left !== null) {
root = root.left;
}
return root;
}
[class]{}-[func]{getInOrderNext}
```
=== "C"

View File

@@ -67,45 +67,13 @@ comments: true
=== "JavaScript"
```javascript title="binary_tree_bfs.js"
/* 层序遍历 */
function hierOrder(root) {
// 初始化队列,加入根结点
let queue = [root];
// 初始化一个列表,用于保存遍历序列
let list = [];
while (queue.length) {
let node = queue.shift(); // 队列出队
list.push(node.val); // 保存结点值
if (node.left)
queue.push(node.left); // 左子结点入队
if (node.right)
queue.push(node.right); // 右子结点入队
}
return list;
}
[class]{}-[func]{hierOrder}
```
=== "TypeScript"
```typescript title="binary_tree_bfs.ts"
/* 层序遍历 */
function hierOrder(root: TreeNode | null): number[] {
// 初始化队列,加入根结点
const queue = [root];
// 初始化一个列表,用于保存遍历序列
const list: number[] = [];
while (queue.length) {
let node = queue.shift() as TreeNode; // 队列出队
list.push(node.val); // 保存结点值
if (node.left) {
queue.push(node.left); // 左子结点入队
}
if (node.right) {
queue.push(node.right); // 右子结点入队
}
}
return list;
}
[class]{}-[func]{hierOrder}
```
=== "C"
@@ -258,69 +226,21 @@ comments: true
=== "JavaScript"
```javascript title="binary_tree_dfs.js"
/* 前序遍历 */
function preOrder(root){
if (root === null) return;
// 访问优先级:根结点 -> 左子树 -> 右子树
list.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
function inOrder(root) {
if (root === null) return;
// 访问优先级:左子树 -> 根结点 -> 右子树
inOrder(root.left);
list.push(root.val);
inOrder(root.right);
}
/* 后序遍历 */
function postOrder(root) {
if (root === null) return;
// 访问优先级:左子树 -> 右子树 -> 根结点
postOrder(root.left);
postOrder(root.right);
list.push(root.val);
}
[class]{}-[func]{preOrder}
[class]{}-[func]{inOrder}
[class]{}-[func]{postOrder}
```
=== "TypeScript"
```typescript title="binary_tree_dfs.ts"
/* 前序遍历 */
function preOrder(root: TreeNode | null): void {
if (root === null) {
return;
}
// 访问优先级:根结点 -> 左子树 -> 右子树
list.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
function inOrder(root: TreeNode | null): void {
if (root === null) {
return;
}
// 访问优先级:左子树 -> 根结点 -> 右子树
inOrder(root.left);
list.push(root.val);
inOrder(root.right);
}
/* 后序遍历 */
function postOrder(root: TreeNode | null): void {
if (root === null) {
return;
}
// 访问优先级:左子树 -> 右子树 -> 根结点
postOrder(root.left);
postOrder(root.right);
list.push(root.val);
}
[class]{}-[func]{preOrder}
[class]{}-[func]{inOrder}
[class]{}-[func]{postOrder}
```
=== "C"