From e1631a8e3e686c53ca1aeb4bedc90dcd0da2fb73 Mon Sep 17 00:00:00 2001 From: Revone Date: Fri, 12 Jan 2024 11:07:33 +0800 Subject: [PATCH] refactor: Define a generic CRUD enum to represent operational states. --- CHANGELOG.md | 2 +- README.md | 5 ----- src/data-structures/binary-tree/rb-tree.ts | 14 +++++++------- src/types/common.ts | 7 +++++++ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea9ffa6..6ff851e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. - [Semantic Versioning](https://semver.org/spec/v2.0.0.html) - [`auto-changelog`](https://github.com/CookPete/auto-changelog) -## [v1.50.5](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) +## [v1.50.6](https://github.com/zrwusa/data-structure-typed/compare/v1.35.0...main) (upcoming) ### Changes diff --git a/README.md b/README.md index 1b28403..a10fed8 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,6 @@ In Java.utils, you need to memorize a table for all sequential data structures(Q - @@ -116,28 +115,24 @@ In Java.utils, you need to memorize a table for all sequential data structures(Q - - - - diff --git a/src/data-structures/binary-tree/rb-tree.ts b/src/data-structures/binary-tree/rb-tree.ts index 8f1dcbe..bfb958f 100644 --- a/src/data-structures/binary-tree/rb-tree.ts +++ b/src/data-structures/binary-tree/rb-tree.ts @@ -7,7 +7,7 @@ import type { RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types'; -import { RBTNColor } from '../../types'; +import { CRUD, RBTNColor } from '../../types'; import { BST, BSTNode } from './bst'; import { IBinaryTree } from '../../interfaces'; @@ -208,7 +208,7 @@ export class RedBlackTree< * @returns a boolean value. */ override isRealNode(node: NODE | undefined): node is NODE { - if (node === this._SENTINEL || node === undefined) return false; + if (node === this.SENTINEL || node === undefined) return false; return node instanceof RedBlackTreeNode; } @@ -290,7 +290,7 @@ export class RedBlackTree< const insertStatus = this._insert(newNode); - if (insertStatus === 'inserted') { + if (insertStatus === CRUD.CREATED) { // Ensure the root is black if (this.isRealNode(this._root)) { this._root.color = RBTNColor.BLACK; @@ -299,7 +299,7 @@ export class RedBlackTree< } this._size++; return true; - } else return insertStatus === 'updated'; + } else return insertStatus === CRUD.UPDATED; } /** @@ -434,7 +434,7 @@ export class RedBlackTree< * node in the tree. * @returns {'inserted' | 'updated'} - The result of the insertion. */ - protected _insert(node: NODE): 'inserted' | 'updated' { + protected _insert(node: NODE): CRUD { let current = this.root; let parent: NODE | undefined = undefined; @@ -446,7 +446,7 @@ export class RedBlackTree< current = current.right ?? this.SENTINEL; } else { this._replaceNode(current, node); - return 'updated'; + return CRUD.UPDATED; } } @@ -465,7 +465,7 @@ export class RedBlackTree< node.color = RBTNColor.RED; this._insertFixup(node); - return 'inserted'; + return CRUD.CREATED; } /** diff --git a/src/types/common.ts b/src/types/common.ts index 2b42235..79a4e90 100644 --- a/src/types/common.ts +++ b/src/types/common.ts @@ -63,3 +63,10 @@ export type BTNodePureExemplar = [K, V | undefined] | BTNodePureKeyOrNo export type BSTNKeyOrNode = K | undefined | N; export type BinaryTreeDeleteResult = { deleted: N | null | undefined; needBalanced: N | null | undefined }; + +export enum CRUD { + CREATED = 'CREATED', + READ = 'READ', + UPDATED = 'UPDATED', + DELETED = 'DELETED' +} \ No newline at end of file
Operation Java ArrayList Java Queue Java ArrayDeque
push add offer push push
pop remove poll removeLast removeLast
shift remove poll removeFirst removeFirst
unshift add(0, element) offerFirst unshift