Tree
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Version: | 24 July 2005 |
| Author: | Arthur Clemens |
| Classpath: | playground.classes.data.tree.Tree |
| File last modified: | Saturday, 09 September 2006, 00:52:12 |
All sorts of information is hierarchical and can be represented by a tree structure: websites, photo albums, topic knowledge, etcetera.
Tree is a position tree: each tree node has exactly one parent node and can have multiple children. The root node has no parent.
A Tree can contain any kind of node, as long as it is a TreeNode or a subclass thereof, and if it implements its own constructor and TreeNode.createNewNode method. See BaseTreeNode.
Implementation note:
mUsesCache is default off (false) until all side cases can be catered for.
To do:
Update cache when TreeNode changes name (would this happen?). In that case name changes should occur via the Tree, not on the node directly
Clear cache
Removal of nodes (by name and object)
Clear cache
Removal of nodes (by name and object)
Summary
Constructor
Class properties
Constructor
Tree
function Tree (
inName:String)
Creates a Tree object.
Parameters:
inName:
(optional) name for this tree; useful for debugging
Class properties
ROOT_NODE_NAME
static ROOT_NODE_NAME:String = "__root__"
(read,write)
Instance properties
name
name:String
(read)
The root object of this (sub)tree.
Instance methods
addChild
function addChild (
inNewNode:Object,
inParentNode:Object,
inProperties:KeyValueList) : TreeNode
Adds a child node to a node in the tree structure. If error checking is on and a node with name
inNewNode is already in the Tree, the new node is not added.Parameters:
inNewNode :
(required) the new node to attach to the tree (type TreeNode or String). If a string, a new node is created with the passed name (the identifier name should be without spaces; functions as unique identifier, so no other nodes with this name can exist). If a TreeNode, this node is attached.
inParentNode:
(optional) existing tree node to attach the new node to. Type TreeNode or String. If no node is passed, the new node is attached to the Tree's root node.
inProperties:
(optional) a KeyValueList that stores key-value properties of the new node
Returns:
The newly added TreeNode object.
Implementation note:
Calls TreeNode.addChild.
clear
function clear (
) : Void
Deletes all nodes from the tree, including the root node.
containsNodeWithName
function containsNodeWithName (
inNodeName:String) : Boolean
Checks whether a node with given name is already attached to the Tree. Called when setUsesErrorChecking is set to true.
Returns:
True: a node exists; false: no node exists with that name.
createRootNode
function createRootNode (
inName:String) : TreeNode
Creates a root node of the default type. The default type is TreeNode, but that may be other types for Tree subclasses.
Parameters:
inName:
(optional) name of the new root node; if no name is passed, a new root node is created with the name ROOT_NODE_NAME.
Returns:
The newly created root node.
Implementation note:
Calls createNewNode.
getCount
function getCount (
) : Number
Counts the number of nodes from the root node.
getLastNode
function getLastNode (
) : TreeNode
Finds the last node in current branch of the tree.
Returns:
The last node (TreeNode).
Implementation note:
Calls TreeNode.getLastNode.
getNode
function getNode (
inNodeName:String) : TreeNode
Gets the node by looking up its name.
Parameters:
inNodeName:
name of node
Returns:
The found TreeNode object; if no node is found, returns null.
Implementation note:
Because the lookup of the node can be time costly, found nodes are stored in the (cached) associative array mNodeNamesCache. When
Calls TreeNode.getNode.
getNode is called, mNodeNamesCache is first consulted.Calls TreeNode.getNode.
getNodeWithPath
function getNodeWithPath (
inNodeNamePath:String) : TreeNode
Gets the node by looking up its name path.
Parameters:
inNodeNamePath:
name path of node (see TreeNode.namePath)
Returns:
The found TreeNode object; if no node is found, returns null.
Implementation note:
Because the lookup of the node can be time costly, found nodes are stored in the (cached) associative array mNodeNamesCache. When
Calls TreeNode.getNodeWithAbsolutePath.
getNodeWithPath is called, mNodeNamesCache is first consulted.Calls TreeNode.getNodeWithAbsolutePath.
printNodes
function printNodes (
) : Void
Prints the node structure by calling TreeNode.printNodes recursively.
setRootNode
function setRootNode (
inNode:TreeNode) : Void
Sets an existing node as root node.
Parameters:
inNode:
the TreeNode object to set as root node
Example:
mTree = new Tree(); var rootNode:MyNode = new MyNode(Tree.ROOT_NODE_NAME); mTree.setRootNode(rootNode);
setUsesCache
function setUsesCache (
inFlag:Boolean) : Void
Turns cacheing on or off.
Parameters:
inFlag:
true: Tree uses a cache to store
[node name]->node relations; false: no cache is used.setUsesErrorChecking
function setUsesErrorChecking (
inFlag:Boolean) : Void
Turns error checking on or off while adding nodes. If on, and a duplicate name is found, the new node is not added to the Tree.
Use error checking if you plan to use unique node names. Otherwise you can use TreeNode.namePath to uniquely identify nodes.
Use error checking if you plan to use unique node names. Otherwise you can use TreeNode.namePath to uniquely identify nodes.
Parameters:
inFlag:
true: error checking is performed when adding child nodes: if the node name is valid, and if the node name already exists