diff --git a/src/data-structures/binary-tree/avl-tree.ts b/src/data-structures/binary-tree/avl-tree.ts index 282fd21..d095a27 100644 --- a/src/data-structures/binary-tree/avl-tree.ts +++ b/src/data-structures/binary-tree/avl-tree.ts @@ -7,22 +7,17 @@ */ import {BST, BSTNode} from './bst'; import type {AVLTreeDeleted, BinaryTreeNodeId} from '../types'; +import {IBinaryTreeNode} from '../interfaces'; -export class AVLTreeNode extends BSTNode { - /** - * The function overrides the clone method of the AVLTreeNode class to create a new AVLTreeNode object with the same - * id, value, and count. - * @returns The method is returning a new instance of the AVLTreeNode class with the same id, val, and count values as - * the current instance. - */ - override clone(): AVLTreeNode { - return new AVLTreeNode(this.id, this.val, this.count); +export class AVLTreeNode extends BSTNode implements IBinaryTreeNode { + override _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode | null { + return val !== null ? new AVLTreeNode(id, val, count) : null; } } export class AVLTree extends BST { - override createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode { + override _createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode { return new AVLTreeNode(id, val, count); } diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index c6e4682..fb2faae 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -16,6 +16,7 @@ import type { ResultByProperty, ResultsByProperty } from '../types'; +import {IBinaryTree, IBinaryTreeNode} from '../interfaces'; /* This enumeration defines the position of a node within a family tree composed of three associated nodes, where 'root' represents the root node of the family tree, 'left' represents the left child node, and 'right' represents the right child node. */ export enum FamilyPosition {root, left, right} @@ -28,7 +29,7 @@ export enum FamilyPosition {root, left, right} */ export enum LoopType { iterative = 1, recursive = 2} -export class BinaryTreeNode { +export class BinaryTreeNode implements IBinaryTreeNode { constructor(id: BinaryTreeNodeId, val: T, count?: number) { this._id = id; @@ -47,6 +48,7 @@ export class BinaryTreeNode { } private _val: T; + get val(): T { return this._val; } @@ -123,31 +125,37 @@ export class BinaryTreeNode { this._height = v; } + _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode | null { + return val !== null ? new BinaryTreeNode(id, val, count) : null; + } + swapLocation(swapNode: BinaryTreeNode): BinaryTreeNode { const {val, count, height} = swapNode; - const tempNode = new BinaryTreeNode(swapNode.id, val); - tempNode.val = val; - tempNode.count = count; - tempNode.height = height; + const tempNode = this._createNode(swapNode.id, val); + if (tempNode instanceof BinaryTreeNode) { + tempNode.val = val; + tempNode.count = count; + tempNode.height = height; - swapNode.id = this.id; - swapNode.val = this.val; - swapNode.count = this.count; - swapNode.height = this.height; + swapNode.id = this.id; + swapNode.val = this.val; + swapNode.count = this.count; + swapNode.height = this.height; - this.id = tempNode.id; - this.val = tempNode.val; - this.count = tempNode.count; - this.height = tempNode.height; + this.id = tempNode.id; + this.val = tempNode.val; + this.count = tempNode.count; + this.height = tempNode.height; + } return swapNode; } - clone(): BinaryTreeNode { - return new BinaryTreeNode(this.id, this.val, this.count); + clone(): BinaryTreeNode | null { + return this._createNode(this.id, this.val, this.count); } } -export class BinaryTree { +export class BinaryTree implements IBinaryTree { /** * The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and @@ -243,19 +251,18 @@ export class BinaryTree { } /** - * The function creates a new binary tree node with the given id, value, and count, or returns null if the value is - * null. + * The function creates a new binary tree node with the given id, value, and count if the value is not null, otherwise + * it returns null. * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type - * `BinaryTreeNodeId`, which could be a string or a number, depending on how you want to identify your nodes. - * @param {T | null} val - The `val` parameter represents the value to be stored in the binary tree node. It can be of - * any type `T` or `null`. - * @param {number} [count] - The count parameter is an optional parameter that represents the number of occurrences of - * the value in the binary tree node. It is of type number. - * @returns The function `createNode` returns a `BinaryTreeNode` object if the `val` parameter is not null. - * Otherwise, it returns null. + * `BinaryTreeNodeId`. + * @param {T | null} val - The `val` parameter represents the value of the node. It can be of type `T` (generic type) + * or `null`. + * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number + * of occurrences of the value in the binary tree node. If not provided, the default value is `undefined`. + * @returns a BinaryTreeNode object if the value is not null, otherwise it returns null. */ - createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode | null { - return val !== null ? new BinaryTreeNode(id, val, count) : null; + _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode | null { + return val !== null ? new BinaryTreeNode(id, val, count) : null; } /** @@ -305,7 +312,7 @@ export class BinaryTree { }; let inserted: BinaryTreeNode | null | undefined; - const needInsert = val !== null ? new BinaryTreeNode(id, val, count) : null; + const needInsert = val !== null ? this._createNode(id, val, count) : null; const existNode = val !== null ? this.get(id, 'id') : null; if (this.root) { if (existNode) { @@ -319,7 +326,7 @@ export class BinaryTree { inserted = _bfs(this.root, needInsert); } } else { - this._setRoot(val !== null ? new BinaryTreeNode(id, val, count) : null); + this._setRoot(val !== null ? this._createNode(id, val, count) : null); if (needInsert !== null) { this._setSize(1); this._setCount(count); diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 6641b44..e06c078 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -7,16 +7,17 @@ */ import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types'; import {BinaryTree, BinaryTreeNode, FamilyPosition, LoopType,} from './binary-tree'; +import {IBinaryTree, IBinaryTreeNode} from '../interfaces'; export enum CP {lt = -1, eq = 0, gt = 1} -export class BSTNode extends BinaryTreeNode { - override clone(): BSTNode { - return new BSTNode(this.id, this.val, this.count); +export class BSTNode extends BinaryTreeNode implements IBinaryTreeNode { + override _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode | null { + return val !== null ? new BSTNode(id, val, count) : null; } } -export class BST extends BinaryTree { +export class BST extends BinaryTree implements IBinaryTree { /** * The constructor function accepts an optional options object and sets the comparator property if provided. * @param [options] - An optional object that can contain the following properties: @@ -34,7 +35,7 @@ export class BST extends BinaryTree { } } - override createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode | null { + override _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BSTNode | null { return val !== null ? new BSTNode(id, val, count) : null; } @@ -52,7 +53,7 @@ export class BST extends BinaryTree { */ override add(id: BinaryTreeNodeId, val: T | null, count: number = 1): BSTNode | null { let inserted: BSTNode | null = null; - const newNode = this.createNode(id, val, count); + const newNode = this._createNode(id, val, count); if (this.root === null) { this._setRoot(newNode); this._setSize(this.size + 1); diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 097b542..f9e0fe7 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -1,3 +1,110 @@ -export class RBTree { +import {BinaryTree, BinaryTreeNode, LoopType} from './binary-tree'; +import {IBinaryTree, IBinaryTreeNode} from '../interfaces'; + +enum RBColor { Red, Black } + +class RBNode extends BinaryTreeNode implements IBinaryTreeNode { + // override createNode(id: BinaryTreeNodeId, val: T | null, count?: number): RBNode | null { + // return val !== null ? new RBNode(id, val, count) : null; + // } + + constructor(id: number, val: T, count?: number) { + super(id, val, count); + } + + private _color: RBColor = RBColor.Red; + + get color(): RBColor { + return this._color; + } + + set color(value: RBColor) { + this._color = value; + } + + // private override _parent: RBNode | null; + // override set parent(v: RBNode | null) { + // this._parent = v; + // } + // override get parent(): RBNode | null { + // return this._parent; + // } + // private override _left?: RBNode | null; + // + // override get left(): RBNode | null | undefined { + // return this._left; + // } + // + // override set left(v: RBNode | null | undefined) { + // if (v) { + // v.parent = this; + // v.familyPosition = FamilyPosition.left; + // } + // this._left = v; + // } + // + // private override _right?: RBNode | null; + // + // override get right(): RBNode | null | undefined { + // return this._right; + // } + // + // override set right(v: RBNode | null | undefined) { + // if (v) { + // v.parent = this; + // v.familyPosition = FamilyPosition.right; + // } + // this._right = v; + // } +} + +class RBTree extends BinaryTree implements IBinaryTree { + constructor(options?: { + loopType?: LoopType, + autoIncrementId?: boolean, + isDuplicatedVal?: boolean + }) { + super(options); + } + + // override _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): RBNode | null { + // return val !== null ? new RBNode(id, val, count) : null; + // } + + // private override _root: BinaryTreeNode | null = null; + // + // override get root(): BinaryTreeNode | null { + // return this._root; + // } + + insert(id: number, val: T | null) { + + } + + private leftRotate(node: RBNode) { + + } + + private rightRotate(node: RBNode) { + + } + + private insertFixup(node: RBNode) { + + } + + private deleteFixup(node: RBNode) { + + } + + private transplant(u: RBNode, v: RBNode) { + + } + + // override remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted[] { + // + // return [{deleted: new RBNode(0, 0), needBalanced: null}]; + // } + } diff --git a/src/data-structures/binary-tree/tree-multiset.ts b/src/data-structures/binary-tree/tree-multiset.ts index 8dc7926..46d9405 100644 --- a/src/data-structures/binary-tree/tree-multiset.ts +++ b/src/data-structures/binary-tree/tree-multiset.ts @@ -7,8 +7,9 @@ */ import {BST, BSTNode} from './bst'; import type {BinaryTreeNodeId, TreeMultiSetDeletedResult} from '../types'; +import {IBinaryTree} from '../interfaces'; -export class TreeMultiSet extends BST { +export class TreeMultiSet extends BST implements IBinaryTree { /** * The function creates a new BSTNode with the given id, value, and count. * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to @@ -18,7 +19,7 @@ export class TreeMultiSet extends BST { * occurrences of the value in the binary search tree node. If not provided, the count will default to 1. * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided). */ - override createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode { + override _createNode(id: BinaryTreeNodeId, val: T, count?: number): BSTNode { return new BSTNode(id, val, count); } diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 2d2db55..1c96c97 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -7,7 +7,8 @@ */ import {arrayRemove, uuidV4} from '../../utils'; import {PriorityQueue} from '../priority-queue'; -import type {DijkstraResult, IGraph, VertexId} from '../types'; +import type {DijkstraResult, VertexId} from '../types'; +import {IGraph} from '../interfaces'; export class AbstractVertex { constructor(id: VertexId) { @@ -389,7 +390,7 @@ export abstract class AbstractGraph`. */ dijkstraWithoutHeap(src: V | VertexId, dest?: V | VertexId | null, getMinDist?: boolean, genPaths?: boolean): DijkstraResult { - if (getMinDist === undefined) getMinDist = false ; + if (getMinDist === undefined) getMinDist = false; if (genPaths === undefined) genPaths = false; if (dest === undefined) dest = null; diff --git a/src/data-structures/graph/directed-graph.ts b/src/data-structures/graph/directed-graph.ts index 41e6b86..abd7fca 100644 --- a/src/data-structures/graph/directed-graph.ts +++ b/src/data-structures/graph/directed-graph.ts @@ -7,7 +7,8 @@ */ import {arrayRemove} from '../../utils'; import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph'; -import type {IDirectedGraph, TopologicalStatus, VertexId} from '../types'; +import type {TopologicalStatus, VertexId} from '../types'; +import {IDirectedGraph} from '../interfaces'; export class DirectedVertex extends AbstractVertex { /** diff --git a/src/data-structures/hash/coordinate-set.ts b/src/data-structures/hash/coordinate-set.ts index 937ea7e..7d65fd1 100644 --- a/src/data-structures/hash/coordinate-set.ts +++ b/src/data-structures/hash/coordinate-set.ts @@ -5,7 +5,7 @@ * @copyright Copyright (c) 2022 Tyler Zeng * @license MIT License */ -export class CoordinateSet extends Set { +export class CoordinateSet extends Set { constructor(joint?: string) { super(); if (joint !== undefined) this._joint = joint; diff --git a/src/data-structures/index.ts b/src/data-structures/index.ts index c431497..9dfa0b2 100644 --- a/src/data-structures/index.ts +++ b/src/data-structures/index.ts @@ -9,6 +9,7 @@ export * from './heap'; export * from './priority-queue'; export * from './matrix'; export * from './trie'; +export * from './interfaces'; export * from './types'; diff --git a/src/data-structures/interfaces/abstract-graph.ts b/src/data-structures/interfaces/abstract-graph.ts new file mode 100644 index 0000000..50d08f8 --- /dev/null +++ b/src/data-structures/interfaces/abstract-graph.ts @@ -0,0 +1,40 @@ +import {VertexId} from '../types'; + +export interface IGraph { + + hasVertex(vertexOrId: V | VertexId): boolean; + + getVertex(vertexOrId: VertexId | V): V | null; + + getVertexId(vertexOrId: V | VertexId): VertexId; + + vertexSet(): Map; + + addVertex(v: V): boolean; + + removeVertex(vertexOrId: V | VertexId): boolean; + + removeAllVertices(vertices: V[] | VertexId[]): boolean; + + degreeOf(vertexOrId: V | VertexId): number; + + edgesOf(vertexOrId: V | VertexId): E[]; + + hasEdge(src: V | VertexId, dest: V | VertexId): boolean; + + getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null; + + edgeSet(): E[]; + + addEdge(edge: E): boolean; + + removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null; + + removeEdge(edge: E): E | null; + + setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean; + + getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null; + + getNeighbors(vertexOrId: V | VertexId): V[]; +} \ No newline at end of file diff --git a/src/data-structures/interfaces/avl-tree.ts b/src/data-structures/interfaces/avl-tree.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/avl-tree.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/binary-tree.ts b/src/data-structures/interfaces/binary-tree.ts new file mode 100644 index 0000000..2471ea0 --- /dev/null +++ b/src/data-structures/interfaces/binary-tree.ts @@ -0,0 +1,10 @@ +import {BinaryTreeNodeId} from '../types'; +import {BinaryTreeNode} from '../binary-tree'; + +export interface IBinaryTreeNode { + _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode | null +} + +export interface IBinaryTree { + _createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode | null +} \ No newline at end of file diff --git a/src/data-structures/interfaces/bst.ts b/src/data-structures/interfaces/bst.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/bst.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/directed-graph.ts b/src/data-structures/interfaces/directed-graph.ts new file mode 100644 index 0000000..87399c6 --- /dev/null +++ b/src/data-structures/interfaces/directed-graph.ts @@ -0,0 +1,15 @@ +import {VertexId} from '../types'; + +export interface IDirectedGraph { + incomingEdgesOf(vertex: V): E[]; + + outgoingEdgesOf(vertex: V): E[]; + + inDegreeOf(vertexOrId: V | VertexId): number; + + outDegreeOf(vertexOrId: V | VertexId): number; + + getEdgeSrc(e: E): V | null; + + getEdgeDest(e: E): V | null; +} \ No newline at end of file diff --git a/src/data-structures/interfaces/doubly-linked-list.ts b/src/data-structures/interfaces/doubly-linked-list.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/doubly-linked-list.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/heap.ts b/src/data-structures/interfaces/heap.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/heap.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/index.ts b/src/data-structures/interfaces/index.ts new file mode 100644 index 0000000..cf54b0e --- /dev/null +++ b/src/data-structures/interfaces/index.ts @@ -0,0 +1,13 @@ +export * from './binary-tree'; +export * from './bst'; +export * from './avl-tree'; +export * from './segment-tree'; +export * from './tree-multiset'; +export * from './abstract-graph'; +export * from './directed-graph'; +export * from './undirected-graph'; +export * from './priority-queue'; +export * from './heap'; +export * from './singly-linked-list'; +export * from './doubly-linked-list'; +export * from './navigator'; \ No newline at end of file diff --git a/src/data-structures/interfaces/navigator.ts b/src/data-structures/interfaces/navigator.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/navigator.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/priority-queue.ts b/src/data-structures/interfaces/priority-queue.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/priority-queue.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/segment-tree.ts b/src/data-structures/interfaces/segment-tree.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/segment-tree.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/singly-linked-list.ts b/src/data-structures/interfaces/singly-linked-list.ts new file mode 100644 index 0000000..336ce12 --- /dev/null +++ b/src/data-structures/interfaces/singly-linked-list.ts @@ -0,0 +1 @@ +export {} diff --git a/src/data-structures/interfaces/tree-multiset.ts b/src/data-structures/interfaces/tree-multiset.ts new file mode 100644 index 0000000..693da49 --- /dev/null +++ b/src/data-structures/interfaces/tree-multiset.ts @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/src/data-structures/interfaces/undirected-graph.ts b/src/data-structures/interfaces/undirected-graph.ts new file mode 100644 index 0000000..181f593 --- /dev/null +++ b/src/data-structures/interfaces/undirected-graph.ts @@ -0,0 +1,3 @@ +export interface IUNDirectedGraph { + +} \ No newline at end of file diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 6e5a1de..cc24188 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -49,7 +49,7 @@ export class DoublyLinkedListNode { } } -export class DoublyLinkedList { +export class DoublyLinkedList { /** * 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 fb052b6..3f1b5b0 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -38,7 +38,7 @@ export class SinglyLinkedListNode { } } -export class SinglyLinkedList { +export class SinglyLinkedList { /** * The constructor initializes the linked list with an empty head, tail, and length. diff --git a/src/data-structures/priority-queue/max-priority-queue.ts b/src/data-structures/priority-queue/max-priority-queue.ts index d123353..97a63e0 100644 --- a/src/data-structures/priority-queue/max-priority-queue.ts +++ b/src/data-structures/priority-queue/max-priority-queue.ts @@ -6,7 +6,8 @@ * @license MIT License */ import {PriorityQueue} from './priority-queue'; -import type {PriorityQueueOptions, SpecifyOptional} from '../types'; +import type {PriorityQueueOptions} from '../types'; +import {SpecifyOptional} from '../../utils'; export class MaxPriorityQueue extends PriorityQueue { constructor(options?: Omit, 'comparator'>) diff --git a/src/data-structures/priority-queue/min-priority-queue.ts b/src/data-structures/priority-queue/min-priority-queue.ts index bd01c70..933c819 100644 --- a/src/data-structures/priority-queue/min-priority-queue.ts +++ b/src/data-structures/priority-queue/min-priority-queue.ts @@ -6,7 +6,8 @@ * @license MIT License */ import {PriorityQueue} from './priority-queue'; -import type {PriorityQueueOptions, SpecifyOptional} from '../types'; +import type {PriorityQueueOptions} from '../types'; +import {SpecifyOptional} from '../../utils'; export class MinPriorityQueue extends PriorityQueue { constructor(options?: Omit, 'comparator'>) diff --git a/src/data-structures/priority-queue/priority-queue.ts b/src/data-structures/priority-queue/priority-queue.ts index ec6632e..c846708 100644 --- a/src/data-structures/priority-queue/priority-queue.ts +++ b/src/data-structures/priority-queue/priority-queue.ts @@ -30,10 +30,6 @@ export class PriorityQueue { return this._nodes; } - protected set nodes(value: T[]) { - this._nodes = value; - } - get size(): number { return this.nodes.length; } @@ -137,7 +133,7 @@ export class PriorityQueue { * The clear function clears the nodes array. */ clear() { - this.nodes = []; + this._setNodes([]); } /** @@ -231,6 +227,10 @@ export class PriorityQueue { return visitedNode; } + protected _setNodes(value: T[]) { + this._nodes = value; + } + protected readonly _comparator: PriorityQueueComparator = (a: T, b: T) => { const aKey = a as unknown as number, bKey = b as unknown as number; return aKey - bKey; diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index 0190442..7db2a5d 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -16,7 +16,7 @@ export class Deque extends DoublyLinkedList { // O(1) time complexity of obtaining the value // O(n) time complexity of adding at the beginning and the end // todo tested slowest one -export class ObjectDeque { +export class ObjectDeque { constructor(capacity?: number) { if (capacity !== undefined) this._capacity = capacity; } @@ -27,10 +27,6 @@ export class ObjectDeque { return this._nodes; } - protected set nodes(value: { [p: number]: T }) { - this._nodes = value; - } - private _capacity = Number.MAX_SAFE_INTEGER; get capacity(): number { @@ -67,10 +63,6 @@ export class ObjectDeque { return this._size; } - protected set size(value: number) { - this._size = value; - } - addFirst(value: T) { if (this._size === 0) { const mid = Math.floor(this._capacity / 2); @@ -129,6 +121,14 @@ export class ObjectDeque { isEmpty() { return this._size <= 0; } + + protected _seNodes(value: { [p: number]: T }) { + this._nodes = value; + } + + protected _setSize(value: number) { + this._size = value; + } } // O(1) time complexity of obtaining the value diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index a709f14..239127c 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -3,7 +3,7 @@ * @copyright Tyler Zeng * @class */ -export class Queue { +export class Queue { protected _nodes: T[]; protected _offset: number; diff --git a/src/data-structures/stack/stack.ts b/src/data-structures/stack/stack.ts index 3ad714f..e2f54ba 100644 --- a/src/data-structures/stack/stack.ts +++ b/src/data-structures/stack/stack.ts @@ -3,7 +3,7 @@ * @copyright Tyler Zeng * @class */ -export class Stack { +export class Stack { protected _elements: T[]; /** diff --git a/src/data-structures/types/abstract-graph.ts b/src/data-structures/types/abstract-graph.ts index 6c6fc66..890442d 100644 --- a/src/data-structures/types/abstract-graph.ts +++ b/src/data-structures/types/abstract-graph.ts @@ -2,50 +2,3 @@ export type VertexId = string | number; export type DijkstraResult = { distMap: Map, preMap: Map, seen: Set, paths: V[][], minDist: number, minPath: V[] } | null; - -export interface IGraph { - - hasVertex(vertexOrId: V | VertexId): boolean; - - getVertex(vertexOrId: VertexId | V): V | null; - - getVertexId(vertexOrId: V | VertexId): VertexId; - - vertexSet(): Map; - - addVertex(v: V): boolean; - - removeVertex(vertexOrId: V | VertexId): boolean; - - removeAllVertices(vertices: V[] | VertexId[]): boolean; - - degreeOf(vertexOrId: V | VertexId): number; - - edgesOf(vertexOrId: V | VertexId): E[]; - - hasEdge(src: V | VertexId, dest: V | VertexId): boolean; - - // hasEdge(e: E): boolean; - - getEdge(srcOrId: V | VertexId, destOrId: V | VertexId): E | null; - - // getAllEdges(src: V, dest: V): E[]; - - edgeSet(): E[]; - - addEdge(edge: E): boolean; - - removeEdgeBetween(srcOrId: V | VertexId, destOrId: V | VertexId): E | null; - - removeEdge(edge: E): E | null; - - // removeAllEdges(v1: VertexId | V, v2: VertexId | V): (E | null)[]; - - // removeAllEdges(edges: E[] | [VertexId, VertexId]): boolean; - - setEdgeWeight(srcOrId: V | VertexId, destOrId: V | VertexId, weight: number): boolean; - - getMinPathBetween(v1: V | VertexId, v2: V | VertexId, isWeight?: boolean): V[] | null; - - getNeighbors(vertexOrId: V | VertexId): V[]; -} \ No newline at end of file diff --git a/src/data-structures/types/avl-tree.ts b/src/data-structures/types/avl-tree.ts index 3f0c154..c3511d5 100644 --- a/src/data-structures/types/avl-tree.ts +++ b/src/data-structures/types/avl-tree.ts @@ -1,6 +1,6 @@ import {AVLTreeNode} from '../binary-tree'; -export interface AVLTreeDeleted { +export type AVLTreeDeleted = { deleted: AVLTreeNode | null; needBalanced: AVLTreeNode | null; } \ No newline at end of file diff --git a/src/data-structures/types/directed-graph.ts b/src/data-structures/types/directed-graph.ts index aea9be5..971b88d 100644 --- a/src/data-structures/types/directed-graph.ts +++ b/src/data-structures/types/directed-graph.ts @@ -1,18 +1,8 @@ -import {VertexId} from './abstract-graph'; - -export interface IDirectedGraph { - incomingEdgesOf(vertex: V): E[]; - - outgoingEdgesOf(vertex: V): E[]; - - inDegreeOf(vertexOrId: V | VertexId): number; - - outDegreeOf(vertexOrId: V | VertexId): number; - - getEdgeSrc(e: E): V | null; - - getEdgeDest(e: E): V | null; -} - // 0 means unknown, 1 means visiting, 2 means visited; -export type TopologicalStatus = 0 | 1 | 2; \ No newline at end of file +export type TopologicalStatus = 0 | 1 | 2; + +export enum TopologicalProperty { + VAL = 'VAL', + NODE = 'NODE', + ID = 'ID', +} \ No newline at end of file diff --git a/src/data-structures/types/heap.ts b/src/data-structures/types/heap.ts index 65cf926..389697a 100644 --- a/src/data-structures/types/heap.ts +++ b/src/data-structures/types/heap.ts @@ -1,4 +1,4 @@ -export interface HeapOptions { +export type HeapOptions = { priority?: (element: T) => number; // TODO there is an idea that support chaining which is for conveniently using the data structure // isChaining? : boolean diff --git a/src/data-structures/types/index.ts b/src/data-structures/types/index.ts index 7b19e5d..3a58bb1 100644 --- a/src/data-structures/types/index.ts +++ b/src/data-structures/types/index.ts @@ -9,5 +9,4 @@ export * from './priority-queue'; export * from './heap'; export * from './singly-linked-list'; export * from './doubly-linked-list'; -export * from './navigator'; -export * from '../../utils/types/utils'; \ No newline at end of file +export * from './navigator'; \ No newline at end of file diff --git a/src/data-structures/types/navigator.ts b/src/data-structures/types/navigator.ts index 0676678..231f6b9 100644 --- a/src/data-structures/types/navigator.ts +++ b/src/data-structures/types/navigator.ts @@ -1,7 +1,7 @@ export type Direction = 'up' | 'right' | 'down' | 'left'; export type Turning = { [key in Direction]: Direction }; -export interface NavigatorParams { +export type NavigatorParams = { matrix: T[][], turning: Turning, onMove: (cur: [number, number]) => void diff --git a/src/data-structures/types/priority-queue.ts b/src/data-structures/types/priority-queue.ts index 48e398a..3fe3b75 100644 --- a/src/data-structures/types/priority-queue.ts +++ b/src/data-structures/types/priority-queue.ts @@ -1,6 +1,6 @@ export type PriorityQueueComparator = (a: T, b: T) => number; -export interface PriorityQueueOptions { +export type PriorityQueueOptions = { nodes?: T[]; isFix?: boolean; comparator: PriorityQueueComparator; diff --git a/tests/unit/data-structures/graph/directed-graph.test.ts b/tests/unit/data-structures/graph/directed-graph.test.ts index b364c8c..83f4cd6 100644 --- a/tests/unit/data-structures/graph/directed-graph.test.ts +++ b/tests/unit/data-structures/graph/directed-graph.test.ts @@ -59,7 +59,7 @@ describe('DirectedGraph Operation Test', () => { graph.addEdge(edgeBC); const topologicalOrder = graph.topologicalSort(); - if (topologicalOrder) expect(topologicalOrder.map(v => v.id)).toEqual(['A', 'B', 'C']); + if (topologicalOrder) expect(topologicalOrder.map(v => v instanceof DirectedVertex ? v.id: '')).toEqual(['A', 'B', 'C']); }); }); @@ -168,9 +168,9 @@ describe('DirectedGraph Test2 operations', () => { expect(sorted).toBeInstanceOf(Array); if (sorted && sorted.length > 0) { expect(sorted.length).toBe(9); - expect(sorted[0].data).toBe('data9'); - expect(sorted[3].data).toBe('data6'); - expect(sorted[8].id).toBe(1); + if (sorted[0] instanceof MyVertex) expect(sorted[0].data).toBe('data9'); + sorted[3] instanceof MyVertex && expect(sorted[3].data).toBe('data6'); + sorted[8] instanceof MyVertex && expect(sorted[8].id).toBe(1); } });