From f0927e8b0a634451a302bb82ea89d806ca5ae2b4 Mon Sep 17 00:00:00 2001 From: Revone Date: Mon, 11 Dec 2023 20:27:05 +0800 Subject: [PATCH] docs: Wrote a feature description for each data structure. refactor: Standardize the import of all types and objects --- src/data-structures/binary-tree/avl-tree.ts | 4 +- src/data-structures/binary-tree/bst.ts | 10 ++--- src/data-structures/binary-tree/rb-tree.ts | 4 +- .../binary-tree/tree-multimap.ts | 6 +-- src/data-structures/graph/abstract-graph.ts | 9 ++-- src/data-structures/graph/directed-graph.ts | 4 +- src/data-structures/graph/map-graph.ts | 2 +- src/data-structures/graph/undirected-graph.ts | 4 +- src/data-structures/hash/hash-map.ts | 16 +++++-- src/data-structures/hash/hash-table.ts | 2 +- src/data-structures/heap/heap.ts | 14 +++++- src/data-structures/heap/max-heap.ts | 13 +++++- src/data-structures/heap/min-heap.ts | 13 +++++- .../linked-list/doubly-linked-list.ts | 12 ++++-- .../linked-list/singly-linked-list.ts | 6 +-- .../priority-queue/max-priority-queue.ts | 2 +- .../priority-queue/min-priority-queue.ts | 2 +- .../priority-queue/priority-queue.ts | 11 ++++- src/data-structures/queue/deque.ts | 14 +++--- src/data-structures/queue/queue.ts | 19 +++++++- src/data-structures/stack/stack.ts | 22 +++++++--- src/data-structures/trie/trie.ts | 17 ++++++-- src/types/common.ts | 43 +++++++++++++++---- .../data-structures/binary-tree/avl-tree.ts | 1 - .../binary-tree/binary-tree.ts | 27 +----------- src/types/data-structures/binary-tree/bst.ts | 1 - .../data-structures/binary-tree/rb-tree.ts | 2 +- .../binary-tree/tree-multimap.ts | 2 +- 28 files changed, 182 insertions(+), 100 deletions(-) diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index aa81029..ccfbc47 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -11,7 +11,7 @@ import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, - BSTNodeKeyOrNode, + BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode @@ -170,7 +170,7 @@ export class AVLTree = 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, destNode: BSTNodeKeyOrNode): N | undefined { + protected override _swapProperties(srcNode: BSTNKeyOrNode, destNode: BSTNKeyOrNode): N | undefined { srcNode = this.ensureNode(srcNode); destNode = this.ensureNode(destNode); diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index d651e55..60a75bc 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -7,7 +7,7 @@ */ import type { BSTNested, - BSTNodeKeyOrNode, + BSTNKeyOrNode, BSTNodeNested, BSTOptions, BTNCallback, @@ -380,7 +380,7 @@ export class BST = BSTNode = this.root): K | undefined { + lastKey(beginRoot: BSTNKeyOrNode = this.root): K | undefined { let current = this.ensureNode(beginRoot); if (!current) return undefined; @@ -467,7 +467,7 @@ export class BST = BSTNode, iterationType = IterationType.ITERATIVE): N | undefined { + override ensureNode(key: BSTNKeyOrNode, iterationType = IterationType.ITERATIVE): N | undefined { return this.isNotNodeInstance(key) ? this.getNodeByKey(key, iterationType) : key; } @@ -498,7 +498,7 @@ export class BST = BSTNode | undefined, callback: C = this._defaultOneParamCallback as C, onlyOne = false, - beginRoot: BSTNodeKeyOrNode = this.root, + beginRoot: BSTNKeyOrNode = this.root, iterationType = this.iterationType ): N[] { beginRoot = this.ensureNode(beginRoot); @@ -579,7 +579,7 @@ export class BST = BSTNode>( callback: C = this._defaultOneParamCallback as C, lesserOrGreater: CP = CP.lt, - targetNode: BSTNodeKeyOrNode = this.root, + targetNode: BSTNKeyOrNode = this.root, iterationType = this.iterationType ): ReturnType[] { targetNode = this.ensureNode(targetNode); diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index efd5ea5..df081cc 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -8,7 +8,7 @@ import { BinaryTreeDeleteResult, - BSTNodeKeyOrNode, + BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode, @@ -364,7 +364,7 @@ export class RedBlackTree getNode>( identifier: ReturnType | undefined, callback: C = this._defaultOneParamCallback as C, - beginRoot: BSTNodeKeyOrNode = this.root, + beginRoot: BSTNKeyOrNode = this.root, iterationType = this.iterationType ): N | null | undefined { if ((identifier as any) instanceof RedBlackTreeNode) callback = (node => node) as C; diff --git a/src/data-structures/binary-tree/tree-multimap.ts b/src/data-structures/binary-tree/tree-multimap.ts index 3100a08..399801a 100644 --- a/src/data-structures/binary-tree/tree-multimap.ts +++ b/src/data-structures/binary-tree/tree-multimap.ts @@ -7,7 +7,7 @@ */ import type { BinaryTreeDeleteResult, - BSTNodeKeyOrNode, + BSTNKeyOrNode, BTNCallback, BTNExemplar, BTNKeyOrNode, @@ -372,7 +372,7 @@ export class TreeMultimap * @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): N | undefined { + protected override _addTo(newNode: N | undefined, parent: BSTNKeyOrNode): N | undefined { parent = this.ensureNode(parent); if (parent) { if (parent.left === undefined) { @@ -407,7 +407,7 @@ export class TreeMultimap * @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, destNode: BSTNodeKeyOrNode): N | undefined { + protected override _swapProperties(srcNode: BSTNKeyOrNode, destNode: BSTNKeyOrNode): N | undefined { srcNode = this.ensureNode(srcNode); destNode = this.ensureNode(destNode); if (srcNode && destNode) { diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 959dd33..c65ca36 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -5,13 +5,12 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @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 { key: VertexKey; diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index 9c59b4b..39995ee 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -5,10 +5,10 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @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 extends AbstractVertex { /** diff --git a/src/data-structures/graph/map-graph.ts b/src/data-structures/graph/map-graph.ts index 9b0a08c..abde67d 100644 --- a/src/data-structures/graph/map-graph.ts +++ b/src/data-structures/graph/map-graph.ts @@ -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 extends DirectedVertex { diff --git a/src/data-structures/graph/undirected-graph.ts b/src/data-structures/graph/undirected-graph.ts index 5d775c9..400e2d0 100644 --- a/src/data-structures/graph/undirected-graph.ts +++ b/src/data-structures/graph/undirected-graph.ts @@ -5,10 +5,10 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @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 extends AbstractVertex { /** diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index bce029e..775b237 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -5,11 +5,16 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @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 extends IterableEntryBase { protected _store: { [key: string]: HashMapStoreItem } = {}; protected _objMap: Map = new Map(); @@ -248,6 +253,11 @@ export class HashMap extends IterableEntryBase { } } +/** + * 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 extends IterableEntryBase { protected _noObjMap: Record> = {}; diff --git a/src/data-structures/hash/hash-table.ts b/src/data-structures/hash/hash-table.ts index a46b135..58bf6eb 100644 --- a/src/data-structures/hash/hash-table.ts +++ b/src/data-structures/hash/hash-table.ts @@ -6,7 +6,7 @@ * @license MIT License */ -import { HashFunction } from '../../types'; +import type { HashFunction } from '../../types'; export class HashTableNode { key: K; diff --git a/src/data-structures/heap/heap.ts b/src/data-structures/heap/heap.ts index a383bd1..13aed22 100644 --- a/src/data-structures/heap/heap.ts +++ b/src/data-structures/heap/heap.ts @@ -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 extends IterableElementBase { options: HeapOptions; diff --git a/src/data-structures/heap/max-heap.ts b/src/data-structures/heap/max-heap.ts index a0462ea..4849822 100644 --- a/src/data-structures/heap/max-heap.ts +++ b/src/data-structures/heap/max-heap.ts @@ -5,10 +5,19 @@ * @copyright Copyright (c) 2022 Kirk Qi * @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 extends Heap { constructor( elements?: Iterable, diff --git a/src/data-structures/heap/min-heap.ts b/src/data-structures/heap/min-heap.ts index 65ef234..6fd1143 100644 --- a/src/data-structures/heap/min-heap.ts +++ b/src/data-structures/heap/min-heap.ts @@ -5,10 +5,19 @@ * @copyright Copyright (c) 2022 Kirk Qi * @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 extends Heap { constructor( elements?: Iterable, diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 4530814..ed187de 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -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 * @license MIT License */ +import type { ElementCallback } from '../../types'; +import { IterableElementBase } from '../base'; + export class DoublyLinkedListNode { value: E; next: DoublyLinkedListNode | undefined; @@ -25,6 +25,12 @@ export class DoublyLinkedListNode { } } +/** + * 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 extends IterableElementBase { /** * The constructor initializes the linked list with an empty head, tail, and length. diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index 23eda14..d585714 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -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 * @license MIT License */ +import type { ElementCallback } from "../../types"; +import { IterableElementBase } from "../base"; + export class SinglyLinkedListNode { value: E; next: SinglyLinkedListNode | undefined; diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index 5bfa218..bb5c5c5 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -5,8 +5,8 @@ * @copyright Copyright (c) 2022 Kirk Qi * @license MIT License */ -import { PriorityQueue } from './priority-queue'; import type { PriorityQueueOptions } from '../../types'; +import { PriorityQueue } from './priority-queue'; export class MaxPriorityQueue extends PriorityQueue { constructor( diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index a06b8ee..0d693da 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -5,8 +5,8 @@ * @copyright Copyright (c) 2022 Kirk Qi * @license MIT License */ -import { PriorityQueue } from './priority-queue'; import type { PriorityQueueOptions } from '../../types'; +import { PriorityQueue } from './priority-queue'; export class MinPriorityQueue extends PriorityQueue { constructor(elements?: Iterable, diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index 0f3a5cc..6e454a6 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -5,10 +5,17 @@ * @copyright Copyright (c) 2022 Kirk Qi * @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 extends Heap { constructor(elements?: Iterable, options?: PriorityQueueOptions) { super(elements, options); diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 9c86969..91f603a 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -5,19 +5,17 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @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 extends IterableElementBase { protected _bucketFirst = 0; protected _firstInBucket = 0; diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index ac802e1..b4fc023 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -3,10 +3,19 @@ * @copyright Tyler Zeng * @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 extends IterableElementBase { /** * 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 extends IterableElementBase { } } +/** + * 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 extends SinglyLinkedList { /** * The enqueue function adds a value to the end of an array. diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts index 30f98f2..0434bad 100644 --- a/src/data-structures/stack/stack.ts +++ b/src/data-structures/stack/stack.ts @@ -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 + * @license MIT License + */ +import type { ElementCallback } from '../../types'; +import { IterableElementBase } from '../base'; /** - * @license MIT - * @copyright Tyler Zeng - * @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 extends IterableElementBase { /** @@ -115,7 +125,7 @@ export class Stack extends IterableElementBase { pop(): E | undefined { if (this.isEmpty()) return undefined; - return this.elements.pop() || undefined; + return this.elements.pop(); } /** diff --git a/src/data-structures/trie/trie.ts b/src/data-structures/trie/trie.ts index baec3ad..9340342 100644 --- a/src/data-structures/trie/trie.ts +++ b/src/data-structures/trie/trie.ts @@ -5,9 +5,8 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @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 { constructor(words?: string[], caseSensitive = true) { diff --git a/src/types/common.ts b/src/types/common.ts index 5780a57..b69a9df 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -1,20 +1,43 @@ -export type Comparator = (a: K, b: K) => number; - export enum BSTVariant { MIN = 'MIN', MAX = 'MAX', } -export type DFSOrderPattern = 'pre' | 'in' | 'post'; - -export type BTNCallback = (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 = (a: K, b: K) => number; + +export type DFSOrderPattern = 'pre' | 'in' | 'post'; + +export type NodeDisplayLayout = [string[], number, number, number]; + +export type BTNCallback = (node: N) => D; + export interface IterableWithSize extends Iterable { size: number | ((...args: any[]) => number); } @@ -33,8 +56,10 @@ export type BTNKeyOrNode = K | null | undefined | N; export type BTNExemplar = BTNEntry | BTNKeyOrNode -export type BTNodePureExemplar = [K, V | undefined] | BTNodePureKeyOrNode - export type BTNodePureKeyOrNode = K | N; -export type BSTNodeKeyOrNode = K | undefined | N; \ No newline at end of file +export type BTNodePureExemplar = [K, V | undefined] | BTNodePureKeyOrNode; + +export type BSTNKeyOrNode = K | undefined | N; + +export type BinaryTreeDeleteResult = { deleted: N | null | undefined; needBalanced: N | null | undefined }; diff --git a/src/types/data-structures/binary-tree/avl-tree.ts b/src/types/data-structures/binary-tree/avl-tree.ts index bb3ba01..eb43fe0 100644 --- a/src/types/data-structures/binary-tree/avl-tree.ts +++ b/src/types/data-structures/binary-tree/avl-tree.ts @@ -5,5 +5,4 @@ export type AVLTreeNodeNested = AVLTreeNode> = AVLTree>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> - export type AVLTreeOptions = BSTOptions & {}; diff --git a/src/types/data-structures/binary-tree/binary-tree.ts b/src/types/data-structures/binary-tree/binary-tree.ts index 6610940..afbf50b 100644 --- a/src/types/data-structures/binary-tree/binary-tree.ts +++ b/src/types/data-structures/binary-tree/binary-tree.ts @@ -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 = { deleted: N | null | undefined; needBalanced: N | null | undefined }; +import { IterationType } from "../../common"; export type BinaryTreeNodeNested = BinaryTreeNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @@ -32,5 +9,3 @@ export type BinaryTreeOptions = { iterationType: IterationType, extractor: (key: K) => number } - -export type NodeDisplayLayout = [string[], number, number, number]; diff --git a/src/types/data-structures/binary-tree/bst.ts b/src/types/data-structures/binary-tree/bst.ts index 094d5a8..73f50ba 100644 --- a/src/types/data-structures/binary-tree/bst.ts +++ b/src/types/data-structures/binary-tree/bst.ts @@ -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 = BSTNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> export type BSTNested> = BST>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/src/types/data-structures/binary-tree/rb-tree.ts b/src/types/data-structures/binary-tree/rb-tree.ts index f9539df..6abb7c9 100644 --- a/src/types/data-structures/binary-tree/rb-tree.ts +++ b/src/types/data-structures/binary-tree/rb-tree.ts @@ -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} diff --git a/src/types/data-structures/binary-tree/tree-multimap.ts b/src/types/data-structures/binary-tree/tree-multimap.ts index 621a2dd..ffc7172 100644 --- a/src/types/data-structures/binary-tree/tree-multimap.ts +++ b/src/types/data-structures/binary-tree/tree-multimap.ts @@ -1,5 +1,5 @@ import { TreeMultimap, TreeMultimapNode } from '../../../data-structures'; -import { AVLTreeOptions } from './avl-tree'; +import type { AVLTreeOptions } from './avl-tree'; export type TreeMultimapNodeNested = TreeMultimapNode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>