Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/avlTree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@ import { AvlTreeNode } from './avlTreeNode';
export class AvlTree<T> extends BinarySearchTree<T> {
constructor(compare?: (a: T, b: T) => number, options?: { key: string });
insert(value: T): AvlTree<T>;
insertIterative(value: T): AvlTree<T>;
remove(value: T): boolean;
removeIterative(value: T): boolean;
find(value: T): AvlTreeNode<T> | null;
findIterative(value: T): AvlTreeNode<T> | null;
findKey(key: number|string): AvlTreeNode<T> | null;
max(node?: AvlTreeNode<T>): AvlTreeNode<T> | null;
maxIterative(node?: AvlTreeNode<T>): AvlTreeNode<T> | null;
min(node?: AvlTreeNode<T>): AvlTreeNode<T> | null;
minIterative(node?: AvlTreeNode<T>): AvlTreeNode<T> | null;
lowerBound(value: T, includeEqual?: boolean): AvlTreeNode<T> | null;
lowerBoundIterative(value: T, includeEqual?: boolean): AvlTreeNode<T> | null;
lowerBoundKey(key: number|string, includeEqual?: boolean): AvlTreeNode<T> | null;
floor(value: T, includeEqual?: boolean): AvlTreeNode<T> | null;
floorKey(key: number|string, includeEqual?: boolean): AvlTreeNode<T> | null;
upperBound(value: T, includeEqual?: boolean): AvlTreeNode<T> | null;
upperBoundIterative(value: T, includeEqual?: boolean): AvlTreeNode<T> | null;
upperBoundKey(key: number|string, includeEqual?: boolean): AvlTreeNode<T> | null;
ceil(value: T, includeEqual?: boolean): AvlTreeNode<T> | null;
ceilKey(key: number|string, includeEqual?: boolean): AvlTreeNode<T> | null;
root(): AvlTreeNode<T> | null;
traverseInOrder(cb: (node: AvlTreeNode<T>) => void): void;
traversePreOrder(cb: (node: AvlTreeNode<T>) => void): void;
traversePostOrder(cb: (node: AvlTreeNode<T>) => void): void;
traverseInOrder(cb: (node: AvlTreeNode<T>) => void, abortCb?: () => boolean): void;
traverseInOrderIterative(cb: (node: AvlTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePreOrder(cb: (node: AvlTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePreOrderIterative(cb: (node: AvlTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePostOrder(cb: (node: AvlTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePostOrderIterative(cb: (node: AvlTreeNode<T>) => void, abortCb?: () => boolean): void;
removeNode(node: AvlTreeNode): boolean;
}
106 changes: 104 additions & 2 deletions src/avlTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class AvlTree extends BinarySearchTree {

/**
* Inserts a value into the tree and maintains
* the tree balanced by making the necessary rotations
* the tree balanced by making the necessary rotations (recursive implementation)
*
* @public
* @param {number|string|object} value
Expand Down Expand Up @@ -111,9 +111,67 @@ class AvlTree extends BinarySearchTree {
return this;
}

/**
* Inserts a value into the tree and maintains
* the tree balanced by making the necessary rotations (iterative implementation)
*
* @public
* @param {number|string|object} value
* @return {AvlTree}
*/
insertIterative(value) {
const newNode = new AvlTreeNode(value, this._compare);

if (this._root === null) {
this._root = newNode;
this._count += 1;
return this;
}

// Find insertion point and track path
const path = [];
let node = this._root;
let inserted = false;

while (!inserted) {
path.push(node);
const compare = this._compare(value, node.getValue());

if (compare < 0) {
if (node.hasLeft()) {
node = node.getLeft();
} else {
newNode.setParent(node);
node.setLeft(newNode).updateHeight();
this._count += 1;
inserted = true;
}
} else if (compare > 0) {
if (node.hasRight()) {
node = node.getRight();
} else {
newNode.setParent(node);
node.setRight(newNode).updateHeight();
this._count += 1;
inserted = true;
}
} else {
node.setValue(value);
return this;
}
}

// Balance all ancestors in reverse order (backward-tracking)
for (let i = path.length - 1; i >= 0; i -= 1) {
this._balanceNode(path[i]);
}

return this;
}

/**
* Removes a node from the tree and maintains
* the tree balanced by making the necessary rotations
* the tree balanced by making the necessary rotations (recursive implementation)
*
* @public
* @param {number|string|object} value
Expand Down Expand Up @@ -145,6 +203,50 @@ class AvlTree extends BinarySearchTree {
return removeRecursively(value, this._root);
}

/**
* Removes a node from the tree and maintains
* the tree balanced by making the necessary rotations (iterative implementation)
*
* @public
* @param {number|string|object} value
* @return {boolean}
*/
removeIterative(value) {
if (this._root === null) {
return false;
}

const path = [];
let node = this._root;
let found = false;
while (node !== null && !found) {
const compare = this._compare(value, node.getValue());

if (compare === 0) {
found = true;
} else {
path.push(node);
if (compare < 0) {
node = node.getLeft();
} else {
node = node.getRight();
}
}
}

if (!found) {
return false;
}

const removed = this.removeNode(node);
for (let i = path.length - 1; i >= 0; i -= 1) {
// Balance all ancestors in reverse order (backward-tracking)
this._balanceNode(path[i]);
}

return removed;
}

/**
* Removes a node from the tree
* @public
Expand Down
11 changes: 11 additions & 0 deletions src/binarySearchTree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,37 @@ import { BinarySearchTreeNode } from './binarySearchTreeNode';
export class BinarySearchTree<T> {
constructor(compare?: (a: T, b: T) => number, options?: { key: string });
insert(value: T): BinarySearchTree<T>;
insertIterative(value: T): BinarySearchTree<T>;
has(value: T): boolean;
hasIterative(value: T): boolean;
hasKey(key: number|string): boolean;
find(value: T): BinarySearchTreeNode<T> | null;
findIterative(value: T): BinarySearchTreeNode<T> | null;
findKey(key: number|string): BinarySearchTreeNode<T> | null;
max(node?: BinarySearchTreeNode<T>): BinarySearchTreeNode<T> | null;
maxIterative(node?: BinarySearchTreeNode<T>): BinarySearchTreeNode<T> | null;
min(node?: BinarySearchTreeNode<T>): BinarySearchTreeNode<T> | null;
minIterative(node?: BinarySearchTreeNode<T>): BinarySearchTreeNode<T> | null;
lowerBound(value: T, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
lowerBoundIterative(value: T, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
lowerBoundKey(key: number|string, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
floor(value: T, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
floorKey(key: number|string, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
upperBound(value: T, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
upperBoundIterative(value: T, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
upperBoundKey(key: number|string, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
ceil(value: T, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
ceilKey(key: number|string, includeEqual?: boolean): BinarySearchTreeNode<T> | null;
root(): BinarySearchTreeNode<T> | null;
count(): number;
remove(value: T): boolean;
removeIterative(value: T): boolean;
removeNode(node: BinarySearchTreeNode): boolean;
traverseInOrder(cb: (node: BinarySearchTreeNode<T>) => void, abortCb?: () => boolean): void;
traverseInOrderIterative(cb: (node: BinarySearchTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePreOrder(cb: (node: BinarySearchTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePreOrderIterative(cb: (node: BinarySearchTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePostOrder(cb: (node: BinarySearchTreeNode<T>) => void, abortCb?: () => boolean): void;
traversePostOrderIterative(cb: (node: BinarySearchTreeNode<T>) => void, abortCb?: () => boolean): void;
clear(): void;
}
Loading