Complete API documentation for the SawitTree class.
- Sawit.js API Reference
- Table of Contents
- Types
- Constructor
- Static Factory Methods
- Properties
- Core Operations
- Min/Max Operations
- Floor/Ceiling Operations
- Order Statistics
- Range Queries
- Iteration & Traversal
- Functional Methods
forEach(callback: ForEachCallback<T>): voidmap<U>(fn: MapFunction<T, U>, comparator?): SawitTree<U>filter(predicate: Predicate<T>): SawitTree<T>reduce<U>(fn, initialValue: U): Uevery(predicate: Predicate<T>): booleansome(predicate: Predicate<T>): booleanfind(predicate: Predicate<T>): T | undefined
- Set Operations
- Conversion Methods
type Comparator<T> = (a: T, b: T) => number;Function that compares two values. Returns:
- Negative number if
a < b - Positive number if
a > b - Zero if
a === b
type ForEachCallback<T> = (value: T, index: number) => void;type Predicate<T> = (value: T) => boolean;type MapFunction<T, U> = (value: T) => U;Creates a new empty SawitTree.
Parameters:
comparator(optional): Custom comparison function
Example:
// Default comparator (works for numbers, strings)
const numTree = new SawitTree<number>();
// Custom comparator for objects
const userTree = new SawitTree<User>((a, b) => a.age - b.age);
// Descending order
const descTree = new SawitTree<number>((a, b) => b - a);Creates a tree from an iterable.
Complexity: O(n log n)
const tree = SawitTree.from([5, 3, 7, 1, 9]);
const fromSet = SawitTree.from(new Set([1, 2, 3]));Creates a tree from variable arguments.
Complexity: O(n log n)
const tree = SawitTree.of(5, 3, 7, 1, 9);Returns the number of elements. O(1)
Returns the height of the tree. O(1)
Returns true if the tree is empty. O(1)
Inserts a value into the tree. Duplicates are ignored.
Complexity: O(log n)
Returns: this for chaining
tree.insert(5).insert(3).insert(7);Inserts multiple values.
Complexity: O(k log n)
tree.insertAll([1, 2, 3, 4, 5]);Removes a value from the tree.
Complexity: O(log n)
Returns: true if found and removed
const removed = tree.delete(5); // true or falseChecks if a value exists.
Complexity: O(log n)
if (tree.has(5)) {
console.log('Found!');
}Alias for has().
Removes all elements.
Complexity: O(1)
tree.clear();
console.log(tree.isEmpty); // trueReturns the minimum value.
Complexity: O(log n)
Returns the maximum value.
Complexity: O(log n)
Removes and returns the minimum value.
Complexity: O(log n)
while (!tree.isEmpty) {
console.log(tree.extractMin()); // Processes in ascending order
}Removes and returns the maximum value.
Complexity: O(log n)
Returns the largest value ≤ the given value.
Complexity: O(log n)
const tree = SawitTree.from([1, 3, 5, 7, 9]);
tree.floor(6); // 5
tree.floor(5); // 5
tree.floor(0); // undefinedReturns the smallest value ≥ the given value.
Complexity: O(log n)
tree.ceiling(6); // 7
tree.ceiling(5); // 5
tree.ceiling(10); // undefinedReturns the largest value strictly less than the given value.
Complexity: O(log n)
tree.lower(5); // 3Returns the smallest value strictly greater than the given value.
Complexity: O(log n)
tree.higher(5); // 7Returns the number of elements less than the given value.
Complexity: O(log n)
const tree = SawitTree.from([1, 3, 5, 7, 9]);
tree.rank(5); // 2 (two elements are less than 5)
tree.rank(6); // 3Returns the element at the given rank (0-indexed).
Complexity: O(log n)
tree.select(0); // 1 (smallest)
tree.select(2); // 5
tree.select(4); // 9 (largest)Returns the k-th smallest element (1-indexed).
Complexity: O(log n)
tree.kthSmallest(1); // 1 (smallest)
tree.kthSmallest(3); // 5Returns the k-th largest element (1-indexed).
Complexity: O(log n)
tree.kthLargest(1); // 9 (largest)
tree.kthLargest(3); // 5Returns all elements in the range [low, high] (inclusive).
Complexity: O(k + log n) where k is the number of elements in range
const tree = SawitTree.from([1, 3, 5, 7, 9]);
tree.range(3, 7); // [3, 5, 7]Returns the count of elements in the range.
Complexity: O(log n)
tree.rangeCount(3, 7); // 3Enables for...of loops and spread syntax.
for (const value of tree) {
console.log(value);
}
const arr = [...tree];In-order traversal (left, root, right) - produces sorted output.
for (const value of tree.inOrder()) {
console.log(value);
}Pre-order traversal (root, left, right). Useful for tree serialization.
Post-order traversal (left, right, root). Useful for tree deletion.
Level-order (BFS) traversal. Visits nodes level by level.
// Tree: 4
// / \
// 2 6
// / \
// 1 3
console.log([...tree.levelOrder()]); // [4, 2, 6, 1, 3]Reverse in-order traversal (descending order).
Executes a callback for each element in sorted order.
Complexity: O(n)
tree.forEach((value, index) => {
console.log(`${index}: ${value}`);
});Creates a new tree with transformed values.
Complexity: O(n log n)
const doubled = tree.map(x => x * 2);Creates a new tree with elements that pass the test.
Complexity: O(n log n)
const evens = tree.filter(x => x % 2 === 0);Reduces the tree to a single value.
Complexity: O(n)
const sum = tree.reduce((acc, val) => acc + val, 0);Tests whether all elements pass the predicate.
Complexity: O(n)
const allPositive = tree.every(x => x > 0);Tests whether any element passes the predicate.
Complexity: O(n)
const hasNegative = tree.some(x => x < 0);Finds the first element that passes the predicate.
Complexity: O(n)
const firstEven = tree.find(x => x % 2 === 0);Returns a new tree with elements from both trees.
Complexity: O((n + m) log(n + m))
const combined = tree1.union(tree2);Returns a new tree with elements present in both trees.
Complexity: O(n log m)
const common = tree1.intersection(tree2);Returns a new tree with elements in this tree but not in other.
Complexity: O(n log m)
const diff = tree1.difference(tree2);Returns elements in either tree but not both.
Complexity: O((n + m) log(n + m))
Checks if all elements of this tree are in other.
Complexity: O(n log m)
Checks if this tree contains all elements of other.
Complexity: O(m log n)
Converts to a sorted array.
Complexity: O(n)
const arr = tree.toArray();Converts to a Set.
Complexity: O(n)
const set = tree.toSet();Creates a deep copy.
Complexity: O(n log n)
const copy = tree.clone();Returns a JSON representation of the tree structure.
Complexity: O(n)
console.log(tree.toString());Validates AVL tree properties (for debugging).
console.log(tree.isValid()); // true if balanced