bst-typed 1.48.9 → 1.49.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/data-structures/binary-tree/avl-tree.d.ts +16 -11
- package/dist/data-structures/binary-tree/avl-tree.js +12 -4
- package/dist/data-structures/binary-tree/binary-tree.d.ts +70 -61
- package/dist/data-structures/binary-tree/binary-tree.js +25 -21
- package/dist/data-structures/binary-tree/bst.d.ts +20 -13
- package/dist/data-structures/binary-tree/bst.js +12 -3
- package/dist/data-structures/binary-tree/rb-tree.d.ts +19 -25
- package/dist/data-structures/binary-tree/rb-tree.js +21 -35
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +20 -13
- package/dist/data-structures/binary-tree/tree-multimap.js +12 -3
- package/dist/data-structures/graph/abstract-graph.d.ts +9 -3
- package/dist/data-structures/graph/abstract-graph.js +27 -31
- package/dist/data-structures/graph/directed-graph.d.ts +8 -1
- package/dist/data-structures/graph/directed-graph.js +1 -8
- package/dist/data-structures/graph/map-graph.d.ts +1 -1
- package/dist/data-structures/graph/undirected-graph.d.ts +8 -1
- package/dist/data-structures/graph/undirected-graph.js +1 -8
- package/dist/data-structures/hash/hash-map.d.ts +14 -2
- package/dist/data-structures/hash/hash-map.js +19 -8
- package/dist/data-structures/hash/hash-table.d.ts +2 -2
- package/dist/data-structures/heap/heap.d.ts +14 -3
- package/dist/data-structures/heap/heap.js +12 -0
- package/dist/data-structures/heap/max-heap.d.ts +11 -1
- package/dist/data-structures/heap/max-heap.js +10 -7
- package/dist/data-structures/heap/min-heap.d.ts +11 -1
- package/dist/data-structures/heap/min-heap.js +10 -7
- package/dist/data-structures/linked-list/doubly-linked-list.d.ts +8 -2
- package/dist/data-structures/linked-list/doubly-linked-list.js +6 -7
- package/dist/data-structures/linked-list/singly-linked-list.d.ts +2 -2
- package/dist/data-structures/linked-list/singly-linked-list.js +0 -7
- package/dist/data-structures/priority-queue/max-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/max-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/min-priority-queue.d.ts +1 -1
- package/dist/data-structures/priority-queue/min-priority-queue.js +0 -7
- package/dist/data-structures/priority-queue/priority-queue.d.ts +9 -1
- package/dist/data-structures/priority-queue/priority-queue.js +8 -7
- package/dist/data-structures/queue/deque.d.ts +6 -5
- package/dist/data-structures/queue/deque.js +6 -12
- package/dist/data-structures/queue/queue.d.ts +18 -3
- package/dist/data-structures/queue/queue.js +16 -6
- package/dist/data-structures/stack/stack.d.ts +15 -5
- package/dist/data-structures/stack/stack.js +7 -4
- package/dist/data-structures/trie/trie.d.ts +13 -3
- package/dist/data-structures/trie/trie.js +11 -8
- package/dist/interfaces/binary-tree.d.ts +4 -4
- package/dist/types/common.d.ts +32 -8
- package/dist/types/common.js +22 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +1 -24
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -22
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +1 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +1 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +23 -13
- package/src/data-structures/binary-tree/binary-tree.ts +93 -97
- package/src/data-structures/binary-tree/bst.ts +26 -15
- package/src/data-structures/binary-tree/rb-tree.ts +33 -48
- package/src/data-structures/binary-tree/tree-multimap.ts +32 -14
- package/src/data-structures/graph/abstract-graph.ts +35 -25
- package/src/data-structures/graph/directed-graph.ts +2 -2
- package/src/data-structures/graph/map-graph.ts +1 -1
- package/src/data-structures/graph/undirected-graph.ts +2 -2
- package/src/data-structures/hash/hash-map.ts +20 -3
- package/src/data-structures/hash/hash-table.ts +3 -3
- package/src/data-structures/heap/heap.ts +14 -3
- package/src/data-structures/heap/max-heap.ts +11 -2
- package/src/data-structures/heap/min-heap.ts +11 -2
- package/src/data-structures/linked-list/doubly-linked-list.ts +9 -3
- package/src/data-structures/linked-list/singly-linked-list.ts +3 -3
- package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
- package/src/data-structures/priority-queue/priority-queue.ts +9 -2
- package/src/data-structures/queue/deque.ts +7 -9
- package/src/data-structures/queue/queue.ts +18 -3
- package/src/data-structures/stack/stack.ts +16 -6
- package/src/data-structures/trie/trie.ts +13 -4
- package/src/interfaces/binary-tree.ts +5 -5
- package/src/types/common.ts +37 -12
- package/src/types/data-structures/binary-tree/avl-tree.ts +0 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +1 -26
- package/src/types/data-structures/binary-tree/bst.ts +0 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +1 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +1 -1
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.LinkedListQueue = exports.Queue = void 0;
|
|
4
|
+
const base_1 = require("../base");
|
|
5
|
+
const linked_list_1 = require("../linked-list");
|
|
4
6
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
7
|
+
* 1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
|
|
8
|
+
* 2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
|
|
9
|
+
* 3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
|
|
10
|
+
* 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
|
|
11
|
+
* 5. Data Buffering: Acting as a buffer for data packets in network communication.
|
|
12
|
+
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
|
|
13
|
+
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
|
8
14
|
*/
|
|
9
|
-
const linked_list_1 = require("../linked-list");
|
|
10
|
-
const base_1 = require("../base");
|
|
11
15
|
class Queue extends base_1.IterableElementBase {
|
|
12
16
|
/**
|
|
13
17
|
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
|
@@ -157,7 +161,7 @@ class Queue extends base_1.IterableElementBase {
|
|
|
157
161
|
* @param {E} value - The value parameter represents the value that you want to add to the queue.
|
|
158
162
|
*/
|
|
159
163
|
enqueue(value) {
|
|
160
|
-
this.push(value);
|
|
164
|
+
return this.push(value);
|
|
161
165
|
}
|
|
162
166
|
/**
|
|
163
167
|
* Time Complexity: O(n) - same as shift().
|
|
@@ -307,6 +311,12 @@ class Queue extends base_1.IterableElementBase {
|
|
|
307
311
|
}
|
|
308
312
|
}
|
|
309
313
|
exports.Queue = Queue;
|
|
314
|
+
/**
|
|
315
|
+
* 1. First In, First Out (FIFO) Strategy: Like other queue implementations, LinkedListQueue follows the first in, first out principle, meaning the element that is added to the queue first will be the first to be removed.
|
|
316
|
+
* 2. Based on Linked List: LinkedListQueue uses a linked list to store elements. Each node in the linked list contains data and a pointer to the next node.
|
|
317
|
+
* 3. Memory Usage: Since each element requires additional space to store a pointer to the next element, linked lists may use more memory compared to arrays.
|
|
318
|
+
* 4. Frequent Enqueuing and Dequeuing Operations: If your application involves frequent enqueuing and dequeuing operations and is less concerned with random access, then LinkedListQueue is a good choice.
|
|
319
|
+
*/
|
|
310
320
|
class LinkedListQueue extends linked_list_1.SinglyLinkedList {
|
|
311
321
|
/**
|
|
312
322
|
* The enqueue function adds a value to the end of an array.
|
|
@@ -1,9 +1,19 @@
|
|
|
1
|
-
import { IterableElementBase } from "../base";
|
|
2
|
-
import { ElementCallback } from "../../types";
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { ElementCallback } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
|
+
/**
|
|
11
|
+
* 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
|
|
12
|
+
* 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
|
|
13
|
+
* 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
|
|
14
|
+
* 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
15
|
+
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
16
|
+
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
7
17
|
*/
|
|
8
18
|
export declare class Stack<E = any> extends IterableElementBase<E> {
|
|
9
19
|
/**
|
|
@@ -3,9 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Stack = void 0;
|
|
4
4
|
const base_1 = require("../base");
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* 1. Last In, First Out (LIFO): The core characteristic of a stack is its last in, first out nature, meaning the last element added to the stack will be the first to be removed.
|
|
7
|
+
* 2. Uses: Stacks are commonly used for managing a series of tasks or elements that need to be processed in a last in, first out manner. They are widely used in various scenarios, such as in function calls in programming languages, evaluation of arithmetic expressions, and backtracking algorithms.
|
|
8
|
+
* 3. Performance: Stack operations are typically O(1) in time complexity, meaning that regardless of the stack's size, adding, removing, and viewing the top element are very fast operations.
|
|
9
|
+
* 4. Function Calls: In most modern programming languages, the records of function calls are managed through a stack. When a function is called, its record (including parameters, local variables, and return address) is 'pushed' into the stack. When the function returns, its record is 'popped' from the stack.
|
|
10
|
+
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
|
11
|
+
* 6. Backtracking Algorithms: In problems where multiple branches need to be explored but only one branch can be explored at a time, stacks can be used to save the state at each branching point.
|
|
9
12
|
*/
|
|
10
13
|
class Stack extends base_1.IterableElementBase {
|
|
11
14
|
/**
|
|
@@ -103,7 +106,7 @@ class Stack extends base_1.IterableElementBase {
|
|
|
103
106
|
pop() {
|
|
104
107
|
if (this.isEmpty())
|
|
105
108
|
return undefined;
|
|
106
|
-
return this.elements.pop()
|
|
109
|
+
return this.elements.pop();
|
|
107
110
|
}
|
|
108
111
|
/**
|
|
109
112
|
* Time Complexity: O(n)
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import type { ElementCallback } from '../../types';
|
|
9
|
+
import { IterableElementBase } from '../base';
|
|
10
10
|
/**
|
|
11
11
|
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
|
12
12
|
* and a flag indicating whether it's the end of a word.
|
|
@@ -18,7 +18,17 @@ export declare class TrieNode {
|
|
|
18
18
|
constructor(key: string);
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
|
-
* Trie represents a
|
|
21
|
+
* 1. Node Structure: Each node in a Trie represents a string (or a part of a string). The root node typically represents an empty string.
|
|
22
|
+
* 2. Child Node Relationship: Each node's children represent the strings that can be formed by adding one character to the string at the current node. For example, if a node represents the string 'ca', one of its children might represent 'cat'.
|
|
23
|
+
* 3. Fast Retrieval: Trie allows retrieval in O(m) time complexity, where m is the length of the string to be searched.
|
|
24
|
+
* 4. Space Efficiency: Trie can store a large number of strings very space-efficiently, especially when these strings share common prefixes.
|
|
25
|
+
* 5. Autocomplete and Prediction: Trie can be used for implementing autocomplete and word prediction features, as it can quickly find all strings with a common prefix.
|
|
26
|
+
* 6. Sorting: Trie can be used to sort a set of strings in alphabetical order.
|
|
27
|
+
* 7. String Retrieval: For example, searching for a specific string in a large set of strings.
|
|
28
|
+
* 8. Autocomplete: Providing recommended words or phrases as a user types.
|
|
29
|
+
* 9. Spell Check: Checking the spelling of words.
|
|
30
|
+
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
31
|
+
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data."
|
|
22
32
|
*/
|
|
23
33
|
export declare class Trie extends IterableElementBase<string> {
|
|
24
34
|
constructor(words?: string[], caseSensitive?: boolean);
|
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* data-structure-typed
|
|
4
|
-
*
|
|
5
|
-
* @author Tyler Zeng
|
|
6
|
-
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
7
|
-
* @license MIT License
|
|
8
|
-
*/
|
|
9
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
3
|
exports.Trie = exports.TrieNode = void 0;
|
|
11
4
|
const base_1 = require("../base");
|
|
@@ -22,7 +15,17 @@ class TrieNode {
|
|
|
22
15
|
}
|
|
23
16
|
exports.TrieNode = TrieNode;
|
|
24
17
|
/**
|
|
25
|
-
* Trie represents a
|
|
18
|
+
* 1. Node Structure: Each node in a Trie represents a string (or a part of a string). The root node typically represents an empty string.
|
|
19
|
+
* 2. Child Node Relationship: Each node's children represent the strings that can be formed by adding one character to the string at the current node. For example, if a node represents the string 'ca', one of its children might represent 'cat'.
|
|
20
|
+
* 3. Fast Retrieval: Trie allows retrieval in O(m) time complexity, where m is the length of the string to be searched.
|
|
21
|
+
* 4. Space Efficiency: Trie can store a large number of strings very space-efficiently, especially when these strings share common prefixes.
|
|
22
|
+
* 5. Autocomplete and Prediction: Trie can be used for implementing autocomplete and word prediction features, as it can quickly find all strings with a common prefix.
|
|
23
|
+
* 6. Sorting: Trie can be used to sort a set of strings in alphabetical order.
|
|
24
|
+
* 7. String Retrieval: For example, searching for a specific string in a large set of strings.
|
|
25
|
+
* 8. Autocomplete: Providing recommended words or phrases as a user types.
|
|
26
|
+
* 9. Spell Check: Checking the spelling of words.
|
|
27
|
+
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
|
28
|
+
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data."
|
|
26
29
|
*/
|
|
27
30
|
class Trie extends base_1.IterableElementBase {
|
|
28
31
|
constructor(words, caseSensitive = true) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions,
|
|
2
|
+
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BTNCallback, BTNExemplar } from '../types';
|
|
3
3
|
export interface IBinaryTree<K = number, V = any, N extends BinaryTreeNode<K, V, N> = BinaryTreeNodeNested<K, V>, TREE extends BinaryTree<K, V, N, TREE> = BinaryTreeNested<K, V, N>> {
|
|
4
4
|
createNode(key: K, value?: N['value']): N;
|
|
5
5
|
createTree(options?: Partial<BinaryTreeOptions<K>>): TREE;
|
|
6
|
-
add(keyOrNodeOrEntry:
|
|
7
|
-
addMany(nodes: Iterable<
|
|
8
|
-
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C):
|
|
6
|
+
add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V, count?: number): N | null | undefined;
|
|
7
|
+
addMany(nodes: Iterable<BTNExemplar<K, V, N>>, values?: Iterable<V | undefined>): (N | null | undefined)[];
|
|
8
|
+
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BinaryTreeDeleteResult<N>[];
|
|
9
9
|
}
|
package/dist/types/common.d.ts
CHANGED
|
@@ -1,15 +1,35 @@
|
|
|
1
|
-
export type Comparator<K> = (a: K, b: K) => number;
|
|
2
1
|
export declare enum BSTVariant {
|
|
3
2
|
MIN = "MIN",
|
|
4
3
|
MAX = "MAX"
|
|
5
4
|
}
|
|
6
|
-
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
7
|
-
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
8
5
|
export declare enum CP {
|
|
9
6
|
lt = "lt",
|
|
10
7
|
eq = "eq",
|
|
11
8
|
gt = "gt"
|
|
12
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Enum representing different loop types.
|
|
12
|
+
*
|
|
13
|
+
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
14
|
+
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
15
|
+
*/
|
|
16
|
+
export declare enum IterationType {
|
|
17
|
+
ITERATIVE = "ITERATIVE",
|
|
18
|
+
RECURSIVE = "RECURSIVE"
|
|
19
|
+
}
|
|
20
|
+
export declare enum FamilyPosition {
|
|
21
|
+
ROOT = "ROOT",
|
|
22
|
+
LEFT = "LEFT",
|
|
23
|
+
RIGHT = "RIGHT",
|
|
24
|
+
ROOT_LEFT = "ROOT_LEFT",
|
|
25
|
+
ROOT_RIGHT = "ROOT_RIGHT",
|
|
26
|
+
ISOLATED = "ISOLATED",
|
|
27
|
+
MAL_NODE = "MAL_NODE"
|
|
28
|
+
}
|
|
29
|
+
export type Comparator<K> = (a: K, b: K) => number;
|
|
30
|
+
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
|
31
|
+
export type NodeDisplayLayout = [string[], number, number, number];
|
|
32
|
+
export type BTNCallback<N, D = any> = (node: N) => D;
|
|
13
33
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
14
34
|
size: number | ((...args: any[]) => number);
|
|
15
35
|
}
|
|
@@ -22,9 +42,13 @@ export type BinaryTreePrintOptions = {
|
|
|
22
42
|
isShowNull?: boolean;
|
|
23
43
|
isShowRedBlackNIL?: boolean;
|
|
24
44
|
};
|
|
25
|
-
export type
|
|
26
|
-
export type
|
|
27
|
-
export type
|
|
28
|
-
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
|
45
|
+
export type BTNEntry<K, V> = [K | null | undefined, V | undefined];
|
|
46
|
+
export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|
47
|
+
export type BTNExemplar<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>;
|
|
29
48
|
export type BTNodePureKeyOrNode<K, N> = K | N;
|
|
30
|
-
export type
|
|
49
|
+
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
|
50
|
+
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
|
51
|
+
export type BinaryTreeDeleteResult<N> = {
|
|
52
|
+
deleted: N | null | undefined;
|
|
53
|
+
needBalanced: N | null | undefined;
|
|
54
|
+
};
|
package/dist/types/common.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.CP = exports.BSTVariant = void 0;
|
|
3
|
+
exports.FamilyPosition = exports.IterationType = exports.CP = exports.BSTVariant = void 0;
|
|
4
4
|
var BSTVariant;
|
|
5
5
|
(function (BSTVariant) {
|
|
6
6
|
BSTVariant["MIN"] = "MIN";
|
|
@@ -12,3 +12,24 @@ var CP;
|
|
|
12
12
|
CP["eq"] = "eq";
|
|
13
13
|
CP["gt"] = "gt";
|
|
14
14
|
})(CP = exports.CP || (exports.CP = {}));
|
|
15
|
+
/**
|
|
16
|
+
* Enum representing different loop types.
|
|
17
|
+
*
|
|
18
|
+
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
19
|
+
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
20
|
+
*/
|
|
21
|
+
var IterationType;
|
|
22
|
+
(function (IterationType) {
|
|
23
|
+
IterationType["ITERATIVE"] = "ITERATIVE";
|
|
24
|
+
IterationType["RECURSIVE"] = "RECURSIVE";
|
|
25
|
+
})(IterationType = exports.IterationType || (exports.IterationType = {}));
|
|
26
|
+
var FamilyPosition;
|
|
27
|
+
(function (FamilyPosition) {
|
|
28
|
+
FamilyPosition["ROOT"] = "ROOT";
|
|
29
|
+
FamilyPosition["LEFT"] = "LEFT";
|
|
30
|
+
FamilyPosition["RIGHT"] = "RIGHT";
|
|
31
|
+
FamilyPosition["ROOT_LEFT"] = "ROOT_LEFT";
|
|
32
|
+
FamilyPosition["ROOT_RIGHT"] = "ROOT_RIGHT";
|
|
33
|
+
FamilyPosition["ISOLATED"] = "ISOLATED";
|
|
34
|
+
FamilyPosition["MAL_NODE"] = "MAL_NODE";
|
|
35
|
+
})(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {}));
|
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
|
-
|
|
3
|
-
* Enum representing different loop types.
|
|
4
|
-
*
|
|
5
|
-
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
6
|
-
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
7
|
-
*/
|
|
8
|
-
export declare enum IterationType {
|
|
9
|
-
ITERATIVE = "ITERATIVE",
|
|
10
|
-
RECURSIVE = "RECURSIVE"
|
|
11
|
-
}
|
|
12
|
-
export declare enum FamilyPosition {
|
|
13
|
-
ROOT = "ROOT",
|
|
14
|
-
LEFT = "LEFT",
|
|
15
|
-
RIGHT = "RIGHT",
|
|
16
|
-
ROOT_LEFT = "ROOT_LEFT",
|
|
17
|
-
ROOT_RIGHT = "ROOT_RIGHT",
|
|
18
|
-
ISOLATED = "ISOLATED",
|
|
19
|
-
MAL_NODE = "MAL_NODE"
|
|
20
|
-
}
|
|
21
|
-
export type BiTreeDeleteResult<N> = {
|
|
22
|
-
deleted: N | null | undefined;
|
|
23
|
-
needBalanced: N | null | undefined;
|
|
24
|
-
};
|
|
2
|
+
import { IterationType } from "../../common";
|
|
25
3
|
export type BinaryTreeNodeNested<K, V> = BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, BinaryTreeNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
26
4
|
export type BinaryTreeNested<K, V, N extends BinaryTreeNode<K, V, N>> = BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, BinaryTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
27
5
|
export type BinaryTreeOptions<K> = {
|
|
28
6
|
iterationType: IterationType;
|
|
29
7
|
extractor: (key: K) => number;
|
|
30
8
|
};
|
|
31
|
-
export type NodeDisplayLayout = [string[], number, number, number];
|
|
@@ -1,24 +1,2 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FamilyPosition = exports.IterationType = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Enum representing different loop types.
|
|
6
|
-
*
|
|
7
|
-
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
8
|
-
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
9
|
-
*/
|
|
10
|
-
var IterationType;
|
|
11
|
-
(function (IterationType) {
|
|
12
|
-
IterationType["ITERATIVE"] = "ITERATIVE";
|
|
13
|
-
IterationType["RECURSIVE"] = "RECURSIVE";
|
|
14
|
-
})(IterationType = exports.IterationType || (exports.IterationType = {}));
|
|
15
|
-
var FamilyPosition;
|
|
16
|
-
(function (FamilyPosition) {
|
|
17
|
-
FamilyPosition["ROOT"] = "ROOT";
|
|
18
|
-
FamilyPosition["LEFT"] = "LEFT";
|
|
19
|
-
FamilyPosition["RIGHT"] = "RIGHT";
|
|
20
|
-
FamilyPosition["ROOT_LEFT"] = "ROOT_LEFT";
|
|
21
|
-
FamilyPosition["ROOT_RIGHT"] = "ROOT_RIGHT";
|
|
22
|
-
FamilyPosition["ISOLATED"] = "ISOLATED";
|
|
23
|
-
FamilyPosition["MAL_NODE"] = "MAL_NODE";
|
|
24
|
-
})(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {}));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TreeMultimap, TreeMultimapNode } from '../../../data-structures';
|
|
2
|
-
import { AVLTreeOptions } from './avl-tree';
|
|
2
|
+
import type { AVLTreeOptions } from './avl-tree';
|
|
3
3
|
export type TreeMultimapNodeNested<K, V> = TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, TreeMultimapNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
4
|
export type TreeMultimapNested<K, V, N extends TreeMultimapNode<K, V, N>> = TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, TreeMultimap<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
5
|
export type TreeMultimapOptions<K> = Omit<AVLTreeOptions<K>, 'isMergeDuplicatedNodeByKey'> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bst-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.49.1",
|
|
4
4
|
"description": "BST (Binary Search Tree). Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -144,6 +144,6 @@
|
|
|
144
144
|
"typescript": "^4.9.5"
|
|
145
145
|
},
|
|
146
146
|
"dependencies": {
|
|
147
|
-
"data-structure-typed": "^1.
|
|
147
|
+
"data-structure-typed": "^1.49.1"
|
|
148
148
|
}
|
|
149
149
|
}
|
|
@@ -10,11 +10,12 @@ import type {
|
|
|
10
10
|
AVLTreeNested,
|
|
11
11
|
AVLTreeNodeNested,
|
|
12
12
|
AVLTreeOptions,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
BinaryTreeDeleteResult,
|
|
14
|
+
BSTNKeyOrNode,
|
|
15
|
+
BTNCallback,
|
|
16
|
+
BTNExemplar,
|
|
17
|
+
BTNKeyOrNode
|
|
16
18
|
} from '../../types';
|
|
17
|
-
import { BTNCallback } from '../../types';
|
|
18
19
|
import { IBinaryTree } from '../../interfaces';
|
|
19
20
|
|
|
20
21
|
export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNodeNested<K, V>> extends BSTNode<K, V, N> {
|
|
@@ -34,7 +35,6 @@ export class AVLTreeNode<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLT
|
|
|
34
35
|
* 5. Efficient Lookups: Offers O(log n) search time, where 'n' is the number of nodes, due to its balanced nature.
|
|
35
36
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
36
37
|
* 7. Path Length: The path length from the root to any leaf is longer compared to an unbalanced BST, but shorter than a linear chain of nodes.
|
|
37
|
-
* 8. Memory Overhead: Stores balance factors (or heights) at each node, leading to slightly higher memory usage compared to a regular BST.
|
|
38
38
|
*/
|
|
39
39
|
export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, N, TREE> = AVLTree<K, V, N, AVLTreeNested<K, V, N>>>
|
|
40
40
|
extends BST<K, V, N, TREE>
|
|
@@ -42,14 +42,14 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* The constructor function initializes an AVLTree object with optional elements and options.
|
|
45
|
-
* @param [elements] - The `elements` parameter is an optional iterable of `
|
|
45
|
+
* @param [elements] - The `elements` parameter is an optional iterable of `BTNExemplar<K, V, N>`
|
|
46
46
|
* objects. It represents a collection of elements that will be added to the AVL tree during
|
|
47
47
|
* initialization.
|
|
48
48
|
* @param [options] - The `options` parameter is an optional object that allows you to customize the
|
|
49
49
|
* behavior of the AVL tree. It is of type `Partial<AVLTreeOptions>`, which means that you can
|
|
50
50
|
* provide only a subset of the properties defined in the `AVLTreeOptions` interface.
|
|
51
51
|
*/
|
|
52
|
-
constructor(elements?: Iterable<
|
|
52
|
+
constructor(elements?: Iterable<BTNExemplar<K, V, N>>, options?: Partial<AVLTreeOptions<K>>) {
|
|
53
53
|
super([], options);
|
|
54
54
|
if (elements) super.addMany(elements);
|
|
55
55
|
}
|
|
@@ -83,13 +83,23 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* The function checks if an exemplar is an instance of AVLTreeNode.
|
|
86
|
-
* @param exemplar - The `exemplar` parameter is of type `
|
|
86
|
+
* @param exemplar - The `exemplar` parameter is of type `BTNExemplar<K, V, N>`.
|
|
87
87
|
* @returns a boolean value indicating whether the exemplar is an instance of the AVLTreeNode class.
|
|
88
88
|
*/
|
|
89
|
-
override isNode(exemplar:
|
|
89
|
+
override isNode(exemplar: BTNExemplar<K, V, N>): exemplar is N {
|
|
90
90
|
return exemplar instanceof AVLTreeNode;
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The function "isNotNodeInstance" checks if a potential key is a K.
|
|
95
|
+
* @param {any} potentialKey - The potentialKey parameter is of type any, which means it can be any
|
|
96
|
+
* data type.
|
|
97
|
+
* @returns a boolean value indicating whether the potentialKey is of type number or not.
|
|
98
|
+
*/
|
|
99
|
+
override isNotNodeInstance(potentialKey: BTNKeyOrNode<K, N>): potentialKey is K {
|
|
100
|
+
return !(potentialKey instanceof AVLTreeNode)
|
|
101
|
+
}
|
|
102
|
+
|
|
93
103
|
/**
|
|
94
104
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
95
105
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -108,7 +118,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
108
118
|
* being added to the binary tree.
|
|
109
119
|
* @returns The method is returning either the inserted node or undefined.
|
|
110
120
|
*/
|
|
111
|
-
override add(keyOrNodeOrEntry:
|
|
121
|
+
override add(keyOrNodeOrEntry: BTNExemplar<K, V, N>, value?: V): N | undefined {
|
|
112
122
|
if (keyOrNodeOrEntry === null) return undefined;
|
|
113
123
|
const inserted = super.add(keyOrNodeOrEntry, value);
|
|
114
124
|
if (inserted) this._balancePath(inserted);
|
|
@@ -133,12 +143,12 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
133
143
|
* that is deleted from the binary tree. It is an optional parameter and if not provided, it will
|
|
134
144
|
* default to the `_defaultOneParamCallback` function. The `callback` function should have a single
|
|
135
145
|
* parameter of type `N
|
|
136
|
-
* @returns The method is returning an array of `
|
|
146
|
+
* @returns The method is returning an array of `BinaryTreeDeleteResult<N>`.
|
|
137
147
|
*/
|
|
138
148
|
override delete<C extends BTNCallback<N>>(
|
|
139
149
|
identifier: ReturnType<C>,
|
|
140
150
|
callback: C = this._defaultOneParamCallback as C
|
|
141
|
-
):
|
|
151
|
+
): BinaryTreeDeleteResult<N>[] {
|
|
142
152
|
if ((identifier as any) instanceof AVLTreeNode) callback = (node => node) as C;
|
|
143
153
|
const deletedResults = super.delete(identifier, callback);
|
|
144
154
|
for (const { needBalanced } of deletedResults) {
|
|
@@ -160,7 +170,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|
|
160
170
|
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
|
161
171
|
* if either `srcNode` or `destNode` is undefined.
|
|
162
172
|
*/
|
|
163
|
-
protected override _swapProperties(srcNode:
|
|
173
|
+
protected override _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined {
|
|
164
174
|
srcNode = this.ensureNode(srcNode);
|
|
165
175
|
destNode = this.ensureNode(destNode);
|
|
166
176
|
|