mirror of
https://github.com/zrwusa/data-structure-typed.git
synced 2024-11-23 12:54:04 +00:00
[binary-tree] unnecessary geters and setters removed
This commit is contained in:
parent
3bffa7320c
commit
ed1ecf541f
50
package-lock.json
generated
50
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.34.2",
|
||||
"version": "1.34.8",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "data-structure-typed",
|
||||
"version": "1.34.2",
|
||||
"version": "1.34.8",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/benchmark": "^2.1.3",
|
||||
|
@ -15,17 +15,17 @@
|
|||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"avl-tree-typed": "^1.34.1",
|
||||
"avl-tree-typed": "^1.34.8",
|
||||
"benchmark": "^2.1.4",
|
||||
"binary-tree-typed": "^1.34.1",
|
||||
"bst-typed": "^1.34.1",
|
||||
"binary-tree-typed": "^1.34.8",
|
||||
"bst-typed": "^1.34.8",
|
||||
"dependency-cruiser": "^14.1.0",
|
||||
"eslint": "^8.50.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-import": "^2.28.1",
|
||||
"heap-typed": "^1.34.1",
|
||||
"heap-typed": "^1.34.8",
|
||||
"istanbul-badges-readme": "^1.8.5",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.0.3",
|
||||
|
@ -2393,12 +2393,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/avl-tree-typed": {
|
||||
"version": "1.34.1",
|
||||
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.34.1.tgz",
|
||||
"integrity": "sha512-5SgeKmOZivy6krk/KoKswaQVdaEXSZCkGWq+5yjfnszYKpwZzkijN/CAw1r/S1keF7L9cNA+wUMaE6XBE/KZOQ==",
|
||||
"version": "1.34.8",
|
||||
"resolved": "https://registry.npmjs.org/avl-tree-typed/-/avl-tree-typed-1.34.8.tgz",
|
||||
"integrity": "sha512-5f4zkcP3qspipM0OJ1VY5t/77wbWHOto5QgUCX+YuXMlpkMwquAiTg7hSqOQJaLFCmol28BkfdxJfzVB0T08Kg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.34.1"
|
||||
"data-structure-typed": "^1.34.8"
|
||||
}
|
||||
},
|
||||
"node_modules/babel-jest": {
|
||||
|
@ -2583,12 +2583,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/binary-tree-typed": {
|
||||
"version": "1.34.1",
|
||||
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.34.1.tgz",
|
||||
"integrity": "sha512-ar2ROpvmPVN9ZlEP094lkV0mZdP+8cqWjUaLzQHuMk6jXUSfo3i95iIFJCVTqX0v3tZpCH51OfQoJxBX130eQQ==",
|
||||
"version": "1.34.8",
|
||||
"resolved": "https://registry.npmjs.org/binary-tree-typed/-/binary-tree-typed-1.34.8.tgz",
|
||||
"integrity": "sha512-sDsvV0tvs1tullwIoA6/gak+9vHQAOnXQvbNn9SyMBp2IMeIhNRC9SpHKPU75AgtBiSqVJ3qxMYE6uw360UzcQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.34.1"
|
||||
"data-structure-typed": "^1.34.8"
|
||||
}
|
||||
},
|
||||
"node_modules/brace-expansion": {
|
||||
|
@ -2667,12 +2667,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/bst-typed": {
|
||||
"version": "1.34.1",
|
||||
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.34.1.tgz",
|
||||
"integrity": "sha512-z5K3SXz71CufxYEh3ztzzfubfsJSFQW6J6p2zJL8EZrbLACFOmP6/B/3sFFsWzHiFuTrn/imWRycYQbjDR/YPw==",
|
||||
"version": "1.34.8",
|
||||
"resolved": "https://registry.npmjs.org/bst-typed/-/bst-typed-1.34.8.tgz",
|
||||
"integrity": "sha512-sEqjh8ouZaCD5Tt6IicNTNaeYzpSdeaVRBiHXtYdla5YAMRpiBepw+O1gfwOpKlLTNqVY/wEt2zGgtWmfLKB+Q==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.34.1"
|
||||
"data-structure-typed": "^1.34.8"
|
||||
}
|
||||
},
|
||||
"node_modules/buffer-from": {
|
||||
|
@ -3024,9 +3024,9 @@
|
|||
}
|
||||
},
|
||||
"node_modules/data-structure-typed": {
|
||||
"version": "1.34.1",
|
||||
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.34.1.tgz",
|
||||
"integrity": "sha512-/qNoKl1/cPpZUR7V8qMNS+WOqLIz4RVEuVZeOilJGCfWerbx/bPEtQsqEKMXLwclQJ4KLKSAoblJNPCIEuUYRw==",
|
||||
"version": "1.34.8",
|
||||
"resolved": "https://registry.npmjs.org/data-structure-typed/-/data-structure-typed-1.34.8.tgz",
|
||||
"integrity": "sha512-8pQJxRvlOHnZ6pwgk2BZWKhd8hxkV+waJgX0SIS3tSJv00WB0vRfg610pLWuBxBdnmnFvFIX6HGFUUH6ERRDIw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/debug": {
|
||||
|
@ -4356,12 +4356,12 @@
|
|||
}
|
||||
},
|
||||
"node_modules/heap-typed": {
|
||||
"version": "1.34.1",
|
||||
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.34.1.tgz",
|
||||
"integrity": "sha512-yXOmzX/6xcX3CdIoM/RmaPpzz4oftZtSfOU8ru21+bZUzCYiRs5cY7oXGXux9LmUgowrxQgvNveRD1AZYPzXhQ==",
|
||||
"version": "1.34.8",
|
||||
"resolved": "https://registry.npmjs.org/heap-typed/-/heap-typed-1.34.8.tgz",
|
||||
"integrity": "sha512-jgd/JSdk3qZKI1luGL+O48plcIAxv6ZGPbb4bA0iVOs1jBXAZLQam92Yc91DkU2a8tSW+5c8sBIy3CkGE3ZRrA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"data-structure-typed": "^1.34.1"
|
||||
"data-structure-typed": "^1.34.8"
|
||||
}
|
||||
},
|
||||
"node_modules/html-escaper": {
|
||||
|
|
10
package.json
10
package.json
|
@ -23,7 +23,7 @@
|
|||
"fix:test": "npm run lint:test && npm run format:test",
|
||||
"fix": "npm run fix:src && npm run fix:test",
|
||||
"update:test-deps": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed --save-dev",
|
||||
"update-all:test-deps": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
|
||||
"install:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
|
||||
"test": "jest",
|
||||
"check:deps": "dependency-cruiser src",
|
||||
"changelog": "auto-changelog",
|
||||
|
@ -50,17 +50,17 @@
|
|||
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
||||
"@typescript-eslint/parser": "^6.7.4",
|
||||
"auto-changelog": "^2.4.0",
|
||||
"avl-tree-typed": "^1.34.1",
|
||||
"avl-tree-typed": "^1.34.8",
|
||||
"benchmark": "^2.1.4",
|
||||
"binary-tree-typed": "^1.34.1",
|
||||
"bst-typed": "^1.34.1",
|
||||
"binary-tree-typed": "^1.34.8",
|
||||
"bst-typed": "^1.34.8",
|
||||
"dependency-cruiser": "^14.1.0",
|
||||
"eslint": "^8.50.0",
|
||||
"eslint-config-prettier": "^9.0.0",
|
||||
"eslint-import-resolver-alias": "^1.1.2",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
"eslint-plugin-import": "^2.28.1",
|
||||
"heap-typed": "^1.34.1",
|
||||
"heap-typed": "^1.34.8",
|
||||
"istanbul-badges-readme": "^1.8.5",
|
||||
"jest": "^29.7.0",
|
||||
"prettier": "^3.0.3",
|
||||
|
|
|
@ -26,36 +26,20 @@ export abstract class AbstractBinaryTreeNode<
|
|||
> implements IAbstractBinaryTreeNode<V, NEIGHBOR>
|
||||
{
|
||||
/**
|
||||
* The constructor function initializes a BinaryTreeNode object with an key and an optional value.
|
||||
* The constructor function initializes a BinaryTreeNode object with a key and an optional value.
|
||||
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
||||
* of the binary tree node. It is used to distinguish one node from another in the binary tree.
|
||||
* @param {V} [val] - The "val" parameter is an optional parameter of type V. It represents the value that will be
|
||||
* stored in the binary tree node. If no value is provided, it will be set to undefined.
|
||||
*/
|
||||
protected constructor(key: BinaryTreeNodeKey, val?: V) {
|
||||
this._key = key;
|
||||
this._val = val;
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
private _key: BinaryTreeNodeKey;
|
||||
key: BinaryTreeNodeKey;
|
||||
|
||||
get key(): BinaryTreeNodeKey {
|
||||
return this._key;
|
||||
}
|
||||
|
||||
set key(v: BinaryTreeNodeKey) {
|
||||
this._key = v;
|
||||
}
|
||||
|
||||
private _val: V | undefined;
|
||||
|
||||
get val(): V | undefined {
|
||||
return this._val;
|
||||
}
|
||||
|
||||
set val(value: V | undefined) {
|
||||
this._val = value;
|
||||
}
|
||||
val: V | undefined;
|
||||
|
||||
private _left: NEIGHBOR | null | undefined;
|
||||
|
||||
|
@ -83,25 +67,7 @@ export abstract class AbstractBinaryTreeNode<
|
|||
this._right = v;
|
||||
}
|
||||
|
||||
private _parent: NEIGHBOR | null | undefined;
|
||||
|
||||
get parent(): NEIGHBOR | null | undefined {
|
||||
return this._parent;
|
||||
}
|
||||
|
||||
set parent(v: NEIGHBOR | null | undefined) {
|
||||
this._parent = v;
|
||||
}
|
||||
|
||||
private _height = 0;
|
||||
|
||||
get height(): number {
|
||||
return this._height;
|
||||
}
|
||||
|
||||
set height(v: number) {
|
||||
this._height = v;
|
||||
}
|
||||
parent: NEIGHBOR | null | undefined;
|
||||
|
||||
/**
|
||||
* The function determines the position of a node in a family tree structure.
|
||||
|
@ -169,23 +135,11 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
return this._loopType;
|
||||
}
|
||||
|
||||
private _visitedKey: BinaryTreeNodeKey[] = [];
|
||||
visitedKey: BinaryTreeNodeKey[] = [];
|
||||
|
||||
get visitedKey(): BinaryTreeNodeKey[] {
|
||||
return this._visitedKey;
|
||||
}
|
||||
visitedVal: N['val'][] = [];
|
||||
|
||||
private _visitedVal: N['val'][] = [];
|
||||
|
||||
get visitedVal(): N['val'][] {
|
||||
return this._visitedVal;
|
||||
}
|
||||
|
||||
private _visitedNode: N[] = [];
|
||||
|
||||
get visitedNode(): N[] {
|
||||
return this._visitedNode;
|
||||
}
|
||||
visitedNode: N[] = [];
|
||||
|
||||
abstract createNode(key: BinaryTreeNodeKey, val?: N['val']): N | null;
|
||||
|
||||
|
@ -197,19 +151,15 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* @returns The `destNode` is being returned.
|
||||
*/
|
||||
swapLocation(srcNode: N, destNode: N): N {
|
||||
const {key, val, height} = destNode;
|
||||
const {key, val} = destNode;
|
||||
const tempNode = this.createNode(key, val);
|
||||
|
||||
if (tempNode) {
|
||||
tempNode.height = height;
|
||||
|
||||
destNode.key = srcNode.key;
|
||||
destNode.val = srcNode.val;
|
||||
destNode.height = srcNode.height;
|
||||
|
||||
srcNode.key = tempNode.key;
|
||||
srcNode.val = tempNode.val;
|
||||
srcNode.height = tempNode.height;
|
||||
}
|
||||
|
||||
return destNode;
|
||||
|
@ -1453,30 +1403,6 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
this._loopType = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function sets the value of the `_visitedKey` property in a protected manner.
|
||||
* @param {BinaryTreeNodeKey[]} value - value is an array of BinaryTreeNodeKey values.
|
||||
*/
|
||||
protected _setVisitedKey(value: BinaryTreeNodeKey[]) {
|
||||
this._visitedKey = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function sets the value of the "_visitedVal" property to the given array.
|
||||
* @param value - An array of type N.
|
||||
*/
|
||||
protected _setVisitedVal(value: Array<N>) {
|
||||
this._visitedVal = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function sets the value of the _visitedNode property.
|
||||
* @param {N[]} value - N[] is an array of elements of type N.
|
||||
*/
|
||||
protected _setVisitedNode(value: N[]) {
|
||||
this._visitedNode = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function sets the root property of an object to a given value, and if the value is not null, it also sets the
|
||||
* parent property of the value to undefined.
|
||||
|
@ -1502,9 +1428,9 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
* properties.
|
||||
*/
|
||||
protected _clearResults() {
|
||||
this._visitedKey = [];
|
||||
this._visitedVal = [];
|
||||
this._visitedNode = [];
|
||||
this.visitedKey = [];
|
||||
this.visitedVal = [];
|
||||
this.visitedNode = [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1563,22 +1489,22 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
|
||||
switch (nodeOrPropertyName) {
|
||||
case 'key':
|
||||
this._visitedKey.push(node.key);
|
||||
this.visitedKey.push(node.key);
|
||||
break;
|
||||
case 'val':
|
||||
this._visitedVal.push(node.val);
|
||||
this.visitedVal.push(node.val);
|
||||
break;
|
||||
case 'node':
|
||||
this._visitedNode.push(node);
|
||||
this.visitedNode.push(node);
|
||||
break;
|
||||
default:
|
||||
this._visitedKey.push(node.key);
|
||||
this.visitedKey.push(node.key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The time complexity of Morris traversal is O(n), it's may slower than others
|
||||
* The time complexity of Morris traversal is O(n), it may slower than others
|
||||
* The space complexity Morris traversal is O(1) because no using stack
|
||||
*/
|
||||
|
||||
|
@ -1594,13 +1520,13 @@ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val
|
|||
|
||||
switch (nodeOrPropertyName) {
|
||||
case 'key':
|
||||
return this._visitedKey;
|
||||
return this.visitedKey;
|
||||
case 'val':
|
||||
return this._visitedVal;
|
||||
return this.visitedVal;
|
||||
case 'node':
|
||||
return this._visitedNode;
|
||||
return this.visitedNode;
|
||||
default:
|
||||
return this._visitedKey;
|
||||
return this.visitedKey;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,8 +13,11 @@ export class AVLTreeNode<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AV
|
|||
extends BSTNode<V, NEIGHBOR>
|
||||
implements IAVLTreeNode<V, NEIGHBOR>
|
||||
{
|
||||
height: number;
|
||||
|
||||
constructor(key: BinaryTreeNodeKey, val?: V) {
|
||||
super(key, val);
|
||||
this.height = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,6 +32,32 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|||
super(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* The `swapLocation` function swaps the location of two nodes in a binary tree.
|
||||
* @param {N} srcNode - The source node that you want to swap with the destination node.
|
||||
* @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
|
||||
* be swapped to.
|
||||
* @returns The `destNode` is being returned.
|
||||
*/
|
||||
override swapLocation(srcNode: N, destNode: N): N {
|
||||
const {key, val, height} = destNode;
|
||||
const tempNode = this.createNode(key, val);
|
||||
|
||||
if (tempNode) {
|
||||
tempNode.height = height;
|
||||
|
||||
destNode.key = srcNode.key;
|
||||
destNode.val = srcNode.val;
|
||||
destNode.height = srcNode.height;
|
||||
|
||||
srcNode.key = tempNode.key;
|
||||
srcNode.val = tempNode.val;
|
||||
srcNode.height = tempNode.height;
|
||||
}
|
||||
|
||||
return destNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* The function creates a new AVL tree node with the given key and value.
|
||||
* @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
|
||||
|
|
|
@ -15,7 +15,7 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|||
implements ITreeMultisetNode<V, NEIGHBOR>
|
||||
{
|
||||
/**
|
||||
* The constructor function initializes a BinaryTreeNode object with an key, value, and count.
|
||||
* The constructor function initializes a BinaryTreeNode object with a key, value, and count.
|
||||
* @param {BinaryTreeNodeKey} key - The `key` parameter is of type `BinaryTreeNodeKey` and represents the unique identifier
|
||||
* of the binary tree node.
|
||||
* @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
|
||||
|
@ -26,18 +26,10 @@ export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIG
|
|||
*/
|
||||
constructor(key: BinaryTreeNodeKey, val?: V, count = 1) {
|
||||
super(key, val);
|
||||
this._count = count;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
private _count: number;
|
||||
|
||||
get count(): number {
|
||||
return this._count;
|
||||
}
|
||||
|
||||
set count(v: number) {
|
||||
this._count = v;
|
||||
}
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -54,7 +46,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
|
|||
* TreeMultiset.
|
||||
*/
|
||||
constructor(options?: TreeMultisetOptions) {
|
||||
super({...options});
|
||||
super(options);
|
||||
}
|
||||
|
||||
private _count = 0;
|
||||
|
|
|
@ -12,13 +12,9 @@ import {
|
|||
import {AbstractBinaryTreeNode} from '../data-structures';
|
||||
|
||||
export interface IAbstractBinaryTreeNode<T, NEIGHBOR extends IAbstractBinaryTreeNode<T, NEIGHBOR>> {
|
||||
get key(): BinaryTreeNodeKey;
|
||||
key: BinaryTreeNodeKey;
|
||||
|
||||
set key(v: BinaryTreeNodeKey);
|
||||
|
||||
get val(): T | undefined;
|
||||
|
||||
set val(v: T | undefined);
|
||||
val: T | undefined;
|
||||
|
||||
get left(): NEIGHBOR | null | undefined;
|
||||
|
||||
|
@ -28,15 +24,9 @@ export interface IAbstractBinaryTreeNode<T, NEIGHBOR extends IAbstractBinaryTree
|
|||
|
||||
set right(v: NEIGHBOR | null | undefined);
|
||||
|
||||
get parent(): NEIGHBOR | null | undefined;
|
||||
|
||||
set parent(v: NEIGHBOR | null | undefined);
|
||||
parent: NEIGHBOR | null | undefined;
|
||||
|
||||
get familyPosition(): FamilyPosition;
|
||||
|
||||
get height(): number;
|
||||
|
||||
set height(v: number);
|
||||
}
|
||||
|
||||
export interface IAbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N>> {
|
||||
|
|
|
@ -2,7 +2,9 @@ import {AVLTreeNode} from '../data-structures';
|
|||
import {IBST, IBSTNode} from './bst';
|
||||
import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types';
|
||||
|
||||
export type IAVLTreeNode<T, NEIGHBOR extends IAVLTreeNode<T, NEIGHBOR>> = IBSTNode<T, NEIGHBOR>;
|
||||
export interface IAVLTreeNode<T, NEIGHBOR extends IAVLTreeNode<T, NEIGHBOR>> extends IBSTNode<T, NEIGHBOR> {
|
||||
height: number;
|
||||
}
|
||||
|
||||
export interface IAVLTree<N extends AVLTreeNode<N['val'], N>> extends IBST<N> {
|
||||
add(key: BinaryTreeNodeKey, val?: N['val'] | null): N | null | undefined;
|
||||
|
|
|
@ -2,7 +2,7 @@ import {BSTNode} from '../data-structures';
|
|||
import {IBinaryTree, IBinaryTreeNode} from './binary-tree';
|
||||
import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodePropertyName} from '../types';
|
||||
|
||||
export type IBSTNode<T, NEIGHBOR extends IBSTNode<T, NEIGHBOR>> = IBinaryTreeNode<T, NEIGHBOR>;
|
||||
export interface IBSTNode<T, NEIGHBOR extends IBSTNode<T, NEIGHBOR>> extends IBinaryTreeNode<T, NEIGHBOR> {}
|
||||
|
||||
export interface IBST<N extends BSTNode<N['val'], N>> extends IBinaryTree<N> {
|
||||
createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
|
||||
|
|
|
@ -12,15 +12,15 @@ describe('AVL Tree Test', () => {
|
|||
expect(node6 && tree.getHeight(node6)).toBe(3);
|
||||
expect(node6 && tree.getDepth(node6)).toBe(1);
|
||||
|
||||
const getNodeById = tree.get(10, 'id');
|
||||
expect(getNodeById?.id).toBe(10);
|
||||
const getNodeById = tree.get(10, 'key');
|
||||
expect(getNodeById?.key).toBe(10);
|
||||
|
||||
const getMinNodeByRoot = tree.getLeftMost();
|
||||
expect(getMinNodeByRoot?.id).toBe(1);
|
||||
expect(getMinNodeByRoot?.key).toBe(1);
|
||||
|
||||
const node15 = tree.get(15);
|
||||
const getMinNodeBySpecificNode = node15 && tree.getLeftMost(node15);
|
||||
expect(getMinNodeBySpecificNode?.id).toBe(12);
|
||||
expect(getMinNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
const subTreeSum = node15 && tree.subTreeSum(node15);
|
||||
expect(subTreeSum).toBe(70);
|
||||
|
@ -32,65 +32,65 @@ describe('AVL Tree Test', () => {
|
|||
expect(node15?.val).toBe(15);
|
||||
|
||||
const dfs = tree.DFS('in', 'node');
|
||||
expect(dfs[0].id).toBe(1);
|
||||
expect(dfs[dfs.length - 1].id).toBe(16);
|
||||
expect(dfs[0].key).toBe(1);
|
||||
expect(dfs[dfs.length - 1].key).toBe(16);
|
||||
|
||||
tree.perfectlyBalance();
|
||||
const bfs = tree.BFS('node');
|
||||
expect(tree.isPerfectlyBalanced()).toBe(true);
|
||||
expect(bfs[0].id).toBe(8);
|
||||
expect(bfs[bfs.length - 1].id).toBe(16);
|
||||
expect(bfs[0].key).toBe(8);
|
||||
expect(bfs[bfs.length - 1].key).toBe(16);
|
||||
|
||||
expect(tree.remove(11)[0].deleted?.id).toBe(11);
|
||||
expect(tree.remove(11)[0].deleted?.key).toBe(11);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(node15 && tree.getHeight(node15)).toBe(2);
|
||||
|
||||
expect(tree.remove(1)[0].deleted?.id).toBe(1);
|
||||
expect(tree.remove(1)[0].deleted?.key).toBe(1);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(4);
|
||||
|
||||
expect(tree.remove(4)[0].deleted?.id).toBe(4);
|
||||
expect(tree.remove(4)[0].deleted?.key).toBe(4);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(4);
|
||||
|
||||
expect(tree.remove(10)[0].deleted?.id).toBe(10);
|
||||
expect(tree.remove(10)[0].deleted?.key).toBe(10);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(15)[0].deleted?.id).toBe(15);
|
||||
expect(tree.remove(15)[0].deleted?.key).toBe(15);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(5)[0].deleted?.id).toBe(5);
|
||||
expect(tree.remove(5)[0].deleted?.key).toBe(5);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(13)[0].deleted?.id).toBe(13);
|
||||
expect(tree.remove(13)[0].deleted?.key).toBe(13);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(3)[0].deleted?.id).toBe(3);
|
||||
expect(tree.remove(3)[0].deleted?.key).toBe(3);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(8)[0].deleted?.id).toBe(8);
|
||||
expect(tree.remove(8)[0].deleted?.key).toBe(8);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(3);
|
||||
|
||||
expect(tree.remove(6)[0].deleted?.id).toBe(6);
|
||||
expect(tree.remove(6)[0].deleted?.key).toBe(6);
|
||||
expect(tree.remove(6).length).toBe(0);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(2);
|
||||
|
||||
expect(tree.remove(7)[0].deleted?.id).toBe(7);
|
||||
expect(tree.remove(7)[0].deleted?.key).toBe(7);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(2);
|
||||
|
||||
expect(tree.remove(9)[0].deleted?.id).toBe(9);
|
||||
expect(tree.remove(9)[0].deleted?.key).toBe(9);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(2);
|
||||
expect(tree.remove(14)[0].deleted?.id).toBe(14);
|
||||
expect(tree.remove(14)[0].deleted?.key).toBe(14);
|
||||
expect(tree.isAVLBalanced()).toBe(true);
|
||||
expect(tree.getHeight()).toBe(1);
|
||||
|
||||
|
@ -101,8 +101,8 @@ describe('AVL Tree Test', () => {
|
|||
expect(lastBFSIds[2]).toBe(16);
|
||||
|
||||
const lastBFSNodes = tree.BFS('node');
|
||||
expect(lastBFSNodes[0].id).toBe(12);
|
||||
expect(lastBFSNodes[1].id).toBe(2);
|
||||
expect(lastBFSNodes[2].id).toBe(16);
|
||||
expect(lastBFSNodes[0].key).toBe(12);
|
||||
expect(lastBFSNodes[1].key).toBe(2);
|
||||
expect(lastBFSNodes[2].key).toBe(16);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -10,7 +10,7 @@ describe('Individual package BST operations test', () => {
|
|||
bst.addMany(idsOrVals, idsOrVals);
|
||||
expect(bst.root).toBeInstanceOf(BSTNode);
|
||||
|
||||
if (bst.root) expect(bst.root.id).toBe(11);
|
||||
if (bst.root) expect(bst.root.key).toBe(11);
|
||||
|
||||
expect(bst.size).toBe(16);
|
||||
|
||||
|
@ -21,17 +21,17 @@ describe('Individual package BST operations test', () => {
|
|||
expect(node6 && bst.getDepth(6)).toBe(3);
|
||||
|
||||
const nodeId10 = bst.get(10);
|
||||
expect(nodeId10?.id).toBe(10);
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = bst.get(9, 'val');
|
||||
expect(nodeVal9?.id).toBe(9);
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const leftMost = bst.getLeftMost();
|
||||
expect(leftMost?.id).toBe(1);
|
||||
expect(leftMost?.key).toBe(1);
|
||||
|
||||
const node15 = bst.get(15);
|
||||
const minNodeBySpecificNode = node15 && bst.getLeftMost(node15);
|
||||
expect(minNodeBySpecificNode?.id).toBe(12);
|
||||
expect(minNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
const subTreeSum = node15 && bst.subTreeSum(15);
|
||||
expect(subTreeSum).toBe(70);
|
||||
|
@ -45,22 +45,22 @@ describe('Individual package BST operations test', () => {
|
|||
expect(node11).toBeInstanceOf(BSTNode);
|
||||
|
||||
const dfsInorderNodes = bst.DFS('in', 'node');
|
||||
expect(dfsInorderNodes[0].id).toBe(1);
|
||||
expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16);
|
||||
expect(dfsInorderNodes[0].key).toBe(1);
|
||||
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
||||
|
||||
bst.perfectlyBalance();
|
||||
expect(bst.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced = bst.BFS('node');
|
||||
expect(bfsNodesAfterBalanced[0].id).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16);
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
||||
const removed11 = bst.remove(11);
|
||||
expect(removed11).toBeInstanceOf(Array);
|
||||
expect(removed11[0]).toBeDefined();
|
||||
expect(removed11[0].deleted).toBeDefined();
|
||||
|
||||
if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
|
||||
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
||||
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
|
||||
|
@ -70,7 +70,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed1).toBeInstanceOf(Array);
|
||||
expect(removed1[0]).toBeDefined();
|
||||
expect(removed1[0].deleted).toBeDefined();
|
||||
if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
|
||||
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
||||
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
|
||||
|
@ -80,7 +80,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed4).toBeInstanceOf(Array);
|
||||
expect(removed4[0]).toBeDefined();
|
||||
expect(removed4[0].deleted).toBeDefined();
|
||||
if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4);
|
||||
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(4);
|
||||
|
||||
|
@ -88,7 +88,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed10).toBeInstanceOf(Array);
|
||||
expect(removed10[0]).toBeDefined();
|
||||
expect(removed10[0].deleted).toBeDefined();
|
||||
if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10);
|
||||
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(4);
|
||||
|
||||
|
@ -96,7 +96,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed15).toBeInstanceOf(Array);
|
||||
expect(removed15[0]).toBeDefined();
|
||||
expect(removed15[0].deleted).toBeDefined();
|
||||
if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15);
|
||||
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
||||
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
@ -105,7 +105,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed5).toBeInstanceOf(Array);
|
||||
expect(removed5[0]).toBeDefined();
|
||||
expect(removed5[0].deleted).toBeDefined();
|
||||
if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5);
|
||||
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
||||
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
@ -114,7 +114,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed13).toBeInstanceOf(Array);
|
||||
expect(removed13[0]).toBeDefined();
|
||||
expect(removed13[0].deleted).toBeDefined();
|
||||
if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13);
|
||||
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
|
@ -122,7 +122,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed3).toBeInstanceOf(Array);
|
||||
expect(removed3[0]).toBeDefined();
|
||||
expect(removed3[0].deleted).toBeDefined();
|
||||
if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3);
|
||||
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
|
@ -130,7 +130,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed8).toBeInstanceOf(Array);
|
||||
expect(removed8[0]).toBeDefined();
|
||||
expect(removed8[0].deleted).toBeDefined();
|
||||
if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8);
|
||||
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
||||
expect(bst.isAVLBalanced()).toBe(true);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
|
@ -138,7 +138,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed6).toBeInstanceOf(Array);
|
||||
expect(removed6[0]).toBeDefined();
|
||||
expect(removed6[0].deleted).toBeDefined();
|
||||
if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6);
|
||||
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
||||
expect(bst.remove(6).length).toBe(0);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
@ -147,7 +147,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed7).toBeInstanceOf(Array);
|
||||
expect(removed7[0]).toBeDefined();
|
||||
expect(removed7[0].deleted).toBeDefined();
|
||||
if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7);
|
||||
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
|
@ -155,7 +155,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed9).toBeInstanceOf(Array);
|
||||
expect(removed9[0]).toBeDefined();
|
||||
expect(removed9[0].deleted).toBeDefined();
|
||||
if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9);
|
||||
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(3);
|
||||
|
||||
|
@ -163,7 +163,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed14).toBeInstanceOf(Array);
|
||||
expect(removed14[0]).toBeDefined();
|
||||
expect(removed14[0].deleted).toBeDefined();
|
||||
if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14);
|
||||
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
||||
expect(bst.isAVLBalanced()).toBe(false);
|
||||
expect(bst.getHeight()).toBe(2);
|
||||
|
||||
|
@ -175,41 +175,41 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes = bst.BFS('node');
|
||||
expect(bfsNodes[0].id).toBe(2);
|
||||
expect(bfsNodes[1].id).toBe(12);
|
||||
expect(bfsNodes[2].id).toBe(16);
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
expect(bfsNodes[2].key).toBe(16);
|
||||
});
|
||||
|
||||
it('should perform various operations on a Binary Search Tree with object values', () => {
|
||||
const objBST = new BST<BSTNode<{id: number; keyA: number}>>();
|
||||
const objBST = new BST<BSTNode<{key: number; keyA: number}>>();
|
||||
expect(objBST).toBeInstanceOf(BST);
|
||||
objBST.add(11, {id: 11, keyA: 11});
|
||||
objBST.add(3, {id: 3, keyA: 3});
|
||||
objBST.add(11, {key: 11, keyA: 11});
|
||||
objBST.add(3, {key: 3, keyA: 3});
|
||||
const values = [
|
||||
{id: 15, keyA: 15},
|
||||
{id: 1, keyA: 1},
|
||||
{id: 8, keyA: 8},
|
||||
{id: 13, keyA: 13},
|
||||
{id: 16, keyA: 16},
|
||||
{id: 2, keyA: 2},
|
||||
{id: 6, keyA: 6},
|
||||
{id: 9, keyA: 9},
|
||||
{id: 12, keyA: 12},
|
||||
{id: 14, keyA: 14},
|
||||
{id: 4, keyA: 4},
|
||||
{id: 7, keyA: 7},
|
||||
{id: 10, keyA: 10},
|
||||
{id: 5, keyA: 5}
|
||||
{key: 15, keyA: 15},
|
||||
{key: 1, keyA: 1},
|
||||
{key: 8, keyA: 8},
|
||||
{key: 13, keyA: 13},
|
||||
{key: 16, keyA: 16},
|
||||
{key: 2, keyA: 2},
|
||||
{key: 6, keyA: 6},
|
||||
{key: 9, keyA: 9},
|
||||
{key: 12, keyA: 12},
|
||||
{key: 14, keyA: 14},
|
||||
{key: 4, keyA: 4},
|
||||
{key: 7, keyA: 7},
|
||||
{key: 10, keyA: 10},
|
||||
{key: 5, keyA: 5}
|
||||
];
|
||||
|
||||
objBST.addMany(
|
||||
values.map(item => item.id),
|
||||
values.map(item => item.key),
|
||||
values
|
||||
);
|
||||
|
||||
expect(objBST.root).toBeInstanceOf(BSTNode);
|
||||
|
||||
if (objBST.root) expect(objBST.root.id).toBe(11);
|
||||
if (objBST.root) expect(objBST.root.key).toBe(11);
|
||||
|
||||
expect(objBST.has(6)).toBe(true);
|
||||
|
||||
|
@ -217,19 +217,19 @@ describe('Individual package BST operations test', () => {
|
|||
expect(node6 && objBST.getHeight(node6)).toBe(2);
|
||||
expect(node6 && objBST.getDepth(node6)).toBe(3);
|
||||
|
||||
const nodeId10 = objBST.get(10, 'id');
|
||||
expect(nodeId10?.id).toBe(10);
|
||||
const nodeId10 = objBST.get(10, 'key');
|
||||
expect(nodeId10?.key).toBe(10);
|
||||
|
||||
const nodeVal9 = objBST.get(9, 'id');
|
||||
expect(nodeVal9?.id).toBe(9);
|
||||
const nodeVal9 = objBST.get(9, 'key');
|
||||
expect(nodeVal9?.key).toBe(9);
|
||||
|
||||
const leftMost = objBST.getLeftMost();
|
||||
expect(leftMost?.id).toBe(1);
|
||||
expect(leftMost?.key).toBe(1);
|
||||
|
||||
const node15 = objBST.get(15);
|
||||
expect(node15?.val).toEqual({id: 15, keyA: 15});
|
||||
expect(node15?.val).toEqual({key: 15, keyA: 15});
|
||||
const minNodeBySpecificNode = node15 && objBST.getLeftMost(node15);
|
||||
expect(minNodeBySpecificNode?.id).toBe(12);
|
||||
expect(minNodeBySpecificNode?.key).toBe(12);
|
||||
|
||||
const subTreeSum = node15 && objBST.subTreeSum(node15);
|
||||
expect(subTreeSum).toBe(70);
|
||||
|
@ -243,22 +243,22 @@ describe('Individual package BST operations test', () => {
|
|||
expect(node11).toBeInstanceOf(BSTNode);
|
||||
|
||||
const dfsInorderNodes = objBST.DFS('in', 'node');
|
||||
expect(dfsInorderNodes[0].id).toBe(1);
|
||||
expect(dfsInorderNodes[dfsInorderNodes.length - 1].id).toBe(16);
|
||||
expect(dfsInorderNodes[0].key).toBe(1);
|
||||
expect(dfsInorderNodes[dfsInorderNodes.length - 1].key).toBe(16);
|
||||
|
||||
objBST.perfectlyBalance();
|
||||
expect(objBST.isPerfectlyBalanced()).toBe(true);
|
||||
|
||||
const bfsNodesAfterBalanced = objBST.BFS('node');
|
||||
expect(bfsNodesAfterBalanced[0].id).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].id).toBe(16);
|
||||
expect(bfsNodesAfterBalanced[0].key).toBe(8);
|
||||
expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
|
||||
|
||||
const removed11 = objBST.remove(11);
|
||||
expect(removed11).toBeInstanceOf(Array);
|
||||
expect(removed11[0]).toBeDefined();
|
||||
expect(removed11[0].deleted).toBeDefined();
|
||||
|
||||
if (removed11[0].deleted) expect(removed11[0].deleted.id).toBe(11);
|
||||
if (removed11[0].deleted) expect(removed11[0].deleted.key).toBe(11);
|
||||
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
|
||||
|
@ -268,7 +268,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed1).toBeInstanceOf(Array);
|
||||
expect(removed1[0]).toBeDefined();
|
||||
expect(removed1[0].deleted).toBeDefined();
|
||||
if (removed1[0].deleted) expect(removed1[0].deleted.id).toBe(1);
|
||||
if (removed1[0].deleted) expect(removed1[0].deleted.key).toBe(1);
|
||||
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
|
||||
|
@ -278,7 +278,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed4).toBeInstanceOf(Array);
|
||||
expect(removed4[0]).toBeDefined();
|
||||
expect(removed4[0].deleted).toBeDefined();
|
||||
if (removed4[0].deleted) expect(removed4[0].deleted.id).toBe(4);
|
||||
if (removed4[0].deleted) expect(removed4[0].deleted.key).toBe(4);
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(4);
|
||||
|
||||
|
@ -286,7 +286,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed10).toBeInstanceOf(Array);
|
||||
expect(removed10[0]).toBeDefined();
|
||||
expect(removed10[0].deleted).toBeDefined();
|
||||
if (removed10[0].deleted) expect(removed10[0].deleted.id).toBe(10);
|
||||
if (removed10[0].deleted) expect(removed10[0].deleted.key).toBe(10);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(4);
|
||||
|
||||
|
@ -294,7 +294,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed15).toBeInstanceOf(Array);
|
||||
expect(removed15[0]).toBeDefined();
|
||||
expect(removed15[0].deleted).toBeDefined();
|
||||
if (removed15[0].deleted) expect(removed15[0].deleted.id).toBe(15);
|
||||
if (removed15[0].deleted) expect(removed15[0].deleted.key).toBe(15);
|
||||
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
@ -303,7 +303,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed5).toBeInstanceOf(Array);
|
||||
expect(removed5[0]).toBeDefined();
|
||||
expect(removed5[0].deleted).toBeDefined();
|
||||
if (removed5[0].deleted) expect(removed5[0].deleted.id).toBe(5);
|
||||
if (removed5[0].deleted) expect(removed5[0].deleted.key).toBe(5);
|
||||
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
@ -312,7 +312,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed13).toBeInstanceOf(Array);
|
||||
expect(removed13[0]).toBeDefined();
|
||||
expect(removed13[0].deleted).toBeDefined();
|
||||
if (removed13[0].deleted) expect(removed13[0].deleted.id).toBe(13);
|
||||
if (removed13[0].deleted) expect(removed13[0].deleted.key).toBe(13);
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
|
@ -320,7 +320,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed3).toBeInstanceOf(Array);
|
||||
expect(removed3[0]).toBeDefined();
|
||||
expect(removed3[0].deleted).toBeDefined();
|
||||
if (removed3[0].deleted) expect(removed3[0].deleted.id).toBe(3);
|
||||
if (removed3[0].deleted) expect(removed3[0].deleted.key).toBe(3);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
|
@ -328,7 +328,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed8).toBeInstanceOf(Array);
|
||||
expect(removed8[0]).toBeDefined();
|
||||
expect(removed8[0].deleted).toBeDefined();
|
||||
if (removed8[0].deleted) expect(removed8[0].deleted.id).toBe(8);
|
||||
if (removed8[0].deleted) expect(removed8[0].deleted.key).toBe(8);
|
||||
expect(objBST.isAVLBalanced()).toBe(true);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
|
@ -336,7 +336,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed6).toBeInstanceOf(Array);
|
||||
expect(removed6[0]).toBeDefined();
|
||||
expect(removed6[0].deleted).toBeDefined();
|
||||
if (removed6[0].deleted) expect(removed6[0].deleted.id).toBe(6);
|
||||
if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
|
||||
expect(objBST.remove(6).length).toBe(0);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
@ -345,7 +345,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed7).toBeInstanceOf(Array);
|
||||
expect(removed7[0]).toBeDefined();
|
||||
expect(removed7[0].deleted).toBeDefined();
|
||||
if (removed7[0].deleted) expect(removed7[0].deleted.id).toBe(7);
|
||||
if (removed7[0].deleted) expect(removed7[0].deleted.key).toBe(7);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
|
@ -353,7 +353,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed9).toBeInstanceOf(Array);
|
||||
expect(removed9[0]).toBeDefined();
|
||||
expect(removed9[0].deleted).toBeDefined();
|
||||
if (removed9[0].deleted) expect(removed9[0].deleted.id).toBe(9);
|
||||
if (removed9[0].deleted) expect(removed9[0].deleted.key).toBe(9);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(3);
|
||||
|
||||
|
@ -361,7 +361,7 @@ describe('Individual package BST operations test', () => {
|
|||
expect(removed14).toBeInstanceOf(Array);
|
||||
expect(removed14[0]).toBeDefined();
|
||||
expect(removed14[0].deleted).toBeDefined();
|
||||
if (removed14[0].deleted) expect(removed14[0].deleted.id).toBe(14);
|
||||
if (removed14[0].deleted) expect(removed14[0].deleted.key).toBe(14);
|
||||
expect(objBST.isAVLBalanced()).toBe(false);
|
||||
expect(objBST.getHeight()).toBe(2);
|
||||
|
||||
|
@ -373,8 +373,8 @@ describe('Individual package BST operations test', () => {
|
|||
expect(bfsIDs[2]).toBe(16);
|
||||
|
||||
const bfsNodes = objBST.BFS('node');
|
||||
expect(bfsNodes[0].id).toBe(2);
|
||||
expect(bfsNodes[1].id).toBe(12);
|
||||
expect(bfsNodes[2].id).toBe(16);
|
||||
expect(bfsNodes[0].key).toBe(2);
|
||||
expect(bfsNodes[1].key).toBe(12);
|
||||
expect(bfsNodes[2].key).toBe(16);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -52,14 +52,6 @@ describe('BinaryTreeNode', () => {
|
|||
expect(node1.left).toBe(node2);
|
||||
});
|
||||
|
||||
it('should set and get the height correctly', () => {
|
||||
const node = new BinaryTreeNode<number>(1);
|
||||
expect(node.height).toBe(0);
|
||||
|
||||
node.height = 3;
|
||||
expect(node.height).toBe(3);
|
||||
});
|
||||
|
||||
it('should determine family position correctly', () => {
|
||||
const root = new BinaryTreeNode<number>(1);
|
||||
const leftChild = new BinaryTreeNode<number>(2);
|
||||
|
|
Loading…
Reference in a new issue