struct::tree_v1(3tcl)
โจ NAME โจ
struct::tree_v1 โ Create and manipulate tree objects
This is from the Tcl Data Structures manual, section 3tcl. Hey bestie, we're about to get into it. ๐
๐ฆ SYNOPSIS ๐ฆ
package require Tcl 8.5 9
package require struct::tree ?1.2.3?
treeName option ?arg arg ...?
treeName append node ?-key key? value
treeName children node
treeName cut node
treeName delete node ?node ...?
treeName depth node
treeName destroy
treeName exists node
treeName get node ?-key key?
treeName getall node
treeName keys node
treeName keyexists node ?-key key?
treeName index node
treeName insert parent index ?child ?child ...??
treeName isleaf node
treeName lappend node ?-key key? value
treeName move parent index node ?node ...?
treeName next node
treeName numchildren node
treeName parent node
treeName previous node
treeName set node ?-key key? ?value?
treeName size ?node?
treeName splice parent from ?to? ?child?
treeName swap node1 node2
treeName unset node ?-key key?
treeName walk node ?-order order? ?-type type? -command cmd
๐ DESCRIPTION ๐
The ::struct::tree command creates a new tree object with an associated global Tcl command whose name is treeName. This command may be used to invoke various operations on the tree. It has the following general form:
treeName option ?arg arg ...?
Option and the args determine the exact behavior of the command.
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:
- Trees are accessed through an object command, whereas arrays are accessed as variables. (This means trees cannot be local to a procedure.)
- Trees have a hierarchical structure, whereas an array is just an unordered collection.
- Each node of a tree has a separate collection of attributes and values. This is like an array where every value is a dictionary.
The following commands are possible for tree objects:
treeName append node ?-key key? valueโ Glues value onto the end of one of the keyed values hanging off node. If you don't pass-key, the keydatais assumed. โจtreeName children nodeโ Gives you back a list of the children of node. Like a little headcount of the immediate fam. ๐ซถtreeName cut nodeโ Takes node out of the tree, but not its children. Those children get rehomed as children of node's parent, at the exact index where node used to sit. No orphan vibes. ๐treeName delete node ?node ...?โ Yeets the specified nodes out of the tree. All of those nodes' children get removed too, so nobody ends up orphaned. ๐treeName depth nodeโ Tells you how many steps it takes to get from node up to the root node. ๐treeName destroyโ Destroys the tree, including its storage space and associated command. It's giving full cleanup, no crumbs. ๐งtreeName exists nodeโ Returns true if node exists in the tree. Vibe check: present or not? โญtreeName get node ?-key key?โ Returns the value associated with the key key for node. No-key? Then the keydatais assumed. ๐treeName getall nodeโ Returns a serialized list of key/value pairs (suitable for use with[array set]) for node. It's the whole dictionary moment. ๐treeName keys nodeโ Returns a list of keys for node. Just the keys, bestie. ๐treeName keyexists node ?-key key?โ Returns true if the specified key exists for node. No-key? The keydatais assumed. โtreeName index nodeโ Returns the index of node in its parent's list of children. For example, if a node hasnodeFoo,nodeBar, andnodeBazas children, in that order, the index ofnodeBaris1. Math is math. ๐งฎtreeName insert parent index ?child ?child ...??โ Inserts one or more nodes into the tree as children of parent. They go in the order you give them. If parent isroot, it refers to the root of the tree. The new nodes get added to parent's child list at index. index can beend, in which case the new nodes go after the current last child. 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 is added and a name is generated for it. The generated name is of the formnodex, wherexis 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. Big slay. ๐treeName isleaf nodeโ Returns true if node is a leaf of the tree (if node has no children), false otherwise. Leaf = no kids, just vibes. ๐treeName lappend node ?-key key? valueโ Appends value (as a list) to one of the keyed values associated with a node. No-key? The keydatais assumed. ๐treeName move parent index node ?node ...?โ Makes the specified nodes children of parent, inserting them into parent's child list at index. Important: the command takes all nodes out of the tree before inserting them under the new parent, and it determines the position to place them into after the removal, before the re-insertion. This behaviour is important when moving one or more nodes to a different index without changing their parent node. ๐treeName next nodeโ Returns the right sibling of node, or the empty string if node was the last child of its parent. Right-side neighbor check. ๐treeName numchildren nodeโ Returns the number of immediate children of node. Not the whole bloodline, just the direct kids. ๐ถtreeName parent nodeโ Returns the parent of node. Who's your mother? ๐treeName previous nodeโ Returns the left sibling of node, or the empty string if node was the first child of its parent. Left-side neighbor check. ๐treeName set node ?-key key? ?value?โ Sets or gets one of the keyed values associated with a node. No-key? The keydatais assumed. Every node added to a tree automatically gets the value""assigned to the keydata. A node may have any number of keyed values associated with it. If value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key. โจtreeName size ?node?โ Returns a count of the number of descendants of the node node; if no node is specified,rootis assumed. It's the whole downstream tree count. ๐ณtreeName splice parent from ?to? ?child?โ Inserts a node named child into the tree as a child of parent. If parent isroot, it refers to the root of the tree. The new node is added to parent's child list at the index given by from. The children of parent which are in the range of the indices from and to are made children of child. If to is not specified it defaults toend. If no name is given for child, a name is generated for the new node. The generated name is of the formnodex, wherexis a number. The return result from this command is the name of the new node. ๐ชtreeName swap node1 node2โ Swaps the position of node1 and node2 in the tree. Trading places, iconic. ๐treeName unset node ?-key key?โ Removes a keyed value from the node node. No-key? The keydatais assumed. Bye key. ๐treeName walk node ?-order order? ?-type type? -command cmdโ Performs a breadth-first or depth-first walk of the tree starting at node. The type of walk, breadth-first or depth-first, is determined by the value of type;bfsindicates breadth-first,dfsindicates depth-first. Depth-first is the default. The order of the walk, pre-, post-, both- or in-order, is determined by the value of order;preindicates pre-order,postindicates post-order,bothindicates both-order andinindicates in-order. Pre-order is the default. ๐ถโโ๏ธ
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 is walked. The combination of a breadth-first walk with in-order is illegal.
โ ๏ธ Not me warning you: breadth-first + in-order? Illegal. Don't do it. ๐ โโ๏ธ
As the walk progresses, the command cmd will be evaluated at each node. Percent substitution will be performed on cmd before evaluation, just as in a bind script. The following substitutions are recognized:
%%โ Insert the literal%character.%tโ Name of the tree object.%nโ Name of the current node.%aโ Name of the action occurring; one ofenter,leave, orvisit.enteractions occur during pre-order walks;leaveactions occur during post-order walks;visitactions occur during in-order walks. In a both-order walk, the command will be evaluated twice for each node; the action isenterfor the first evaluation, andleavefor the second.
๐ BUGS, IDEAS, FEEDBACK ๐
okay bestie, real talk: this doc? and the whole package it's describing? they will undoubtedly have bugs and other problems. like, not maybe. undoubtedly. so please don't come for me, come for the tracker instead ๐
- Found a bug? Report it in the category
struct :: treeof the Tcllib Trackers โ [http://core.tcl.tk/tcllib/reportlist](http://core.tcl.tk/tcllib/reportlist) ๐ซถ - Got an idea for an enhancement? Also slide it into the tracker โ for either the package and/or the documentation. both are welcome, no gatekeeping โจ
๐ธ proposing code changes? read this first ๐ธ
When proposing code changes, please provide unified diffs, i.e. the output of diff -u.
and one more thing that is super important, no cap:
Note further that attachments are strongly preferred over inlined patches.
the way to attach stuff: go to the Edit form of the ticket immediately after its creation, and then use the left-most button in the secondary navigation bar. that's the whole move, iykyk ๐ซ
๐ KEYWORDS ๐
tree
๐ฆ CATEGORY ๐ฆ
Data structures
๐ COPYRIGHT ๐
Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's the page, bestie!! ๐ซถ this was struct::tree_v1(3tcl) from tcllib, version 1.2.3 โ go forth and build ur trees, u absolute icon ๐๐ณโจ
๐ฆ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 ยท Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 ยท Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 ยท Tcl
- Copyright (c) 2002 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms