mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-03 19:03:42 +08: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:
@@ -4,8 +4,8 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com), Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { printLinkedList } = require("../include/PrintUtil");
|
||||
const { ListNode } = require("../include/ListNode");
|
||||
const { printLinkedList } = require("../modules/PrintUtil");
|
||||
const { ListNode } = require("../modules/ListNode");
|
||||
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
function insert(n0, P) {
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../include/ListNode');
|
||||
const { TreeNode } = require('../include/TreeNode');
|
||||
const { printTree } = require('../include/PrintUtil');
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
const { TreeNode } = require('../modules/TreeNode');
|
||||
const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* 函数 */
|
||||
function constFunc() {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { Vertex } = require('../include/Vertex')
|
||||
const { Vertex } = require('../modules/Vertex')
|
||||
|
||||
/* 基于邻接表实现的无向图类 */
|
||||
class GraphAdjList {
|
||||
|
||||
@@ -92,7 +92,7 @@ class GraphAdjMat {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化无向图 */
|
||||
// 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引
|
||||
const vertices = [1, 3, 2, 5, 4];
|
||||
|
||||
@@ -4,10 +4,8 @@
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
|
||||
const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
const { Vertex } = require('../include/Vertex');
|
||||
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* 广度优先遍历 BFS */
|
||||
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
|
||||
@@ -36,7 +34,6 @@ function graphBFS(graph, startVet) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化无向图 */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
|
||||
const { Vertex } = require('../include/Vertex');
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
|
||||
/* 深度优先遍历 DFS */
|
||||
@@ -34,7 +33,6 @@ function graphDFS(graph, startVet) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化无向图 */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6]);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
const { printHeap } = require("../include/PrintUtil");
|
||||
const { printHeap } = require("../modules/PrintUtil");
|
||||
|
||||
/* 最大堆类 */
|
||||
class MaxHeap {
|
||||
|
||||
@@ -10,7 +10,7 @@ function binarySearch(nums, target) {
|
||||
let i = 0, j = nums.length - 1;
|
||||
// 循环,当搜索区间为空时跳出(当 i > j 时为空)
|
||||
while (i <= j) {
|
||||
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j] 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m-1] 中
|
||||
@@ -28,7 +28,7 @@ function binarySearch1(nums, target) {
|
||||
let i = 0, j = nums.length;
|
||||
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
|
||||
while (i < j) {
|
||||
let m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||
const m = parseInt((i + j) / 2); // 计算中点索引 m ,在 JS 中需使用 parseInt 函数取整
|
||||
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
|
||||
i = m + 1;
|
||||
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
|
||||
@@ -41,11 +41,11 @@ function binarySearch1(nums, target) {
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
var target = 6;
|
||||
var nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92];
|
||||
const target = 6;
|
||||
const nums = [1, 3, 6, 8, 12, 15, 23, 67, 70, 92];
|
||||
|
||||
/* 二分查找(双闭区间) */
|
||||
var index = binarySearch(nums, target);
|
||||
let index = binarySearch(nums, target);
|
||||
console.log("目标元素 6 的索引 = " + index);
|
||||
|
||||
/* 二分查找(左闭右开) */
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
const { ListNode, arrToLinkedList } = require("../include/ListNode");
|
||||
const { arrToLinkedList } = require("../modules/ListNode");
|
||||
|
||||
/* 哈希查找(数组) */
|
||||
function hashingSearchArray(map, target) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: JoseHung (szhong@link.cuhk.edu.hk)
|
||||
*/
|
||||
|
||||
const { ListNode, arrToLinkedList } = require("../include/ListNode");
|
||||
const { ListNode, arrToLinkedList } = require("../modules/ListNode");
|
||||
|
||||
/* 线性查找(数组) */
|
||||
function linearSearchArray(nums, target) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* File: quick_sort.js
|
||||
* File: bubble_sort.js
|
||||
* Created Time: 2022-12-01
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* File: quick_sort.js
|
||||
* File: insertion_sort.js
|
||||
* Created Time: 2022-12-01
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* File: quick_sort.js
|
||||
* File: merge_sort.js
|
||||
* Created Time: 2022-12-01
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
@@ -44,6 +44,6 @@ function mergeSort(nums, left, right) {
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const nums = [ 7, 3, 2, 6, 0, 1, 5, 4 ]
|
||||
mergeSort(nums, 0, nums.length - 1)
|
||||
console.log('归并排序完成后 nums =', nums)
|
||||
const nums = [7, 3, 2, 6, 0, 1, 5, 4];
|
||||
mergeSort(nums, 0, nums.length - 1);
|
||||
console.log('归并排序完成后 nums =', nums);
|
||||
|
||||
@@ -7,49 +7,48 @@
|
||||
|
||||
/* 基于数组实现的栈 */
|
||||
class ArrayStack {
|
||||
stack;
|
||||
#stack;
|
||||
constructor() {
|
||||
this.stack = [];
|
||||
this.#stack = [];
|
||||
}
|
||||
|
||||
/* 获取栈的长度 */
|
||||
get size() {
|
||||
return this.stack.length;
|
||||
return this.#stack.length;
|
||||
}
|
||||
|
||||
/* 判断栈是否为空 */
|
||||
empty() {
|
||||
return this.stack.length === 0;
|
||||
return this.#stack.length === 0;
|
||||
}
|
||||
|
||||
/* 入栈 */
|
||||
push(num) {
|
||||
this.stack.push(num);
|
||||
this.#stack.push(num);
|
||||
}
|
||||
|
||||
/* 出栈 */
|
||||
pop() {
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack.pop();
|
||||
return this.#stack.pop();
|
||||
}
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
top() {
|
||||
if (this.empty())
|
||||
throw new Error("栈为空");
|
||||
return this.stack[this.stack.length - 1];
|
||||
return this.#stack[this.#stack.length - 1];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
return this.#stack;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
|
||||
/* 初始化栈 */
|
||||
const stack = new ArrayStack();
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化双向队列 */
|
||||
// JavaScript 没有内置的双端队列,只能把 Array 当作双端队列来使用
|
||||
const deque = [];
|
||||
|
||||
@@ -19,106 +19,106 @@ class ListNode {
|
||||
|
||||
/* 基于双向链表实现的双向队列 */
|
||||
class LinkedListDeque {
|
||||
front; // 头结点 front
|
||||
rear; // 尾结点 rear
|
||||
len; // 双向队列的长度
|
||||
#front; // 头结点 front
|
||||
#rear; // 尾结点 rear
|
||||
#queSize; // 双向队列的长度
|
||||
|
||||
constructor() {
|
||||
this.front = null;
|
||||
this.rear = null;
|
||||
this.len = 0;
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 队尾入队操作 */
|
||||
pushLast(val) {
|
||||
const node = new ListNode(val);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (this.len === 0) {
|
||||
this.front = node;
|
||||
this.rear = node;
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// 将 node 添加至链表尾部
|
||||
this.rear.next = node;
|
||||
node.prev = this.rear;
|
||||
this.rear = node; // 更新尾结点
|
||||
this.#rear.next = node;
|
||||
node.prev = this.#rear;
|
||||
this.#rear = node; // 更新尾结点
|
||||
}
|
||||
this.len++;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 队首入队操作 */
|
||||
pushFirst(val) {
|
||||
const node = new ListNode(val);
|
||||
// 若链表为空,则令 front, rear 都指向 node
|
||||
if (this.len === 0) {
|
||||
this.front = node;
|
||||
this.rear = node;
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// 将 node 添加至链表头部
|
||||
this.front.prev = node;
|
||||
node.next = this.front;
|
||||
this.front = node; // 更新头结点
|
||||
this.#front.prev = node;
|
||||
node.next = this.#front;
|
||||
this.#front = node; // 更新头结点
|
||||
}
|
||||
this.len++;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 队尾出队操作 */
|
||||
pollLast() {
|
||||
if (this.len === 0) {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.rear.val; // 存储尾结点值
|
||||
const value = this.#rear.val; // 存储尾结点值
|
||||
// 删除尾结点
|
||||
let temp = this.rear.prev;
|
||||
let temp = this.#rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.rear.prev = null;
|
||||
this.#rear.prev = null;
|
||||
}
|
||||
this.rear = temp; // 更新尾结点
|
||||
this.len--;
|
||||
this.#rear = temp; // 更新尾结点
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 队首出队操作 */
|
||||
pollFirst() {
|
||||
if (this.len === 0) {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.front.val; // 存储尾结点值
|
||||
const value = this.#front.val; // 存储尾结点值
|
||||
// 删除头结点
|
||||
let temp = this.front.next;
|
||||
let temp = this.#front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.front.next = null;
|
||||
this.#front.next = null;
|
||||
}
|
||||
this.front = temp; // 更新头结点
|
||||
this.len--;
|
||||
this.#front = temp; // 更新头结点
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* 访问队尾元素 */
|
||||
peekLast() {
|
||||
return this.len === 0 ? null : this.rear.val;
|
||||
return this.#queSize === 0 ? null : this.#rear.val;
|
||||
}
|
||||
|
||||
/* 访问队首元素 */
|
||||
peekFirst() {
|
||||
return this.len === 0 ? null : this.front.val;
|
||||
return this.#queSize === 0 ? null : this.#front.val;
|
||||
}
|
||||
|
||||
/* 获取双向队列的长度 */
|
||||
size() {
|
||||
return this.len;
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 判断双向队列是否为空 */
|
||||
isEmpty() {
|
||||
return this.len === 0;
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 打印双向队列 */
|
||||
print() {
|
||||
const arr = [];
|
||||
let temp = this.front;
|
||||
let temp = this.#front;
|
||||
while (temp !== null) {
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
@@ -127,6 +127,7 @@ class LinkedListDeque {
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化双向队列 */
|
||||
const linkedListDeque = new LinkedListDeque();
|
||||
linkedListDeque.pushLast(3);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require("../include/ListNode");
|
||||
const { ListNode } = require("../modules/ListNode");
|
||||
|
||||
/* 基于链表实现的队列 */
|
||||
class LinkedListQueue {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require("../include/ListNode");
|
||||
const { ListNode } = require("../modules/ListNode");
|
||||
|
||||
/* 基于链表实现的栈 */
|
||||
class LinkedListStack {
|
||||
@@ -60,7 +60,7 @@ class LinkedListStack {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化栈 */
|
||||
const stack = new LinkedListStack();
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化队列 */
|
||||
// JavaScript 没有内置的队列,可以把 Array 当作队列来使用
|
||||
@@ -16,16 +15,21 @@ queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log("队列 queue =", queue);
|
||||
|
||||
/* 访问队首元素 */
|
||||
const peek = queue[0];
|
||||
console.log("队首元素 peek =", peek);
|
||||
|
||||
/* 元素出队 */
|
||||
// 底层是数组,因此 shift() 方法的时间复杂度为 O(n)
|
||||
const poll = queue.shift();
|
||||
console.log("出队元素 poll =", poll, ",出队后 queue = ", queue);
|
||||
|
||||
/* 获取队列的长度 */
|
||||
const size = queue.length;
|
||||
console.log("队列长度 size =", size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
const empty = queue.length === 0;
|
||||
const isEmpty = queue.length === 0;
|
||||
console.log("队列是否为空 = ", isEmpty);
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化栈 */
|
||||
// Javascript 没有内置的栈类,可以把 Array 当作栈来使用
|
||||
@@ -16,21 +15,21 @@ stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log("栈 stack =", stack)
|
||||
console.log("栈 stack =", stack);
|
||||
|
||||
/* 访问栈顶元素 */
|
||||
const peek = stack[stack.length - 1];
|
||||
console.log("栈顶元素 peek =", peek)
|
||||
console.log("栈顶元素 peek =", peek);
|
||||
|
||||
/* 元素出栈 */
|
||||
const pop = stack.pop();
|
||||
console.log("出栈元素 pop =", pop)
|
||||
console.log("出栈后 stack =", stack)
|
||||
console.log("出栈元素 pop =", pop);
|
||||
console.log("出栈后 stack =", stack);
|
||||
|
||||
/* 获取栈的长度 */
|
||||
const size = stack.length;
|
||||
console.log("栈的长度 size =", size)
|
||||
console.log("栈的长度 size =", size);
|
||||
|
||||
/* 判断是否为空 */
|
||||
const is_empty = stack.length === 0;
|
||||
console.log("栈是否为空 =", is_empty)
|
||||
const isEmpty = stack.length === 0;
|
||||
console.log("栈是否为空 =", isEmpty);
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
* Author: what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
const { TreeNode } = require("../include/TreeNode");
|
||||
const { printTree } = require("../include/PrintUtil");
|
||||
const { TreeNode } = require("../modules/TreeNode");
|
||||
const { printTree } = require("../modules/PrintUtil");
|
||||
|
||||
/* AVL 树*/
|
||||
class AVLTree {
|
||||
@@ -21,7 +21,7 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 更新结点高度 */
|
||||
updateHeight(node) {
|
||||
#updateHeight(node) {
|
||||
// 结点高度等于最高子树高度 + 1
|
||||
node.height = Math.max(this.height(node.left), this.height(node.right)) + 1;
|
||||
}
|
||||
@@ -35,57 +35,57 @@ class AVLTree {
|
||||
}
|
||||
|
||||
/* 右旋操作 */
|
||||
rightRotate(node) {
|
||||
#rightRotate(node) {
|
||||
const child = node.left;
|
||||
const grandChild = child.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
|
||||
/* 左旋操作 */
|
||||
leftRotate(node) {
|
||||
#leftRotate(node) {
|
||||
const child = node.right;
|
||||
const grandChild = child.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
this.#updateHeight(node);
|
||||
this.#updateHeight(child);
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
rotate(node) {
|
||||
#rotate(node) {
|
||||
// 获取结点 node 的平衡因子
|
||||
const balanceFactor = this.balanceFactor(node);
|
||||
// 左偏树
|
||||
if (balanceFactor > 1) {
|
||||
if (this.balanceFactor(node.left) >= 0) {
|
||||
// 右旋
|
||||
return this.rightRotate(node);
|
||||
return this.#rightRotate(node);
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.left = this.leftRotate(node.left);
|
||||
return this.rightRotate(node);
|
||||
node.left = this.#leftRotate(node.left);
|
||||
return this.#rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (balanceFactor < -1) {
|
||||
if (this.balanceFactor(node.right) <= 0) {
|
||||
// 左旋
|
||||
return this.leftRotate(node);
|
||||
return this.#leftRotate(node);
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.right = this.rightRotate(node.right);
|
||||
return this.leftRotate(node);
|
||||
node.right = this.#rightRotate(node.right);
|
||||
return this.#leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
@@ -94,36 +94,36 @@ class AVLTree {
|
||||
|
||||
/* 插入结点 */
|
||||
insert(val) {
|
||||
this.root = this.insertHelper(this.root, val);
|
||||
this.root = this.#insertHelper(this.root, val);
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 递归插入结点(辅助方法) */
|
||||
insertHelper(node, val) {
|
||||
#insertHelper(node, val) {
|
||||
if (node === null) return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入结点 */
|
||||
if (val < node.val) node.left = this.insertHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.insertHelper(node.right, val);
|
||||
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; // 重复结点不插入,直接返回
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
this.#updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
node = this.#rotate(node);
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 删除结点 */
|
||||
remove(val) {
|
||||
this.root = this.removeHelper(this.root, val);
|
||||
this.root = this.#removeHelper(this.root, val);
|
||||
return this.root;
|
||||
}
|
||||
|
||||
/* 递归删除结点(辅助方法) */
|
||||
removeHelper(node, val) {
|
||||
#removeHelper(node, val) {
|
||||
if (node === null) return null;
|
||||
/* 1. 查找结点,并删除之 */
|
||||
if (val < node.val) node.left = this.removeHelper(node.left, val);
|
||||
else if (val > node.val) node.right = this.removeHelper(node.right, val);
|
||||
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;
|
||||
@@ -133,20 +133,20 @@ class AVLTree {
|
||||
else node = child;
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
const temp = this.getInOrderNext(node.right);
|
||||
node.right = this.removeHelper(node.right, temp.val);
|
||||
const temp = this.#getInOrderNext(node.right);
|
||||
node.right = this.#removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
this.#updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
node = this.#rotate(node);
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
getInOrderNext(node) {
|
||||
#getInOrderNext(node) {
|
||||
if (node === null) return node;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while (node.left !== null) {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
/**
|
||||
* File: binary_tree.js
|
||||
* File: binary_search_tree.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const Tree = require("../include/TreeNode");
|
||||
const { printTree } = require("../include/PrintUtil");
|
||||
const { TreeNode } = require("../modules/TreeNode");
|
||||
const { printTree } = require("../modules/PrintUtil");
|
||||
|
||||
/* 二叉搜索树 */
|
||||
var root;
|
||||
let root;
|
||||
|
||||
function BinarySearchTree(nums) {
|
||||
nums.sort((a, b) => { return a - b }); // 排序数组
|
||||
@@ -25,7 +25,7 @@ function buildTree(nums, i, j) {
|
||||
if (i > j) return null;
|
||||
// 将数组中间结点作为根结点
|
||||
let mid = Math.floor((i + j) / 2);
|
||||
let root = new Tree.TreeNode(nums[mid]);
|
||||
let root = new TreeNode(nums[mid]);
|
||||
// 递归建立左子树和右子树
|
||||
root.left = buildTree(nums, i, mid - 1);
|
||||
root.right = buildTree(nums, mid + 1, j);
|
||||
@@ -64,7 +64,7 @@ function insert(num) {
|
||||
else cur = cur.left;
|
||||
}
|
||||
// 插入结点 val
|
||||
let node = new Tree.TreeNode(num);
|
||||
let node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
return node;
|
||||
@@ -120,7 +120,7 @@ function getInOrderNext(root) {
|
||||
|
||||
/* Driver Code */
|
||||
/* 初始化二叉搜索树 */
|
||||
var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
||||
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
||||
BinarySearchTree(nums);
|
||||
console.log("\n初始化的二叉树为\n");
|
||||
printTree(getRoot());
|
||||
|
||||
@@ -4,16 +4,16 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const Tree = require("../include/TreeNode");
|
||||
const { printTree } = require("../include/PrintUtil");
|
||||
const { TreeNode } = require("../modules/TreeNode");
|
||||
const { printTree } = require("../modules/PrintUtil");
|
||||
|
||||
/* 初始化二叉树 */
|
||||
// 初始化结点
|
||||
let n1 = new Tree.TreeNode(1),
|
||||
n2 = new Tree.TreeNode(2),
|
||||
n3 = new Tree.TreeNode(3),
|
||||
n4 = new Tree.TreeNode(4),
|
||||
n5 = new Tree.TreeNode(5);
|
||||
let n1 = new TreeNode(1),
|
||||
n2 = new TreeNode(2),
|
||||
n3 = new TreeNode(3),
|
||||
n4 = new TreeNode(4),
|
||||
n5 = new TreeNode(5);
|
||||
// 构建引用指向(即指针)
|
||||
n1.left = n2;
|
||||
n1.right = n3;
|
||||
@@ -23,7 +23,7 @@ console.log("\n初始化二叉树\n");
|
||||
printTree(n1);
|
||||
|
||||
/* 插入与删除结点 */
|
||||
let P = new Tree.TreeNode(0);
|
||||
const P = new TreeNode(0);
|
||||
// 在 n1 -> n2 中间插入结点 P
|
||||
n1.left = P;
|
||||
P.left = n2;
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
/**
|
||||
* File: binary_tree.js
|
||||
* File: binary_tree_bfs.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require("../include/TreeNode");
|
||||
const { printTree } = require("../include/PrintUtil");
|
||||
const { arrToTree } = require("../modules/TreeNode");
|
||||
const { printTree } = require("../modules/PrintUtil");
|
||||
|
||||
/* 层序遍历 */
|
||||
function levelOrder(root) {
|
||||
// 初始化队列,加入根结点
|
||||
let queue = [root];
|
||||
const queue = [root];
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
let list = [];
|
||||
const list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // 队列出队
|
||||
list.push(node.val); // 保存结点值
|
||||
@@ -28,10 +28,10 @@ function levelOrder(root) {
|
||||
/* Driver Code */
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
var root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
/* 层序遍历 */
|
||||
let list = levelOrder(root);
|
||||
const list = levelOrder(root);
|
||||
console.log("\n层序遍历的结点打印序列 = " + list);
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
/**
|
||||
* File: binary_tree.js
|
||||
* File: binary_tree_dfs.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
const { arrToTree } = require("../include/TreeNode");
|
||||
const { printTree } = require("../include/PrintUtil");
|
||||
const { arrToTree } = require("../modules/TreeNode");
|
||||
const { printTree } = require("../modules/PrintUtil");
|
||||
|
||||
// 初始化列表,用于存储遍历序列
|
||||
var list = [];
|
||||
const list = [];
|
||||
|
||||
/* 前序遍历 */
|
||||
function preOrder(root) {
|
||||
@@ -40,7 +40,7 @@ function postOrder(root) {
|
||||
/* Driver Code */
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
var root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log("\n初始化二叉树\n");
|
||||
printTree(root);
|
||||
|
||||
|
||||
@@ -98,5 +98,5 @@ function printHeap(arr) {
|
||||
module.exports = {
|
||||
printLinkedList,
|
||||
printTree,
|
||||
printHeap,
|
||||
printHeap
|
||||
};
|
||||
@@ -50,5 +50,5 @@ function arrToTree(arr) {
|
||||
|
||||
module.exports = {
|
||||
TreeNode,
|
||||
arrToTree,
|
||||
arrToTree
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* File: Vertex.ts
|
||||
* File: Vertex.js
|
||||
* Created Time: 2023-02-15
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
Reference in New Issue
Block a user