Comparing version 0.0.4 to 0.1.0
@@ -1,8 +0,19 @@ | ||
export interface AvlTreeNode<K, V> { | ||
export interface AvlTreeNodeJson<K, V> { | ||
readonly key: K; | ||
readonly value: V; | ||
parent?: AvlTreeNode<K, V>; | ||
left?: AvlTreeNodeJson<K, V>; | ||
right?: AvlTreeNodeJson<K, V>; | ||
balanceFactor: number; | ||
} | ||
export declare class AvlTreeNode<K, V> implements AvlTreeNodeJson<K, V> { | ||
readonly key: K; | ||
readonly value: V; | ||
parent?: AvlTreeNode<K, V> | undefined; | ||
left?: AvlTreeNode<K, V>; | ||
right?: AvlTreeNode<K, V>; | ||
balanceFactor: number; | ||
constructor(key: K, value: V, parent?: AvlTreeNode<K, V> | undefined); | ||
successor(): AvlTreeNode<K, V> | undefined; | ||
predecessor(): AvlTreeNode<K, V> | undefined; | ||
toJSON(): AvlTreeNodeJson<K, V>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AvlTreeNode = void 0; | ||
class AvlTreeNode { | ||
constructor(key, value, parent) { | ||
this.key = key; | ||
this.value = value; | ||
this.parent = parent; | ||
this.balanceFactor = 0; | ||
} | ||
successor() { | ||
if (this.right) { | ||
// Find leftmost child | ||
let current = this.right; | ||
while (current.left) { | ||
current = current.left; | ||
} | ||
return current; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
let child = this; | ||
let parent = this.parent; | ||
// Travel up until the current node is no longer the right child of its parent | ||
while ((parent === null || parent === void 0 ? void 0 : parent.right) === child) { | ||
child = parent; | ||
parent = child.parent; | ||
} | ||
return parent; | ||
} | ||
predecessor() { | ||
if (this.left) { | ||
// Find rightmost child | ||
let current = this.left; | ||
while (current.right) { | ||
current = current.right; | ||
} | ||
return current; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
let child = this; | ||
let parent = this.parent; | ||
// Travel up until the current node is no longer the left child of its parent | ||
while ((parent === null || parent === void 0 ? void 0 : parent.left) === child) { | ||
child = parent; | ||
parent = child.parent; | ||
} | ||
return parent; | ||
} | ||
toJSON() { | ||
const json = { | ||
key: this.key, | ||
value: this.value, | ||
balanceFactor: this.balanceFactor | ||
}; | ||
if (this.left) { | ||
json.left = this.left.toJSON(); | ||
} | ||
if (this.right) { | ||
json.right = this.right.toJSON(); | ||
} | ||
return json; | ||
} | ||
} | ||
exports.AvlTreeNode = AvlTreeNode; | ||
//# sourceMappingURL=avl-tree-node.js.map |
@@ -67,4 +67,3 @@ import { AvlTreeNode } from './avl-tree-node'; | ||
export declare function printTreeNode<K, V>(root: AvlTreeNode<K, V> | undefined, printNode?: (node: AvlTreeNode<K, V>) => string): string; | ||
export declare function nodeToJson<K, V>(node: AvlTreeNode<K, V>): AvlTreeNode<K, V>; | ||
export declare function checkTree<K, V>(node: AvlTreeNode<K, V> | undefined): void; | ||
export declare function computeHeight<K, V>(node: AvlTreeNode<K, V> | undefined): number; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.computeHeight = exports.checkTree = exports.nodeToJson = exports.printTreeNode = exports.replaceNode = exports.disconnectChildNodeFromParent = exports.replaceChild = exports.rotateRight = exports.rotateLeft = void 0; | ||
exports.computeHeight = exports.checkTree = exports.printTreeNode = exports.replaceNode = exports.disconnectChildNodeFromParent = exports.replaceChild = exports.rotateRight = exports.rotateLeft = void 0; | ||
/** | ||
@@ -267,17 +267,2 @@ * Rotates left around a root node | ||
} | ||
function nodeToJson(node) { | ||
const json = { | ||
key: node.key, | ||
value: node.value, | ||
balanceFactor: node.balanceFactor | ||
}; | ||
if (node.left) { | ||
json.left = nodeToJson(node.left); | ||
} | ||
if (node.right) { | ||
json.right = nodeToJson(node.right); | ||
} | ||
return json; | ||
} | ||
exports.nodeToJson = nodeToJson; | ||
function checkTree(node) { | ||
@@ -284,0 +269,0 @@ if (!node) { |
@@ -1,2 +0,2 @@ | ||
import { AvlTreeNode } from './avl-tree-node'; | ||
import { AvlTreeNode, AvlTreeNodeJson } from './avl-tree-node'; | ||
/** | ||
@@ -30,3 +30,3 @@ * An AVL tree, a self-balancing binary search tree. | ||
*/ | ||
toJSON(): AvlTreeNode<K, V> | undefined; | ||
toJSON(): AvlTreeNodeJson<K, V> | undefined; | ||
/** | ||
@@ -121,3 +121,9 @@ * Checks if the tree contains a certain key. | ||
walk(fn: (entry: [K, V], index: number, done: () => void) => void): void; | ||
/** | ||
* @deprecated Use AvlTreeNode.predecessor() instead | ||
*/ | ||
predecessor(node: AvlTreeNode<K, V>): AvlTreeNode<K, V> | undefined; | ||
/** | ||
* @deprecated Use AvlTreeNode.successor() instead | ||
*/ | ||
successor(node: AvlTreeNode<K, V>): AvlTreeNode<K, V> | undefined; | ||
@@ -124,0 +130,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AvlTree = void 0; | ||
const avl_tree_node_1 = require("./avl-tree-node"); | ||
const avl_tree_utils_1 = require("./avl-tree-utils"); | ||
@@ -41,3 +42,4 @@ /** | ||
toJSON() { | ||
return this._root && avl_tree_utils_1.nodeToJson(this._root); | ||
var _a; | ||
return (_a = this._root) === null || _a === void 0 ? void 0 : _a.toJSON(); | ||
} | ||
@@ -106,7 +108,3 @@ /** | ||
if (!this._root) { | ||
this._root = { | ||
key, | ||
value, | ||
balanceFactor: 0 | ||
}; | ||
this._root = new avl_tree_node_1.AvlTreeNode(key, value); | ||
this._size += 1; | ||
@@ -138,8 +136,3 @@ return this._root; | ||
} | ||
const newNode = { | ||
key, | ||
value, | ||
parent, | ||
balanceFactor: 0 | ||
}; | ||
const newNode = new avl_tree_node_1.AvlTreeNode(key, value, parent); | ||
if (cmp < 0) { | ||
@@ -427,37 +420,13 @@ parent.left = newNode; | ||
} | ||
/** | ||
* @deprecated Use AvlTreeNode.predecessor() instead | ||
*/ | ||
predecessor(node) { | ||
if (node.left) { | ||
// Find rightmost child | ||
let current = node.left; | ||
while (current.right) { | ||
current = current.right; | ||
} | ||
return current; | ||
} | ||
let child = node; | ||
let parent = node.parent; | ||
// Travel up until the current node is no longer the left child of its parent | ||
while ((parent === null || parent === void 0 ? void 0 : parent.left) === child) { | ||
child = parent; | ||
parent = child.parent; | ||
} | ||
return parent; | ||
return node.predecessor(); | ||
} | ||
/** | ||
* @deprecated Use AvlTreeNode.successor() instead | ||
*/ | ||
successor(node) { | ||
if (node.right) { | ||
// Find leftmost child | ||
let current = node.right; | ||
while (current.left) { | ||
current = current.left; | ||
} | ||
return current; | ||
} | ||
let child = node; | ||
let parent = node.parent; | ||
// Travel up until the current node is no longer the right child of its parent | ||
while ((parent === null || parent === void 0 ? void 0 : parent.right) === child) { | ||
child = parent; | ||
parent = child.parent; | ||
} | ||
return parent; | ||
return node.successor(); | ||
} | ||
@@ -464,0 +433,0 @@ /** |
{ | ||
"name": "quick-avl", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "AVL tree: a self-balancing binary search tree", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
48825
1193