summaryrefslogtreecommitdiff
path: root/tree.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'tree.tcl')
-rw-r--r--tree.tcl24
1 files changed, 24 insertions, 0 deletions
diff --git a/tree.tcl b/tree.tcl
index d897c51..38a0a94 100644
--- a/tree.tcl
+++ b/tree.tcl
@@ -60,6 +60,10 @@ package require oo
# THe default index is "end"
# Returns the name of the newly added node
#
+# $pt delete <nodename>
+#
+# Delete the given node and all it's children.
+#
# $pt walk <nodename> dfs|bfs {actionvar nodevar} <code>
#
# Walks the tree starting from the given node, either breadth first (bfs)
@@ -155,6 +159,26 @@ tree method insert {node {index end}} {
return $childname
}
+tree method delete {node} {
+ if {$node eq "root"} {
+ return -code error "can't delete root node"
+ }
+ $self walk $node dfs {action subnode} {
+ if {$action eq "exit"} {
+ # Remove the node
+ dict unset tree $subnode
+ # And remove as a child of our parent
+ set parent [$self parent $subnode]
+ if {$parent ne ""} {
+ set siblings [dict get $children $parent]
+ set i [lsearch $siblings $subnode]
+ dict set children $parent [lreplace $siblings $i $i]
+ }
+ }
+ }
+}
+
+
tree method lappend {node key args} {
if {[dict exists $tree $node $key]} {
set result [dict get $tree $node $key]