The goog.structs.TreeNode Class

goog.structs.TreeNode
> goog.structs.Node

goog.structs.TreeNode(key, value)

Generic tree node data structure with arbitrary number of child nodes. It is possible to create a dynamic tree structure by overriding {@link #getParent} and {@link #getChildren} in a subclass. All other getters will automatically work.

key {*}
Key.
value {*}
Value.

.addChild(child)

Appends a child node to this node.

child {!goog.structs.TreeNode}
Orphan child node.

.addChildAt(child, index)

Inserts a child node at the given index.

child {!goog.structs.TreeNode}
Orphan child node.
index {number}
The position to insert at.

.clone()

Clones a node and returns a new node.

returns {!goog.structs.TreeNode}
Clone of the tree node without its parent and child nodes. The key and the value are copied by reference.

.contains(node)

Tells whether this node is the ancestor of the given node.

node {!goog.structs.TreeNode}
A node.
returns {boolean}
Whether this node is the ancestor of {@code node}.

.deepClone()

returns {!goog.structs.TreeNode}
Clone of the subtree with this node as root.

.findCommonAncestor(var_args)

Finds the deepest common ancestor of the given nodes. The concept of ancestor is not strict in this case, it includes the node itself.

var_args {...!goog.structs.TreeNode}
The nodes.
returns {goog.structs.TreeNode}
The common ancestor of the nodes or null if they are from different trees.

.forEachChild(f, opt_this)

Traverses all child nodes.

f {function(!goog.structs.TreeNode, number, !Array.<!goog.structs.TreeNode>)}
Callback function. It takes the node, its index and the array of all child nodes as arguments.
opt_this {Object=}
The object to be used as the value of {@code this} within {@code f}.

.forEachDescendant(f, opt_this)

Traverses all child nodes recursively in preorder.

f {function(!goog.structs.TreeNode)}
Callback function. It takes the node as argument.
opt_this {Object=}
The object to be used as the value of {@code this} within {@code f}.

.getAncestors()

returns {!Array.<!goog.structs.TreeNode>}
All ancestor nodes in bottom-up order.

.getChildAt(index)

Gets the child node of this node at the given index.

index {number}
Child index.
returns {goog.structs.TreeNode}
The node at the given index or null if not found.

.getChildCount()

returns {number}
The number of children.

.getChildren()

returns {!Array.<!goog.structs.TreeNode>}
Immutable child nodes.

.getDepth()

returns {number}
The number of ancestors of the node.

.getKey()

Inherited from goog.structs.Node .

Gets the key.

returns {*}
The key.

.getParent()

returns {goog.structs.TreeNode}
Parent node or null if it has no parent.

.getRoot()

returns {!goog.structs.TreeNode}
The root of the tree structure, i.e. the farthest ancestor of the node or the node itself if it has no parents.

.getSubtreeKeys()

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}
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'].

.getValue()

Inherited from goog.structs.Node .

Gets the value.

returns {*}
The value.

.isLastChild()

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.

.isLeaf()

returns {boolean}
Whether the node is a leaf node.

.removeChild(child)

Removes the given child node of this node.

child {goog.structs.TreeNode}
The node to remove.
returns {goog.structs.TreeNode}
The removed node if any.

.removeChildAt(index)

Removes the child node at the given index.

index {number}
The position to remove from.
returns {goog.structs.TreeNode}
The removed node if any.

.removeChildren()

Removes all child nodes of this node.

.replaceChild(newChild, oldChild)

Replaces the given child node.

newChild {!goog.structs.TreeNode}
New node to replace {@code oldChild}. It must not have parent node.
oldChild {!goog.structs.TreeNode}
Existing child node to be replaced.
returns {!goog.structs.TreeNode}
The replaced child node detached from its parent.

.replaceChildAt(newChild, index)

Replaces a child node at the given index.

newChild {!goog.structs.TreeNode}
Child node to set. It must not have parent node.
index {number}
Valid index of the old child to replace.
returns {!goog.structs.TreeNode}
The original child node, detached from its parent.

.traverse(f, opt_this)

Traverses the subtree with the possibility to skip branches. Starts with this node, and visits the descendant nodes depth-first, in preorder.

f {function(!goog.structs.TreeNode): (boolean|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 {Object=}
The object to be used as the value of {@code this} within {@code f}.