mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
docs: Wrote a feature description for each data structure. refactor: Standardize the import of all types and objects
This commit is contained in:
parent
60a3154178
commit
f0927e8b0a
|
@ -11,7 +11,7 @@ import type {
|
|||
AVLTreeNodeNested,
|
||||
AVLTreeOptions,
|
||||
BinaryTreeDeleteResult,
|
||||
BSTNodeKeyOrNode,
|
||||
BSTNKeyOrNode,
|
||||
BTNCallback,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode
|
||||
|
@ -170,7 +170,7 @@ export class AVLTree<K = any, V = any, N extends AVLTreeNode<K, V, N> = AVLTreeN
|
|||
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
||||
* if either `srcNode` or `destNode` is undefined.
|
||||
*/
|
||||
protected override _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined {
|
||||
protected override _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined {
|
||||
srcNode = this.ensureNode(srcNode);
|
||||
destNode = this.ensureNode(destNode);
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
import type {
|
||||
BSTNested,
|
||||
BSTNodeKeyOrNode,
|
||||
BSTNKeyOrNode,
|
||||
BSTNodeNested,
|
||||
BSTOptions,
|
||||
BTNCallback,
|
||||
|
@ -380,7 +380,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
||||
* rightmost node otherwise. If no node is found, it returns 0.
|
||||
*/
|
||||
lastKey(beginRoot: BSTNodeKeyOrNode<K, N> = this.root): K | undefined {
|
||||
lastKey(beginRoot: BSTNKeyOrNode<K, N> = this.root): K | undefined {
|
||||
let current = this.ensureNode(beginRoot);
|
||||
if (!current) return undefined;
|
||||
|
||||
|
@ -467,7 +467,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
* type of iteration to be performed. It has a default value of `IterationType.ITERATIVE`.
|
||||
* @returns either a node object (N) or undefined.
|
||||
*/
|
||||
override ensureNode(key: BSTNodeKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
||||
override ensureNode(key: BSTNKeyOrNode<K, N>, iterationType = IterationType.ITERATIVE): N | undefined {
|
||||
return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key;
|
||||
}
|
||||
|
||||
|
@ -498,7 +498,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
identifier: ReturnType<C> | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
onlyOne = false,
|
||||
beginRoot: BSTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BSTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N[] {
|
||||
beginRoot = this.ensureNode(beginRoot);
|
||||
|
@ -579,7 +579,7 @@ export class BST<K = any, V = any, N extends BSTNode<K, V, N> = BSTNode<K, V, BS
|
|||
lesserOrGreaterTraverse<C extends BTNCallback<N>>(
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
lesserOrGreater: CP = CP.lt,
|
||||
targetNode: BSTNodeKeyOrNode<K, N> = this.root,
|
||||
targetNode: BSTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): ReturnType<C>[] {
|
||||
targetNode = this.ensureNode(targetNode);
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
import {
|
||||
BinaryTreeDeleteResult,
|
||||
BSTNodeKeyOrNode,
|
||||
BSTNKeyOrNode,
|
||||
BTNCallback,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode,
|
||||
|
@ -364,7 +364,7 @@ export class RedBlackTree<K = any, V = any, N extends RedBlackTreeNode<K, V, N>
|
|||
getNode<C extends BTNCallback<N>>(
|
||||
identifier: ReturnType<C> | undefined,
|
||||
callback: C = this._defaultOneParamCallback as C,
|
||||
beginRoot: BSTNodeKeyOrNode<K, N> = this.root,
|
||||
beginRoot: BSTNKeyOrNode<K, N> = this.root,
|
||||
iterationType = this.iterationType
|
||||
): N | null | undefined {
|
||||
if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
import type {
|
||||
BinaryTreeDeleteResult,
|
||||
BSTNodeKeyOrNode,
|
||||
BSTNKeyOrNode,
|
||||
BTNCallback,
|
||||
BTNExemplar,
|
||||
BTNKeyOrNode,
|
||||
|
@ -372,7 +372,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* @returns The method `_addTo` returns either the `parent.left` or `parent.right` node that was
|
||||
* added, or `undefined` if no node was added.
|
||||
*/
|
||||
protected override _addTo(newNode: N | undefined, parent: BSTNodeKeyOrNode<K, N>): N | undefined {
|
||||
protected override _addTo(newNode: N | undefined, parent: BSTNKeyOrNode<K, N>): N | undefined {
|
||||
parent = this.ensureNode(parent);
|
||||
if (parent) {
|
||||
if (parent.left === undefined) {
|
||||
|
@ -407,7 +407,7 @@ export class TreeMultimap<K = any, V = any, N extends TreeMultimapNode<K, V, N>
|
|||
* @returns either the `destNode` object if both `srcNode` and `destNode` are defined, or `undefined`
|
||||
* if either `srcNode` or `destNode` is undefined.
|
||||
*/
|
||||
protected override _swapProperties(srcNode: BSTNodeKeyOrNode<K, N>, destNode: BSTNodeKeyOrNode<K, N>): N | undefined {
|
||||
protected override _swapProperties(srcNode: BSTNKeyOrNode<K, N>, destNode: BSTNKeyOrNode<K, N>): N | undefined {
|
||||
srcNode = this.ensureNode(srcNode);
|
||||
destNode = this.ensureNode(destNode);
|
||||
if (srcNode && destNode) {
|
||||
|
|
|
@ -5,13 +5,12 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { DijkstraResult, EntryCallback, VertexKey } from '../../types';
|
||||
import { uuidV4 } from '../../utils';
|
||||
import { Heap } from '../heap';
|
||||
import type { DijkstraResult, VertexKey } from '../../types';
|
||||
import { EntryCallback } from "../../types";
|
||||
import { IGraph } from '../../interfaces';
|
||||
import { Queue } from '../queue';
|
||||
import { IterableEntryBase } from "../base";
|
||||
import { IGraph } from '../../interfaces';
|
||||
import { Heap } from '../heap';
|
||||
import { Queue } from '../queue';
|
||||
|
||||
export abstract class AbstractVertex<V = any> {
|
||||
key: VertexKey;
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import { arrayRemove } from '../../utils';
|
||||
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
||||
import type { TopologicalStatus, VertexKey } from '../../types';
|
||||
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
||||
import { IGraph } from '../../interfaces';
|
||||
import { arrayRemove } from '../../utils';
|
||||
|
||||
export class DirectedVertex<V = any> extends AbstractVertex<V> {
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { MapGraphCoordinate, VertexKey } from '../../types';
|
||||
import type { MapGraphCoordinate, VertexKey } from '../../types';
|
||||
import { DirectedEdge, DirectedGraph, DirectedVertex } from './directed-graph';
|
||||
|
||||
export class MapVertex<V = any> extends DirectedVertex<V> {
|
||||
|
|
|
@ -5,10 +5,10 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import { arrayRemove } from '../../utils';
|
||||
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
||||
import type { VertexKey } from '../../types';
|
||||
import { IGraph } from '../../interfaces';
|
||||
import { AbstractEdge, AbstractGraph, AbstractVertex } from './abstract-graph';
|
||||
import { arrayRemove } from '../../utils';
|
||||
|
||||
export class UndirectedVertex<V = any> extends AbstractVertex<V> {
|
||||
/**
|
||||
|
|
|
@ -5,11 +5,16 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
import { isWeakKey, rangeCheck } from '../../utils';
|
||||
import type { EntryCallback, HashMapLinkedNode, HashMapOptions, HashMapStoreItem } from '../../types';
|
||||
import { IterableEntryBase } from "../base";
|
||||
import { IterableEntryBase } from '../base';
|
||||
import { isWeakKey, rangeCheck } from '../../utils';
|
||||
|
||||
/**
|
||||
* 1. Key-Value Pair Storage: HashMap stores key-value pairs. Each key maps to a value.
|
||||
* 2. Fast Lookup: It's used when you need to quickly find, insert, or delete elements based on a key.
|
||||
* 3. Unique Keys: Keys are unique. If you try to insert another entry with the same key, the old entry will be replaced by the new one.
|
||||
* 4. Unordered Collection: HashMap does not guarantee the order of elements, and the order may change over time.
|
||||
*/
|
||||
export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
||||
protected _store: { [key: string]: HashMapStoreItem<K, V> } = {};
|
||||
protected _objMap: Map<object, V> = new Map();
|
||||
|
@ -248,6 +253,11 @@ export class HashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Maintaining the Order of Element Insertion: Unlike HashMap, LinkedHashMap maintains the order in which elements are inserted. Therefore, when you traverse it, elements will be returned in the order they were inserted into the map.
|
||||
* 2. Based on Hash Table and Linked List: It combines the structures of a hash table and a linked list, using the hash table to ensure fast access, while maintaining the order of elements through the linked list.
|
||||
* 3. Time Complexity: Similar to HashMap, LinkedHashMap offers constant-time performance for get and put operations in most cases.
|
||||
*/
|
||||
export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
|
||||
|
||||
protected _noObjMap: Record<string, HashMapLinkedNode<K, V | undefined>> = {};
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
* @license MIT License
|
||||
*/
|
||||
|
||||
import { HashFunction } from '../../types';
|
||||
import type { HashFunction } from '../../types';
|
||||
|
||||
export class HashTableNode<K = any, V = any> {
|
||||
key: K;
|
||||
|
|
|
@ -6,8 +6,20 @@
|
|||
*/
|
||||
|
||||
import type { Comparator, DFSOrderPattern, ElementCallback, HeapOptions } from '../../types';
|
||||
import { IterableElementBase } from "../base";
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
/**
|
||||
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
||||
* 2. Heap Properties: Each node in a heap follows a specific order property, which varies depending on the type of heap:
|
||||
* Max Heap: The value of each parent node is greater than or equal to the value of its children.
|
||||
* Min Heap: The value of each parent node is less than or equal to the value of its children.
|
||||
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
||||
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
||||
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
||||
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
||||
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
||||
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
||||
*/
|
||||
export class Heap<E = any> extends IterableElementBase<E> {
|
||||
options: HeapOptions<E>;
|
||||
|
||||
|
|
|
@ -5,10 +5,19 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
import { Heap } from './heap';
|
||||
import type { HeapOptions } from '../../types';
|
||||
import { Heap } from './heap';
|
||||
|
||||
/**
|
||||
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
||||
* 2. Heap Properties: The value of each parent node is greater than or equal to the value of its children.
|
||||
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
||||
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
||||
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
||||
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
||||
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
||||
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
||||
*/
|
||||
export class MaxHeap<E = any> extends Heap<E> {
|
||||
constructor(
|
||||
elements?: Iterable<E>,
|
||||
|
|
|
@ -5,10 +5,19 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
import { Heap } from './heap';
|
||||
import type { HeapOptions } from '../../types';
|
||||
import { Heap } from './heap';
|
||||
|
||||
/**
|
||||
* 1. Complete Binary Tree: Heaps are typically complete binary trees, meaning every level is fully filled except possibly for the last level, which has nodes as far left as possible.
|
||||
* 2. Heap Properties: The value of each parent node is less than or equal to the value of its children.
|
||||
* 3. Root Node Access: In a heap, the largest element (in a max heap) or the smallest element (in a min heap) is always at the root of the tree.
|
||||
* 4. Efficient Insertion and Deletion: Due to its structure, a heap allows for insertion and deletion operations in logarithmic time (O(log n)).
|
||||
* 5. Managing Dynamic Data Sets: Heaps effectively manage dynamic data sets, especially when frequent access to the largest or smallest elements is required.
|
||||
* 6. Non-linear Search: While a heap allows rapid access to its largest or smallest element, it is less efficient for other operations, such as searching for a specific element, as it is not designed for these tasks.
|
||||
* 7. Efficient Sorting Algorithms: For example, heap sort. Heap sort uses the properties of a heap to sort elements.
|
||||
* 8. Graph Algorithms: Such as Dijkstra's shortest path algorithm and Prim's minimum spanning tree algorithm, which use heaps to improve performance.
|
||||
*/
|
||||
export class MinHeap<E = any> extends Heap<E> {
|
||||
constructor(
|
||||
elements?: Iterable<E>,
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
import { IterableElementBase } from "../base";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
/**
|
||||
* data-structure-typed
|
||||
*
|
||||
|
@ -8,6 +5,9 @@ import type { ElementCallback } from "../../types";
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { ElementCallback } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
export class DoublyLinkedListNode<E = any> {
|
||||
value: E;
|
||||
next: DoublyLinkedListNode<E> | undefined;
|
||||
|
@ -25,6 +25,12 @@ export class DoublyLinkedListNode<E = any> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 1. Node Structure: Each node contains three parts: a data field, a pointer (or reference) to the previous node, and a pointer to the next node. This structure allows traversal of the linked list in both directions.
|
||||
* 2. Bidirectional Traversal: Unlike singly linked lists, doubly linked lists can be easily traversed forwards or backwards. This makes insertions and deletions in the list more flexible and efficient.
|
||||
* 3. No Centralized Index: Unlike arrays, elements in a linked list are not stored contiguously, so there is no centralized index. Accessing elements in a linked list typically requires traversing from the head or tail node.
|
||||
* 4. High Efficiency in Insertion and Deletion: Adding or removing elements in a linked list does not require moving other elements, making these operations more efficient than in arrays.
|
||||
*/
|
||||
export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* The constructor initializes the linked list with an empty head, tail, and length.
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
import { IterableElementBase } from "../base";
|
||||
import type { ElementCallback } from "../../types";
|
||||
|
||||
/**
|
||||
* data-structure-typed
|
||||
*
|
||||
|
@ -8,6 +5,9 @@ import type { ElementCallback } from "../../types";
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { ElementCallback } from "../../types";
|
||||
import { IterableElementBase } from "../base";
|
||||
|
||||
export class SinglyLinkedListNode<E = any> {
|
||||
value: E;
|
||||
next: SinglyLinkedListNode<E> | undefined;
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import { PriorityQueue } from './priority-queue';
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
import { PriorityQueue } from './priority-queue';
|
||||
|
||||
export class MaxPriorityQueue<E = any> extends PriorityQueue<E> {
|
||||
constructor(
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import { PriorityQueue } from './priority-queue';
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
import { PriorityQueue } from './priority-queue';
|
||||
|
||||
export class MinPriorityQueue<E = any> extends PriorityQueue<E> {
|
||||
constructor(elements?: Iterable<E>,
|
||||
|
|
|
@ -5,10 +5,17 @@
|
|||
* @copyright Copyright (c) 2022 Kirk Qi <qilinaus@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
import { Heap } from '../heap';
|
||||
import type { PriorityQueueOptions } from '../../types';
|
||||
import { Heap } from '../heap';
|
||||
|
||||
/**
|
||||
* 1. Element Priority: In a PriorityQueue, elements are sorted according to their priority. Each dequeue (element removal) operation removes the element with the highest priority. The priority can be determined based on the natural ordering of the elements or through a provided comparator (Comparator).
|
||||
* 2. Heap-Based Implementation: PriorityQueue is typically implemented using a binary heap, allowing both insertion and removal operations to be completed in O(log n) time, where n is the number of elements in the queue.
|
||||
* 3. Task Scheduling: In systems where tasks need to be processed based on the urgency of tasks rather than the order of arrival.
|
||||
* 4. Dijkstra's Algorithm: In shortest path algorithms for graphs, used to select the next shortest edge to visit.
|
||||
* 5. Huffman Coding: Used to select the smallest node combination when constructing a Huffman tree.
|
||||
* 6. Kth Largest Element in a Data Stream: Used to maintain a min-heap of size K for quickly finding the Kth largest element in stream data
|
||||
*/
|
||||
export class PriorityQueue<E = any> extends Heap<E> {
|
||||
constructor(elements?: Iterable<E>, options?: PriorityQueueOptions<E>) {
|
||||
super(elements, options);
|
||||
|
|
|
@ -5,19 +5,17 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
|
||||
import type { ElementCallback, IterableWithSizeOrLength } from "../../types";
|
||||
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
||||
import { IterableElementBase } from "../base";
|
||||
import { calcMinUnitsRequired, rangeCheck } from "../../utils";
|
||||
|
||||
/**
|
||||
* Deque can provide random access with O(1) time complexity
|
||||
* Deque is usually more compact and efficient in memory usage because it does not require additional space to store pointers.
|
||||
* Deque may experience performance jitter, but DoublyLinkedList will not
|
||||
* Deque is implemented using a dynamic array. Inserting or deleting beyond both ends of the array may require moving elements or reallocating space.
|
||||
* 1. Operations at Both Ends: Supports adding and removing elements at both the front and back of the queue. This allows it to be used as a stack (last in, first out) and a queue (first in, first out).
|
||||
* 2. Efficient Random Access: Being based on an array, it offers fast random access capability, allowing constant time access to any element.
|
||||
* 3. Continuous Memory Allocation: Since it is based on an array, all elements are stored contiguously in memory, which can bring cache friendliness and efficient memory access.
|
||||
* 4. Efficiency: Adding and removing elements at both ends of a deque is usually very fast. However, when the dynamic array needs to expand, it may involve copying the entire array to a larger one, and this operation has a time complexity of O(n).
|
||||
* 5. Performance jitter: Deque may experience performance jitter, but DoublyLinkedList will not
|
||||
*/
|
||||
|
||||
export class Deque<E> extends IterableElementBase<E> {
|
||||
protected _bucketFirst = 0;
|
||||
protected _firstInBucket = 0;
|
||||
|
|
|
@ -3,10 +3,19 @@
|
|||
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
||||
* @class
|
||||
*/
|
||||
import { SinglyLinkedList } from '../linked-list';
|
||||
import type { ElementCallback } from '../../types';
|
||||
import { IterableElementBase } from "../base";
|
||||
import type { ElementCallback } from "../../types";
|
||||
import { SinglyLinkedList } from '../linked-list';
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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).
|
||||
* 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.
|
||||
* 4. Task Scheduling: Managing the order of task execution in operating systems or applications.
|
||||
* 5. Data Buffering: Acting as a buffer for data packets in network communication.
|
||||
* 6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store nodes that are to be visited.
|
||||
* 7. Real-time Queuing: Like queuing systems in banks or supermarkets.
|
||||
*/
|
||||
export class Queue<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
* The constructor initializes an instance of a class with an optional array of elements and sets the offset to 0.
|
||||
|
@ -346,6 +355,12 @@ export class Queue<E = any> extends IterableElementBase<E> {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
*/
|
||||
export class LinkedListQueue<E = any> extends SinglyLinkedList<E> {
|
||||
/**
|
||||
* The enqueue function adds a value to the end of an array.
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
import { IterableElementBase } from "../base";
|
||||
import type { ElementCallback } from "../../types";
|
||||
/**
|
||||
* data-structure-typed
|
||||
*
|
||||
* @author Tyler Zeng
|
||||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
import type { ElementCallback } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
/**
|
||||
* @license MIT
|
||||
* @copyright Tyler Zeng <zrwusa@gmail.com>
|
||||
* @class
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* 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.
|
||||
* 5. Expression Evaluation: Used for the evaluation of arithmetic or logical expressions, especially when dealing with parenthesis matching and operator precedence.
|
||||
* 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.
|
||||
*/
|
||||
export class Stack<E = any> extends IterableElementBase<E> {
|
||||
/**
|
||||
|
@ -115,7 +125,7 @@ export class Stack<E = any> extends IterableElementBase<E> {
|
|||
pop(): E | undefined {
|
||||
if (this.isEmpty()) return undefined;
|
||||
|
||||
return this.elements.pop() || undefined;
|
||||
return this.elements.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -5,9 +5,8 @@
|
|||
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
||||
* @license MIT License
|
||||
*/
|
||||
|
||||
import { IterableElementBase } from "../base";
|
||||
import type { ElementCallback } from "../../types";
|
||||
import type { ElementCallback } from '../../types';
|
||||
import { IterableElementBase } from '../base';
|
||||
|
||||
/**
|
||||
* TrieNode represents a node in the Trie data structure. It holds a character key, a map of children nodes,
|
||||
|
@ -26,7 +25,17 @@ export class TrieNode {
|
|||
}
|
||||
|
||||
/**
|
||||
* Trie represents a Trie data structure. It provides basic Trie operations and additional methods.
|
||||
* 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.
|
||||
* 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'.
|
||||
* 3. Fast Retrieval: Trie allows retrieval in O(m) time complexity, where m is the length of the string to be searched.
|
||||
* 4. Space Efficiency: Trie can store a large number of strings very space-efficiently, especially when these strings share common prefixes.
|
||||
* 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.
|
||||
* 6. Sorting: Trie can be used to sort a set of strings in alphabetical order.
|
||||
* 7. String Retrieval: For example, searching for a specific string in a large set of strings.
|
||||
* 8. Autocomplete: Providing recommended words or phrases as a user types.
|
||||
* 9. Spell Check: Checking the spelling of words.
|
||||
* 10. IP Routing: Used in certain types of IP routing algorithms.
|
||||
* 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data."
|
||||
*/
|
||||
export class Trie extends IterableElementBase<string> {
|
||||
constructor(words?: string[], caseSensitive = true) {
|
||||
|
|
|
@ -1,20 +1,43 @@
|
|||
export type Comparator<K> = (a: K, b: K) => number;
|
||||
|
||||
export enum BSTVariant {
|
||||
MIN = 'MIN',
|
||||
MAX = 'MAX',
|
||||
}
|
||||
|
||||
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
||||
|
||||
export type BTNCallback<N, D = any> = (node: N) => D;
|
||||
|
||||
export enum CP {
|
||||
lt = 'lt',
|
||||
eq = 'eq',
|
||||
gt = 'gt'
|
||||
}
|
||||
|
||||
/**
|
||||
* Enum representing different loop types.
|
||||
*
|
||||
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
||||
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
||||
*/
|
||||
export enum IterationType {
|
||||
ITERATIVE = 'ITERATIVE',
|
||||
RECURSIVE = 'RECURSIVE'
|
||||
}
|
||||
|
||||
export enum FamilyPosition {
|
||||
ROOT = 'ROOT',
|
||||
LEFT = 'LEFT',
|
||||
RIGHT = 'RIGHT',
|
||||
ROOT_LEFT = 'ROOT_LEFT',
|
||||
ROOT_RIGHT = 'ROOT_RIGHT',
|
||||
ISOLATED = 'ISOLATED',
|
||||
MAL_NODE = 'MAL_NODE'
|
||||
}
|
||||
|
||||
export type Comparator<K> = (a: K, b: K) => number;
|
||||
|
||||
export type DFSOrderPattern = 'pre' | 'in' | 'post';
|
||||
|
||||
export type NodeDisplayLayout = [string[], number, number, number];
|
||||
|
||||
export type BTNCallback<N, D = any> = (node: N) => D;
|
||||
|
||||
export interface IterableWithSize<T> extends Iterable<T> {
|
||||
size: number | ((...args: any[]) => number);
|
||||
}
|
||||
|
@ -33,8 +56,10 @@ export type BTNKeyOrNode<K, N> = K | null | undefined | N;
|
|||
|
||||
export type BTNExemplar<K, V, N> = BTNEntry<K, V> | BTNKeyOrNode<K, N>
|
||||
|
||||
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>
|
||||
|
||||
export type BTNodePureKeyOrNode<K, N> = K | N;
|
||||
|
||||
export type BSTNodeKeyOrNode<K, N> = K | undefined | N;
|
||||
export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNode<K, N>;
|
||||
|
||||
export type BSTNKeyOrNode<K, N> = K | undefined | N;
|
||||
|
||||
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
||||
|
|
|
@ -5,5 +5,4 @@ export type AVLTreeNodeNested<K, V> = AVLTreeNode<K, V, AVLTreeNode<K, V, AVLTre
|
|||
|
||||
export type AVLTreeNested<K, V, N extends AVLTreeNode<K, V, N>> = AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, AVLTree<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
|
||||
export type AVLTreeOptions<K> = BSTOptions<K> & {};
|
||||
|
|
|
@ -1,28 +1,5 @@
|
|||
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
||||
|
||||
/**
|
||||
* Enum representing different loop types.
|
||||
*
|
||||
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
||||
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
||||
*/
|
||||
|
||||
export enum IterationType {
|
||||
ITERATIVE = 'ITERATIVE',
|
||||
RECURSIVE = 'RECURSIVE'
|
||||
}
|
||||
|
||||
export enum FamilyPosition {
|
||||
ROOT = 'ROOT',
|
||||
LEFT = 'LEFT',
|
||||
RIGHT = 'RIGHT',
|
||||
ROOT_LEFT = 'ROOT_LEFT',
|
||||
ROOT_RIGHT = 'ROOT_RIGHT',
|
||||
ISOLATED = 'ISOLATED',
|
||||
MAL_NODE = 'MAL_NODE'
|
||||
}
|
||||
|
||||
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
|
||||
import { IterationType } from "../../common";
|
||||
|
||||
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
|
@ -32,5 +9,3 @@ export type BinaryTreeOptions<K> = {
|
|||
iterationType: IterationType,
|
||||
extractor: (key: K) => number
|
||||
}
|
||||
|
||||
export type NodeDisplayLayout = [string[], number, number, number];
|
||||
|
|
|
@ -2,7 +2,6 @@ import { BST, BSTNode } from '../../../data-structures';
|
|||
import type { BinaryTreeOptions } from './binary-tree';
|
||||
import { BSTVariant } from "../../common";
|
||||
|
||||
// prettier-ignore
|
||||
export type BSTNodeNested<K, V> = BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, BSTNode<K, V, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
export type BSTNested<K, V, N extends BSTNode<K, V, N>> = BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, BST<K, V, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
||||
import { BSTOptions } from "./bst";
|
||||
import type { BSTOptions } from "./bst";
|
||||
|
||||
export enum RBTNColor { RED = 1, BLACK = 0}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { TreeMultimap, TreeMultimapNode } from '../../../data-structures';
|
||||
import { AVLTreeOptions } from './avl-tree';
|
||||
import type { AVLTreeOptions } from './avl-tree';
|
||||
|
||||
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
|
||||
|
||||
|
|
Loading…
Reference in a new issue