refactor: Define a generic CRUD enum to represent operational states.

This commit is contained in:
Revone 2024-01-12 11:07:33 +08:00
parent c812f43a04
commit e1631a8e3e
4 changed files with 15 additions and 13 deletions

View file

@ -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

View file

@ -107,7 +107,6 @@ In Java.utils, you need to memorize a table for all sequential data structures(Q
<table>
<thead>
<tr>
<th>Operation</th>
<th>Java ArrayList</th>
<th>Java Queue</th>
<th>Java ArrayDeque</th>
@ -116,28 +115,24 @@ In Java.utils, you need to memorize a table for all sequential data structures(Q
</thead>
<tbody>
<tr>
<td>push</td>
<td>add</td>
<td>offer</td>
<td>push</td>
<td>push</td>
</tr>
<tr>
<td>pop</td>
<td>remove</td>
<td>poll</td>
<td>removeLast</td>
<td>removeLast</td>
</tr>
<tr>
<td>shift</td>
<td>remove</td>
<td>poll</td>
<td>removeFirst</td>
<td>removeFirst</td>
</tr>
<tr>
<td>unshift</td>
<td>add(0, element)</td>
<td>offerFirst</td>
<td>unshift</td>

View file

@ -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;
}
/**

View file

@ -63,3 +63,10 @@ export type BTNodePureExemplar<K, V, N> = [K, V | undefined] | BTNodePureKeyOrNo
export type BSTNKeyOrNode<K, N> = K | undefined | N;
export type BinaryTreeDeleteResult<N> = { deleted: N | null | undefined; needBalanced: N | null | undefined };
export enum CRUD {
CREATED = 'CREATED',
READ = 'READ',
UPDATED = 'UPDATED',
DELETED = 'DELETED'
}