Base Class for all tree implementations
Extends Static PropertiesProperty | Type | Default Value | Description |
IN_ORDER | property | "in_order" | In Order |
POST_ORDER | property | "post_order" | Post Order |
PRE_ORDER | property | "pre_order" | Pre Order |
REVERSE_ORDER | property | "reverse_order" | Reverse Order |
function (options){ options = options || {}; this.compare = options.compare || compare; this.__root = null; }
Prints a node
Argumentsnode to print
the current level the node is at, Used for formatting
function (node,level){ //console.log(level); var str = []; if (node == null || node === undefined) { str.push(multiply('\t', level)); str.push("~"); console.log(str.join("")); } else { this.__printNode(node.right, level + 1); str.push(multiply('\t', level)); str.push(node.data + "\n"); console.log(str.join("")); this.__printNode(node.left, level + 1); } }
Clear all items from a tree
Sourcefunction (){ this.__root = null; }
Determines if a value is contained in the tree
Argumentsthe value to find
Boolean
true if the tree contains the item false otherwise.
function (value){ var ret = false; var root = this.__root; while (root != null) { var cmp = this.compare(value, root.data); if (cmp) { root = root[(cmp === -1) ? "left" : "right"]; } else { ret = true; root = null; } } return ret; }
Determines if every item meets the condition returned by the callback.
Argumentscalled for each item in the tree
this
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
Boolean
True if every item passed false otherwise
function (cb,scope,order){ if (typeof cb !== "function") { throw new TypeError("cb must be a function"); } order = order || Tree.IN_ORDER; scope = scope || this; var ret = false; this.traverseWithCondition(this.__root, order, function (node) { ret = cb.call(scope, node, this); return ret; }); return ret; }
Filters a tree, only returning items that result in true being returned from the callback
Argumentscalled for each item in the tree
this
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
comb.collections.Tree
the tree with items that resulted in true being returned from the callback
function (cb,scope,order){ if (typeof cb !== "function") { throw new TypeError("cb must be a function"); } order = order || Tree.IN_ORDER; scope = scope || this; var ret = new this._static(); this.traverse(this.__root, order, function (node) { var include = cb.call(scope, node, this); include && ret.insert(node); }); return ret; }
Finds a value in the tree
Argumentsthe value to find
the value of the node that matched
function (value){ var ret; var root = this.__root; while (root != null) { var cmp = this.compare(value, root.data); if (cmp) { root = root[(cmp === -1) ? "left" : "right"]; } else { ret = root.data; break; } } return ret; }
Find all greater than a value
Argumentsthe value to find nodes greater than
false
] : if true the value will NOT be included in the return array
Array
the array containing all values greater than
function (value,exclusive){ //find a better way!!!! var ret = [], compare = this.compare; this.traverse(this.__root, exports.REVERSE_ORDER, function (v) { var cmp = compare(value, v); if ((!exclusive && cmp === 0) || cmp === -1) { ret.push(v); return true; } else { return false; } }); return ret; }
Find all values less than a value
Argumentsthe value to find nodes less than
false
] : if true the value will NOT be included in the return array
Array
the array containing all values less than
function (value,exclusive){ //find a better way!!!! var ret = [], compare = this.compare; this.traverseWithCondition(this.__root, exports.IN_ORDER, function (v) { var cmp = compare(value, v); if ((!exclusive && cmp === 0) || cmp === 1) { ret.push(v); return true; } else { return false; } }); return ret; }
Loop through each item in the tree
Argumentscalled for each item in the tree
this
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
function (cb,scope,order){ if (typeof cb !== "function") { throw new TypeError("cb must be a function"); } order = order || Tree.IN_ORDER; scope = scope || this; this.traverse(this.__root, order, function (node) { cb.call(scope, node, this); }); }
Inserts an item into the tree
Argumentsthe item to insert
function (data){ throw new Error("Not Implemented"); }
Test if a tree is empty
ReturnsBoolean
true if empty false otherwise
function (){ return this.__root == null; }
Loop through each item in the tree, collecting the value returned by the callback funciton.
Argumentscalled for each item in the tree. Whatever the function returns is inserted into the return tree
this
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
comb.collections.Tree
the tree with the mapped items
function (cb,scope,order){ if (typeof cb !== "function") { throw new TypeError("cb must be a function"); } order = order || Tree.IN_ORDER; scope = scope || this; var ret = new this._static(); this.traverse(this.__root, order, function (node) { ret.insert(cb.call(scope, node, this)); }); return ret; }
Prints a tree to the console.
Sourcefunction (){ this.__printNode(this.__root, 0); }
Reduces a tree
Argumentscalled for each item in the tree
First item in tree(Order dependant)
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
the result of the reduce function
function (fun,accumulator,order){ var arr = this.toArray(order); var args = [fun]; if (!base.isUndefinedOrNull(accumulator)) { args.push(accumulator); } return arr.reduce.apply(arr, args); }
Reduces from right to left
Argumentscalled for each item in the tree
First item in tree(Order dependant)
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
the result of the reduce function
function (fun,accumulator,order){ var arr = this.toArray(order); var args = [fun]; if (!base.isUndefinedOrNull(accumulator)) { args.push(accumulator); } return arr.reduceRight.apply(arr, args); }
Removes an item from the tree
Argumentsthe item to insert
function (data){ throw new Error("Not Implemented"); }
Determines if some item meet the condition returned by the callback. Traversal ends the first time true is found.
Argumentscalled for each item in the tree
this
] : scope to call the function in
Tree.IN_ORDER
] : the traversal scheme
Boolean
True if every item passed false otherwise
function (cb,scope,order){ if (typeof cb !== "function") { throw new TypeError("cb must be a function"); } order = order || Tree.IN_ORDER; scope = scope || this; var ret; this.traverseWithCondition(this.__root, order, function (node) { ret = cb.call(scope, node, this); return !ret; }); return ret; }
Converts a tree into an array based on the specified order
ArgumentsTree.IN_ORDER
] : the traversal scheme
Array
array of all items in the order specified.
function (order){ order = order || Tree.IN_ORDER; var arr = []; this.traverse(this.__root, order, function (node) { arr.push(node); }); return arr; }
Traverse a tree
Not typically used directly
Argumentsthe node to start at
Tree.IN_ORDER
] : the traversal scheme
called for each item
function (node,order,callback){ if (node) { order = order || Tree.PRE_ORDER; if (order === Tree.PRE_ORDER) { callback(node.data); this.traverse(node.left, order, callback); this.traverse(node.right, order, callback); } else if (order === Tree.IN_ORDER) { this.traverse(node.left, order, callback); callback(node.data); this.traverse(node.right, order, callback); } else if (order === Tree.POST_ORDER) { this.traverse(node.left, order, callback); this.traverse(node.right, order, callback); callback(node.data); } else if (order === Tree.REVERSE_ORDER) { this.traverseWithCondition(node.right, order, callback); callback(node.data); this.traverseWithCondition(node.left, order, callback); } } }
Traverse a tree until the callback function returns false
Not typically used directly
Argumentsthe node to start at
Tree.IN_ORDER
] : the traversal scheme
called for each item, traversal continues until the function returns false
function (node,order,callback){ var cont = true; if (node) { order = order || Tree.PRE_ORDER; if (order === Tree.PRE_ORDER) { cont = callback(node.data); if (cont) { cont = this.traverseWithCondition(node.left, order, callback); cont && (cont = this.traverseWithCondition(node.right, order, callback)); } } else if (order === Tree.IN_ORDER) { cont = this.traverseWithCondition(node.left, order, callback); if (cont) { cont = callback(node.data); cont && (cont = this.traverseWithCondition(node.right, order, callback)); } } else if (order === Tree.POST_ORDER) { cont = this.traverseWithCondition(node.left, order, callback); if (cont) { cont && (cont = this.traverseWithCondition(node.right, order, callback)); cont && (cont = callback(node.data)); } } else if (order === Tree.REVERSE_ORDER) { cont = this.traverseWithCondition(node.right, order, callback); if (cont) { cont = callback(node.data); cont && (cont = this.traverseWithCondition(node.left, order, callback)); } } } return cont; }
MIT https://github.com/C2FO/comb/raw/master/LICENSE
git clone git://github.com/C2FO/comb.git