struct::tree_v1(n)
โจ NAME โจ
struct::tree_v1 โ no cap, this is the one that creates and manipulates tree objects ๐ณ๐ (it's a Tcl Data Structures page, bestie, iykyk)
๐ SYNOPSIS ๐
okay so here's the vibe check on how you actually call this thing:
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 ๐
so the ::struct::tree command makes you a brand new tree object, and it comes with a global Tcl command attached to it whose name is treeName. that command is your girl โ you use her to invoke all the various operations on the tree. general form is giving:
treeName option ?arg arg ...?
Option and the args decide the exact behavior of the command. that's it, that's the whole deal ๐
a tree is a collection of named elements called nodes, and one of them is special-cased as the root, plus there's a relation ("parenthood") that drops a whole hierarchical structure on top of the nodes. (shoutout Data Structures and Algorithms; Aho, Hopcroft and Ullman; Addison-Wesley, 1987 ๐โจ). and like, on top of just maintaining the node relationships, this tree implementation also lets you associate any number of keyed values with each node. yes, any number. go feral.
the element names can be arbitrary strings, fr fr.
so a tree is lowkey 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 โ not me finding that out the hard way ๐ญ)
- 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. iconic behaviour honestly ๐
๐ฆ The Commands ๐ฆ
here's every command you can throw at a tree object, bestie. buckle up.
treeName append node ?-key key? value
appends a value to one of the keyed values associated with a node. if no key is specified, the key data is assumed.
treeName children node
returns a list of the children of node. ๐ผ
treeName cut node
removes the node specified by node from the tree โ but not its children. the children of node get made children of the parent of the node, at the index at which node was located. so the kids get adopted by grandma, basically ๐ซถ
treeName delete node ?node ...?
removes the specified nodes from the tree. all of the nodes' children will be removed as well, to prevent orphaned nodes. (yeah, it takes the whole subtree down with it. dramatic but necessary ๐)
treeName depth node
returns the number of steps from node node to the root node.
treeName destroy
destroys the tree, including its storage space and associated command. it's giving main character exit ๐
treeName exists node
returns true if the specified node exists in the tree.
treeName get node ?-key key?
returns the value associated with the key key for the node node. if no key is specified, the key data is assumed.
treeName getall node
returns a serialized list of key/value pairs (suitable for use with [array set]) for the node. handy for when you want the whole outfit at once โจ
treeName keys node
returns a list of keys for the node.
treeName keyexists node ?-key key?
returns true if the specified key exists for the node. if no key is specified, the key data is assumed.
treeName index node
returns the index of node in its parent's list of children. for example, if a node has nodeFoo, nodeBar, and nodeBaz as children, in that order, the index of nodeBar is 1. counting from zero, bestie, we're programmers ๐งฎ
treeName insert parent index ?child ?child ...??
inserts one or more nodes into the tree as children of the node parent. the nodes get added in the order they're given. if parent is root, it refers to the root of the tree. the new nodes get added to the parent node's child list at the index given by index. the index can be end, in which case the new nodes will be added 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. glow-up relocation, honestly.
if no child is specified, a single node gets 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 (":"). no spaces, no colons, periodt.
the return result from this command is a list of nodes added.
treeName isleaf node
returns true if node is a leaf of the tree (if node has no children), false otherwise. ๐
treeName lappend node ?-key key? value
appends a value (as a list) to one of the keyed values associated with a node. if no key is specified, the key data is assumed.
treeName move parent index node ?node ...?
makes the specified nodes children of parent, inserting them into the parent's child list at the index given by index. heads up bestie: the command will take all the 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 matters a lot when you're moving one or more nodes to a different index without changing their parent node. so like, the order of operations is the whole tea here ๐ต
treeName next node
returns the right sibling of node, or the empty string if node was the last child of its parent.
treeName numchildren node
returns the number of immediate children of node.
treeName parent node
returns the parent of node. ๐ฉโ๐ง
treeName previous node
returns the left sibling of node, or the empty string if node was the first child of its parent.
treeName set node ?-key key? ?value?
sets or gets one of the keyed values associated with a node. if no key is specified, the key data is assumed. every node that gets added to a tree has the value "" assigned to the key data automatically. 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. dual purpose queen ๐
treeName size ?node?
returns a count of the number of descendants of the node node; if no node is specified, root is assumed.
treeName splice parent from ?to? ?child?
inserts a node named child into the tree as a child of the node parent. if parent is root, it refers to the root of the tree. the new node gets added to the parent node'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 the value of to is not specified it defaults to end. if no name is given for child, a name will be generated for the new node. the generated name is of the form nodex, where x is a number. the return result from this command is the name of the new node. it's giving surgical precision ๐งฌโจ
treeName swap node1 node2
swaps the position of node1 and node2 in the tree. ๐
treeName unset node ?-key key?
removes a keyed value from the node node. if no key is specified, the key data is assumed.
treeName walk node ?-order order? ?-type type? -command cmd
performs a breadth-first or depth-first walk of the tree starting at the node node. ๐
the type of walk, breadth-first or depth-first, is determined by the value of type; bfs indicates breadth-first, dfs indicates 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; pre indicates pre-order, post indicates post-order, both indicates both-order and in indicates in-order. pre-order is the default.
here's what all that actually means, bestie:
- pre-order walking means 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 a parent node is visited after any of its children.
- both-order walking means a parent node is visited before and after any of its children.
- in-order walking means 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. don't do it, it's not the vibe ๐ซ
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 for a sec. this doc? and the whole package it's describing? they will undoubtedly have bugs and other problems. like, no cap, that's just how software is. it's not a flaw in the vibe, it's the vibe.
so if you find one, pls pls pls report it in the category struct :: tree of the Tcllib Trackers here: http://core.tcl.tk/tcllib/reportlist
and if you have ideas for enhancements โ for the package, for the docs, for either one โ those go to the same place and they are so so welcome. we want them. give them to us. ๐ซถ
when you're proposing code changes, you gotta send unified diffs, i.e. the output of diff -u. that's the format. that's the whole thing. don't be shy about it, just do it.
also, lowkey important: attachments are strongly preferred over inlined patches. like, strongly. so how do you attach stuff? here's the move:
- create the ticket
- go straight to the Edit form of that ticket
- use the left-most button in the secondary navigation bar
that's it. that's the whole ritual. โจ
๐ KEYWORDS ๐
tree
short. sweet. iconic. ๐
๐ CATEGORY ๐
Data structures
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net>
and that's the page bestie!! ๐ธ this one's from tcllib, version 1.2.3, and the page itself is struct::tree_v1(n) โ go forth and tree responsibly ๐๐ฆ
๐ฆ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 ยท TCL
- fedora 44: tcllib 2.0-4.fc44 ยท TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- 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