structs.TreeNode Extends goog.structs.Node
Generic tree node data structure with arbitrary number of child nodes. It is possible to create a dynamic tree structure by overriding #getParent and #getChildren in a subclass. All other getters will automatically work.

Inheritance

Constructor

goog.structs.TreeNode(keyvalue)

Parameters

key : KEY
Key.
value : VALUE
Value.

Instance Methods

Public Protected Private
addChild(child)
Appends a child node to this node.
Arguments:
child : !goog.structs.TreeNode.<KEY, VALUE>
Orphan child node.
code »
addChildAt(childindex)
Inserts a child node at the given index.
Arguments:
child : !goog.structs.TreeNode.<KEY, VALUE>
Orphan child node.
index : number
The position to insert at.
code »
clone() !goog.structs.TreeNode
No description.
Returns: !goog.structs.TreeNode  Clone of the tree node without its parent and child nodes. The key and the value are copied by reference.
code »
contains(node) boolean
Tells whether this node is the ancestor of the given node.
Arguments:
node : !goog.structs.TreeNode.<KEY, VALUE>
A node.
Returns: boolean  Whether this node is the ancestor of node.
code »
deepClone() !goog.structs.TreeNode
No description.
Returns: !goog.structs.TreeNode  Clone of the subtree with this node as root.
code »
forEachChild(fopt_this)
Traverses all child nodes.
Arguments:
f : function(this:THIS, !goog.structs.TreeNode.<KEY, VALUE>, number, !Array.<!goog.structs.TreeNode.<K>>, VALUE>>)
Callback function. It takes the node, its index and the array of all child nodes as arguments.
opt_this : THIS=
The object to be used as the value of this within f.
code »
forEachDescendant(fopt_this)
Traverses all child nodes recursively in preorder.
Arguments:
f : function(!goog.structs.TreeNode.<KEY, VALUE>)
Callback function. It takes the node as argument.
opt_this : Object=
The object to be used as the value of this within f.
code »
getAncestors() !Array.<!goog.structs.TreeNode.<KE>, VALUE>>
No description.
Returns: !Array.<!goog.structs.TreeNode.<KE>, VALUE>>  All ancestor nodes in bottom-up order.
code »
getChildAt(index) goog.structs.TreeNode.<KEY, VALUE>
Gets the child node of this node at the given index.
Arguments:
index : number
Child index.
Returns: goog.structs.TreeNode.<KEY, VALUE>  The node at the given index or null if not found.
code »
getChildCount() number
No description.
Returns: number  The number of children.
code »
getChildren() !Array.<!goog.structs.TreeNode.<KE>, VALUE>>
No description.
Returns: !Array.<!goog.structs.TreeNode.<KE>, VALUE>>  Immutable child nodes.
code »
getDepth() number
No description.
Returns: number  The number of ancestors of the node.
code »
getNodeByKey(key) goog.structs.TreeNode.<KEY, VALUE>
Returns a node whose key matches the given one in the hierarchy rooted at this node. The hierarchy is searched using an in-order traversal.
Arguments:
key : KEY
The key to search for.
Returns: goog.structs.TreeNode.<KEY, VALUE>  The node with the given key, or null if no node with the given key exists in the hierarchy.
code »
getParent() goog.structs.TreeNode.<KEY, VALUE>
No description.
Returns: goog.structs.TreeNode.<KEY, VALUE>  Parent node or null if it has no parent.
code »
getRoot() !goog.structs.TreeNode.<KEY, VALUE>
No description.
Returns: !goog.structs.TreeNode.<KEY, VALUE>  The root of the tree structure, i.e. the farthest ancestor of the node or the node itself if it has no parents.
code »
getSubtreeKeys() !Array.<KEY>
Builds a nested array structure from the node keys in this node's subtree to facilitate testing tree operations that change the hierarchy.
Returns: !Array.<KEY>  The structure of this node's descendants as nested array of node keys. The number of unclosed opening brackets up to a particular node is proportional to the indentation of that node in the graphical representation of the tree. Example:
      this
      |- child1
      |  L- grandchild
      L- child2
    
is represented as ['child1', ['grandchild'], 'child2'].
code »
isLastChild() boolean
Tells if the node is the last child of its parent. This method helps how to connect the tree nodes with lines: L shapes should be used before the last children and |- shapes before the rest. Schematic tree visualization:
Node1
|-Node2
| L-Node3
|   |-Node4
|   L-Node5
L-Node6
Returns: boolean  Whether the node has parent and is the last child of it.
code »
isLeaf() boolean
No description.
Returns: boolean  Whether the node is a leaf node.
code »
removeChild(child) goog.structs.TreeNode.<KEY, VALUE>
Removes the given child node of this node.
Arguments:
child : goog.structs.TreeNode.<KEY, VALUE>
The node to remove.
Returns: goog.structs.TreeNode.<KEY, VALUE>  The removed node if any.
code »
removeChildAt(index) goog.structs.TreeNode.<KEY, VALUE>
Removes the child node at the given index.
Arguments:
index : number
The position to remove from.
Returns: goog.structs.TreeNode.<KEY, VALUE>  The removed node if any.
code »
removeChildren()
Removes all child nodes of this node.
code »
replaceChild(newChildoldChild) !goog.structs.TreeNode.<KEY, VALUE>
Replaces the given child node.
Arguments:
newChild : !goog.structs.TreeNode.<KEY, VALUE>
New node to replace oldChild. It must not have parent node.
oldChild : !goog.structs.TreeNode.<KEY, VALUE>
Existing child node to be replaced.
Returns: !goog.structs.TreeNode.<KEY, VALUE>  The replaced child node detached from its parent.
code »
replaceChildAt(newChildindex) !goog.structs.TreeNode.<KEY, VALUE>
Replaces a child node at the given index.
Arguments:
newChild : !goog.structs.TreeNode.<KEY, VALUE>
Child node to set. It must not have parent node.
index : number
Valid index of the old child to replace.
Returns: !goog.structs.TreeNode.<KEY, VALUE>  The original child node, detached from its parent.
code »
setParent(parent)
Sets the parent node of this node. The callers must ensure that the parent node and only that has this node among its children.
Arguments:
parent : goog.structs.TreeNode.<KEY, VALUE>
The parent to set. If null, the node will be detached from the tree.
code »
traverse(fopt_this)
Traverses the subtree with the possibility to skip branches. Starts with this node, and visits the descendant nodes depth-first, in preorder.
Arguments:
f : function(this:THIS, !goog.structs.TreeNode.<KEY, VALUE>): (boolea> | undefined)
Callback function. It takes the node as argument. The children of this node will be visited if the callback returns true or undefined, and will be skipped if the callback returns false.
opt_this : THIS=
The object to be used as the value of this within f.
code »
clone() !goog.structs.Node.<K, V>
Clones a node and returns a new node.
Returns: !goog.structs.Node.<K, V>  A new goog.structs.Node with the same key value pair.
code »
getKey() K
Gets the key.
Returns: K  The key.
code »
getValue() V
Gets the value.
Returns: V  The value.
code »

Instance Properties

children_ :
Child nodes or null in case of leaf node.
Code »
constructor :
No description.
Code »
Reference to the parent node or null if it has no parent.
Code »
key_ :
The key.
Code »
value_ :
The value.
Code »

Static Methods

goog.structs.TreeNode.findCommonAncestor(var_args) goog.structs.TreeNode.<KEY, VALUE>
Finds the deepest common ancestor of the given nodes. The concept of ancestor is not strict in this case, it includes the node itself.
Arguments:
var_args : ...!goog.structs.TreeNode.<KEY, VALUE>
The nodes.
Returns: goog.structs.TreeNode.<KEY, VALUE>  The common ancestor of the nodes or null if they are from different trees.
code »

Static Properties

goog.structs.TreeNode.EMPTY_ARRAY_ :
Constant for empty array to avoid unnecessary allocations.
Code »
goog.structs.TreeNode.superClass_ :
No description.
Code »

Package structs

Package Reference