|
1 | 1 | # java-script-lib
|
2 | 2 | A library of reusable JavaScript code; currently only contains a fast binary search tree implementation.
|
| 3 | + |
| 4 | +## BinarySearchTree |
| 5 | + |
| 6 | +BinarySearchTree maintains an arbitrary set of elements in sorted order. |
| 7 | +Performs in-order traversal in O(N) time; min, max, get, insertion, and |
| 8 | +removal in O(log N) time; and size in O(1) time. Nodes in the tree perform |
| 9 | +predecessor and successor in O(log N) time. The tree forbids repeated |
| 10 | +insertions with the same key. Assumes single-threaded execution. Many of |
| 11 | +these functions can also be implemented recursively, but their iterative |
| 12 | +implementations are favored for performance. |
| 13 | + |
| 14 | +### Constructor |
| 15 | + |
| 16 | +#### new BinarySearchTree() |
| 17 | + |
| 18 | +Creates a new binary search tree with no nodes. |
| 19 | + |
| 20 | +### Methods |
| 21 | + |
| 22 | +#### traverse(callback) |
| 23 | + |
| 24 | +Traverses this tree in the sort order, meaning according to the key's |
| 25 | +natural order. Calls the given callback function once for each node with the |
| 26 | +current node as its only argument. |
| 27 | + |
| 28 | +#### min() |
| 29 | + |
| 30 | +Returns the node in this tree with the minimum key. Returns null if this tree |
| 31 | +is empty. |
| 32 | + |
| 33 | +#### max() |
| 34 | + |
| 35 | +Returns the node in this tree with the maximum key. Returns null if this tree |
| 36 | +is empty. |
| 37 | + |
| 38 | +#### get(key) |
| 39 | + |
| 40 | +Returns the node in this tree with the given key. Returns null if the given |
| 41 | +key is not present in this tree. |
| 42 | + |
| 43 | +#### insert(key, value) |
| 44 | + |
| 45 | +Inserts the given key, value pair in this tree. Creates a new node with the |
| 46 | +given key and value and inserts it in the appropriate position. Throws an |
| 47 | +exception if the given key is already present in this tree. |
| 48 | + |
| 49 | +#### remove(key) |
| 50 | + |
| 51 | +Removes the node with the given key from this tree. Throws an exception if |
| 52 | +the given key is not present in this tree. Does not modify the removed node. |
| 53 | +References to nodes other than the removed node are guaranteed to remain |
| 54 | +valid. |
| 55 | + |
| 56 | +#### size() |
| 57 | + |
| 58 | +Returns the number of nodes in this tree, which is equivalent to the number |
| 59 | +of key, value pairs. |
| 60 | + |
| 61 | +## BinarySearchTree.prototype.Node |
| 62 | + |
| 63 | +This is a node in a binary search tree with a comparable key and an arbitrary |
| 64 | +value. A null parent indicates that a node is the root of its tree. Nodes are |
| 65 | +sorted according to the key's natural order. There is no need to construct a |
| 66 | +node directly as they are created by inserting key, value pairs. |
| 67 | + |
| 68 | +### Methods |
| 69 | + |
| 70 | +#### traverse(callback) |
| 71 | + |
| 72 | +Traverses the subtree rooted at this node in the sort order, meaning |
| 73 | +according to the key's natural order. Calls the given callback function once |
| 74 | +for each node with the current node as its only argument. |
| 75 | + |
| 76 | +#### predecessor() |
| 77 | + |
| 78 | +Returns the node in this node's tree that immediately precedes it in the sort |
| 79 | +order. The node returned has the greatest key of the nodes with keys less |
| 80 | +than this node's key. Returns null if this node's key is the min. Prefer |
| 81 | +BinarySearchTree.traverse when iterating over an entire tree and |
| 82 | +BinarySearchTree.prototype.Node.traverse when iterating over an entire |
| 83 | +subtree. The traverse methods run in O(N) time in the number of nodes being |
| 84 | +iterated over, while predecessor requires O(N * log N) time to traverse a |
| 85 | +subtree. |
| 86 | + |
| 87 | +#### successor() |
| 88 | + |
| 89 | +Returns the node in this node's tree that immediately succeeds it in the sort |
| 90 | +order. The node returned has the least key of the nodes with keys greater |
| 91 | +than this node's key. Returns null if this node's key is the max. Prefer |
| 92 | +BinarySearchTree.traverse when iterating over an entire tree and |
| 93 | +BinarySearchTree.prototype.Node.traverse when iterating over an entire |
| 94 | +subtree. The traverse methods run in O(N) time in the number of nodes being |
| 95 | +iterated over, while successor requires O(N * log N) time to traverse a |
| 96 | +subtree. |
| 97 | + |
| 98 | +#### min() |
| 99 | + |
| 100 | +Returns the node with the minimum key in the subtree rooted at this node. |
| 101 | + |
| 102 | +#### max() |
| 103 | + |
| 104 | +Returns the node with the maximum key in the subtree rooted at this node. |
| 105 | + |
| 106 | +#### get(key) |
| 107 | + |
| 108 | +Returns the node with the given key in the subtree rooted at this node. |
| 109 | +Returns null if there is no such node. |
| 110 | + |
| 111 | +#### insert(key, value) |
| 112 | + |
| 113 | +Inserts the given key, value pair in the subtree rooted at this node. Creates |
| 114 | +a new node with the given key and value and inserts it in the appropriate |
| 115 | +position. Throws an exception if the given key is already in this subtree. |
| 116 | + |
| 117 | +#### remove() |
| 118 | + |
| 119 | +Removes this node from its tree. Returns the node that now occupies this |
| 120 | +node's position or null if there is no such node. Does not modify the removed |
| 121 | +node. References to nodes other than the removed node are guaranteed to |
| 122 | +remain valid. |
0 commit comments