DOCUMENTATION

N-ary Tree Library Classes

NaryNode

n-ary Nodes have an item value, which can be any data type, although preferably an object, and an array of reference pointers to the node's children

NaryTree

This is an implementation of an iterable n-ary tree data structure and associated methods. An n-ary tree is a tree data structure where any node may have an arbitrary number of child nodes. This can potentially be used as a trie, but it is not designed for that purpose.

NaryNode

n-ary Nodes have an item value, which can be any data type, although preferably an object, and an array of reference pointers to the node's children

Kind: global class

new NaryNode(childObj)

ParamTypeDescription
childObjObjectany object type - The object to be inserted at the new n-ary node

naryNode.children ⇒ Array.<Object>

Retrieve the children of this node

Kind: instance property of NaryNode
Returns: Array.<Object> - children - an array of child nodes

naryNode.value ⇒ Object

Retrieve the object stored in this node

Kind: instance property of NaryNode
Returns: Object - value object - the object stored in this node

naryNode.children

Replace the children array associated with this node

Kind: instance property of NaryNode

ParamTypeDescription
newArrayArray.<array>a new array of child object references

naryNode.value

Replace the object stored in this node

Kind: instance property of NaryNode

ParamTypeDescription
newValueObjectany object type - the new object to store in this node

NaryTree

This is an implementation of an iterable n-ary tree data structure and associated methods. An n-ary tree is a tree data structure where any node may have an arbitrary number of child nodes. This can potentially used as a trie, but it is not designed for that purpose.

Kind: global class

new NaryTree()

Instantiates an empty n-ary tree as a JavaScript object

naryTree.root ⇒ Object

Return the root node of this n-ary tree

Kind: instance property of NaryTree
Returns: Object - NaryNode node object - returns a reference to the root node

naryTree.add(childObj) ⇒ boolean

Adds the parameter child object as root or the root node's next child

Kind: instance method of NaryTree
Returns: boolean - boolean value - success of operation

ParamTypeDescription
childObjObjectobject value - an object to store in the node

naryTree.addAsFirstChild(childObj, parent) ⇒ boolean

Adds child as the first child of the parent node in the n-ary tree

Kind: instance method of NaryTree
Returns: boolean - boolean value - success of operation

ParamTypeDescription
childObjObjectobject value - an object to store in the node
parentObjectNaryNode node object - the parent n-ary node

naryTree.addAsLastChild(childObj, parent) ⇒ boolean

Adds child as the last child of the parent node in the n-ary tree Returns a boolean value

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
childObjObjectobject value - an object to store in the node
parentObjectNaryNode node object - the parent n-ary node

naryTree.addAtPosition(childObj, parent, position) ⇒ boolean

Adds child as the position child of the parent node in the n-ary tree using level-order traversal Returns a boolean value

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
childObjObjectobject value - an object to store in the node
parentObjectNaryNode node object - the parent n-ary node
positionnumberinteger - the position number where the child should be included in the children array for this node

naryTree.clear() ⇒ boolean

Make this n-ary tree empty

Kind: instance method of NaryTree
Returns: boolean - boolean value

naryTree.contains(obj, parentNode) ⇒ boolean

Indicates whether an existing object, passed as an argument, exists in this n-ary tree If parentNode is specified contains only looks within the subtree rooted in the parentNode If obj is itself an n-ary Node a search-by-node is performed instead of a search for a matching object Returns a boolean value

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
objObjectan object stored in any node
parentNodeObjectNaryNode object - the parent n-ary node

naryTree.get(obj) ⇒ boolean

In no object parameter is specified, return the root node object In an object is specified, get returns the first occurrence of the specified object using a level-order tree traversal iterator.

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
objObjectobject - an object stored in a node somewhere in this tree

naryTree.getNode(obj) ⇒ Object

getNode returns the n-ary tree node reference for the first occurrence of the specified object passed as an argument to the method.

Kind: instance method of NaryTree
Returns: Object - NaryNode node object - a reference to first node found containing a reference to the specified object

ParamTypeDescription
objObjectan object store in a node

naryTree.getByObjectProperty(obj) ⇒ Object

In no object parameter is specified, return the root node object In an object is specified, get returns the first occurrence of the specified object using a level-order tree traversal iterator.

Kind: instance method of NaryTree
Returns: Object - node value object - returns the first object found containing the specified property and property value

ParamTypeDescription
objObject{key:value} object - a property key and value object

naryTree.getNodeByObjectProperty(obj) ⇒ Object

getNode returns the n-ary tree node reference for the first occurrence of the specified object passed as an argument to the method.

Kind: instance method of NaryTree
Returns: Object - NaryNode node object - returns the first node found containing an object with the specified property and property value

ParamTypeDescription
objObject{key:value} object - a property key and value object

naryTree.getRootItem() ⇒ Object

returns the object in the root node of the tree

Kind: instance method of NaryTree
Returns: Object - root node value object - a reference to the object stored in the root node of the tree

naryTree.height(NaryNode) ⇒ number

if naryNode is not specified, height returns the height of the naryTree from the root to the leaf nodes if naryNode is specified, height return the height of the naryTree from the naryNode to the leaf nodes

Kind: instance method of NaryTree
Returns: number - height integer - the height of the tree in levels from the reference node to the deepest leaf node

ParamTypeDescription
NaryNodeObjectnode object - an n-ary node present in this tree

naryTree.isEmpty() ⇒ boolean

Returns a boolean value reflecting whether the n-ary tree is empty or not

Kind: instance method of NaryTree
Returns: boolean - boolean value - is the tree empty?

naryTree.iterator(node)

returns a level-order iterator for this n-ary tree

Kind: instance method of NaryTree

ParamTypeDescription
nodeObjectNaryNode object - the parent n-ary node of the tree or contained subtree

naryTree.levelOrderIterator(node)

returns a level-order iterator for this n-ary tree

Kind: instance method of NaryTree

ParamTypeDescription
nodeObjectNaryNode object - the parent n-ary node of the tree or contained subtree

naryTree.preOrderIterator(node, [node])

returns a pre-order iterator for this n-ary tree

Kind: instance method of NaryTree

ParamTypeDefaultDescription
nodeObjectNaryNoe object - the parent n-ary node
[node]numberthis._modCountnumber - the modCount value is used to track whether the tree has been modified during the iteration, which is an error condition

naryTree.remove(obj, parent) ⇒ boolean

If obj is not specified, remove removes the first item in the n-ary tree If obj is specified, remove removes the first occurrence of the specified object If object and parent are specified, remove removes the first occurrence of the specified object which is a descendant of the parent obj

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
objObjectan object stored in any node
parentObjectNaryNode parent object - the parent n-ary node

naryTree.removeNode(naryNode) ⇒ boolean

Removed the n-ary Node passed as a parameter. If no value is passed, the root node is used.

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
naryNodeObjectnode object - the n-ary node to remove

naryTree.removeSubtree(obj, parentNode) ⇒ boolean

Removes the complete subtree having the specified object in the subtree's root node If parent node is specified the object must be underneath the parent node

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
objObjectobject - an object stored in any node
parentNodeObjectNaryNode parent node - the parent n-ary node

naryTree.removeNodeSubtree(naryNode) ⇒ boolean

Removes the complete subtree where the naryTree node parameter is the root node of the sub-tree

Kind: instance method of NaryTree
Returns: boolean - boolean value

ParamTypeDescription
naryNodeObjectnode object - an n-ary node positioned at the root of the subtree to remove

naryTree.size(naryNode) ⇒ number

If naryNode is not specified, returns in the current size of the n-ary tree as a node count If naryNode is specified, returns in the current size of the n-ary tree rooted from the specified node as a node count

Kind: instance method of NaryTree
Returns: number - integer value - a count of the nodes comprising the tree rooted at the specified node parameter

ParamTypeDescription
naryNodeObjectparent node - the parent n-ary node

naryTree.toString([naryNode], [relLevel]) ⇒ string

If naryNode is not specified, returns a string representation of the entire n-ary tree If naryNode is specified, returns a string representation of the n-ary tree from the specified naryNode

Kind: instance method of NaryTree
Returns: string - a string representation of the nodes in this n-ary tree instance

ParamTypeDefaultDescription
[naryNode]Objectthis._rootNaryNode object - an optional parameter specifying the root node of the tree or subtree to be printed as a string representation
[relLevel]number1number - a number used internally to track the correct level indention through recursive calls

naryTree.toJSON([naryNode], [relLevel]) ⇒ string

If naryNode is not specified, returns a JSON representation of the entire n-ary tree If naryNode is specified, returns a JSON representation of the n-ary tree from the specified naryNode

Kind: instance method of NaryTree
Returns: string - a JSON encoded string representation of the nodes in this n-ary tree instance

ParamTypeDefaultDescription
[naryNode]Objectthis._rootNaryNode object - an optional parameter specifying the root node of the tree or subtree to be converted to a JSON encoded representation
[relLevel]number1number - a number used internally to track the correct level indention through recursive calls