struct::tree(3tcl) | Tcl Data Structures | struct::tree(3tcl) |
struct::tree - Create and manipulate tree objects
package require Tcl 8.2
package require struct::tree ?2.1.1?
package require struct::list ?1.5?
::struct::tree ?treeName? ?=|:=|as|deserialize source?
treeName option ?arg arg ...?
::struct::tree::prune
treeName = sourcetree
treeName --> desttree
treeName ancestors node
treeName append node key value
treeName attr key
treeName attr key -nodes list
treeName attr key -glob globpattern
treeName attr key -regexp repattern
treeName children ?-all? node ?filter cmdprefix?
treeName cut node
treeName delete node ?node ...?
treeName depth node
treeName descendants node ?filter cmdprefix?
treeName deserialize serialization
treeName destroy
treeName exists node
treeName get node key
treeName getall node ?pattern?
treeName keys node ?pattern?
treeName keyexists node key
treeName index node
treeName insert parent index ?child ?child ...??
treeName isleaf node
treeName lappend node key value
treeName leaves
treeName move parent index node ?node ...?
treeName next node
treeName numchildren node
treeName nodes
treeName parent node
treeName previous node
treeName rename node newname
treeName rootname
treeName serialize ?node?
treeName set node key ?value?
treeName size ?node?
treeName splice parent from ?to? ?child?
treeName swap node1 node2
treeName unset node key
treeName walk node ?-order order? ?-type type? loopvar script
treeName walkproc node ?-order order? ?-type type? cmdprefix
A tree is a collection of named elements, called nodes, one of which is distinguished as a root, along with a relation ("parenthood") that places a hierarchical structure on the nodes. (Data Structures and Algorithms; Aho, Hopcroft and Ullman; Addison-Wesley, 1987). In addition to maintaining the node relationships, this tree implementation allows any number of keyed values to be associated with each node.
The element names can be arbitrary strings.
A tree is thus similar to an array, but with three important differences:
Note: The major version of the package struct has been changed to version 2.0, due to backward incompatible changes in the API of this module. Please read the section Changes for 2.0 for a full list of all changes, incompatible and otherwise.
The main commands of the package are:
If treeName is not specified a unique name will be generated by the package itself. If a source is specified the new tree will be initialized to it. For the operators =, :=, and as source is interpreted as the name of another tree object, and the assignment operator = will be executed. For deserialize the source is a serialized tree object and deserialize will be executed.
In other words
::struct::tree mytree = b
is equivalent to
::struct::tree mytree mytree = b
and
::struct::tree mytree deserialize $b
is equivalent to
::struct::tree mytree mytree deserialize $b
Two general observations beforehand:
And now the methods supported by tree objects created by this package:
This operation is in effect equivalent to
treeName deserialize [sourcetree serialize]
This operation is in effect equivalent to
desttree deserialize [treeName serialize]
The result is a dictionary mapping from node names to the value of attribute key at that node. Nodes not having the attribute key, or not passing a specified restriction, are not listed in the result.
The possible restrictions are:
Some examples:
mytree insert root end 0 ; mytree set 0 volume 30 mytree insert root end 1 mytree insert root end 2 mytree insert 0 end 3 mytree insert 0 end 4 mytree insert 4 end 5 ; mytree set 5 volume 50 mytree insert 4 end 6 proc vol {t n} { $t keyexists $n volume } proc vgt40 {t n} { if {![$t keyexists $n volume]} {return 0} expr {[$t get $n volume] > 40} } tclsh> lsort [mytree children -all root filter vol] 0 5 tclsh> lsort [mytree children -all root filter vgt40] 5 tclsh> lsort [mytree children root filter vol] 0 tclsh> puts ([lsort [mytree children root filter vgt40]]) ()
This is actually the same as "treeName children -all". descendants should be prefered, and "children -all" will be deprecated sometime in the future.
If any of the specified children already exist in treeName, those nodes will be moved from their original location to the new location indicated by this command.
If no child is specified, a single node will be added, and a name will be generated for the new node. The generated name is of the form nodex, where x is a number. If names are specified they must neither contain whitespace nor colons (":").
The return result from this command is a list of nodes added.
The result of this method has to be semantically identical over all implementations of the tree interface. This is what will enable us to copy tree data between different implementations of the same interface.
The result is a list containing containing a multiple of three elements. It is like a serialized array except that there are two values following each key. They are the names of the nodes in the serialized tree. The two values are a reference to the parent node and the attribute data, in this order.
The reference to the parent node is the empty string for the root node of the tree. For all other nodes it is the index of the parent node in the list. This means that they are integers, greater than or equal to zero, less than the length of the list, and multiples of three. The order of the nodes in the list is important insofar as it is used to reconstruct the lists of children for each node. The children of a node have to be listed in the serialization in the same order as they are listed in their parent in the tree.
The attribute data of a node is a dictionary, i.e. a list of even length containing a serialized array. For a node without attribute data the dictionary is the empty list.
Note: While the current implementation returns the root node as the first element of the list, followed by its children and their children in a depth-first traversal this is not necessarily true for other implementations. The only information a reader of the serialized data can rely on for the structure of the tree is that the root node is signaled by the empty string for the parent reference, that all other nodes refer to their parent through the index in the list, and that children occur in the same order as in their parent.
A possible serialization for the tree structure +- d +- a -+ root -+- b +- e +- c is {root {} {} a 0 {} d 3 {} e 3 {} b 0 {} c 0 {}} The above assumes that none of the nodes have attributes.
The arguments from and to are regular list indices, i.e. the form "end-n" is accepted as well.
Pre-order walking means that a parent node is visited before any of its children. For example, a breadth-first search starting from the root will visit the root, followed by all of the root's children, followed by all of the root's grandchildren. Post-order walking means that a parent node is visited after any of its children. Both-order walking means that a parent node is visited before and after any of its children. In-order walking means that a parent node is visited after its first child and before the second. This is a generalization of in-order walking for binary trees and will do the right thing if a binary tree is walked. The combination of a breadth-first walk with in-order is illegal.
As the walk progresses, the script will be evaluated at each node. The evaluation takes place in the context of the caller of the method. Regarding loop variables, these are listed in loopvar. If one only one variable is specified it will be set to the id of the node. When two variables are specified, i.e. loopvar is a true list, then the first variable will be set to the action performed at the node, and the other to the id of the node itself. All loop variables are created in the context of the caller.
There are three possible actions: enter, leave, or visit. enter actions occur during pre-order walks; leave actions occur during post-order walks; visit actions occur during in-order walks. In a both-order walk, the command will be evaluated twice for each node; the action is enter for the first evaluation, and leave for the second.
Note: The enter action for a node is always performed before the walker will look at the children of that node. This means that changes made by the script to the children of the node will immediately influence the walker and the steps it will take.
Any other manipulation, for example of nodes higher in the tree (i.e already visited), or upon leaving will have undefined results. They may succeed, error out, silently compute the wrong result, or anything in between.
At last a small table showing the relationship between the various options and the possible actions.
order type actions notes ----- ---- ----- ----- pre dfs enter parent before children post dfs leave parent after children in dfs visit parent between first and second child. both dfs enter, leave parent before and after children ----- ---- ----- ----- pre bfs enter parent before children post bfs leave parent after children in bfs -- illegal -- both bfs enter, leave parent before and after children ----- ---- ----- -----
Note the command ::struct::tree::prune. This command can be used in the walk script to force the command to ignore the children of the node we are currently at. It will throw an error if the order of traversal is either post or in as these modes visit the children before their parent, making pruning non-sensical.
The following noteworthy changes have occurred:
All functionality regarding the default attribute "data" has been removed. This default attribute does not exist anymore. All accesses to attributes have to specify the name of the attribute in question. This backward incompatible change allowed us to simplify the signature of all methods handling attributes.
Especially the flag -key is not required anymore, even more, its use is now forbidden. Please read the documentation for the methods set, get, getall, unset, append, lappend, keyexists and keys for a description of the new API's.
Please read the documentation for the methods serialize, deserialize, =, and -->, and the documentation on the construction of tree objects.
Beyond the copying of whole tree objects these new API's also enable the transfer of tree objects over arbitrary channels and for easy persistence.
The following example demonstrates the creation of new nodes:
mytree insert root end 0 ; # Create node 0, as child of the root mytree insert root end 1 2 ; # Ditto nodes 1 & 2 mytree insert 0 end 3 ; # Now create node 3 as child of node 0 mytree insert 0 end ; # Create another child of 0, with a # generated name. The name is returned # as the result of the command.
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category struct :: tree of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.
breadth-first, depth-first, in-order, node, post-order, pre-order, serialization, tree
Data structures
Copyright (c) 2002-2004,2012 Andreas Kupries <andreas_kupries@users.sourceforge.net>
2.1.1 | tcllib |