\ Where tbase is the existing tree structure, and t is the new tree node. : tree-insert-child { t tbase } for t tree->key@ tbase tree->key@ tree-prefix? if tbase tree->children@ 0= if \ This base has no children yet, so set t as its child. t tbase tree->children! exit else tbase tree->children@ to tbase tbase t tree-find-relation tree-sibling = if \ We went deep enough to find a sibling of t. tbase t tree-insert-sibling exit then next then else tbase tree->next@ dup to tbase if next then then endfor abort" no such prefix" ; \ NOTE: this frees the input string : tree-remove { s treevar | tbase prev } treevar tree@ to tbase 0 to prev for s tbase tree->key@ tree-prefix? if \ We found the right prefix. \ Now test if tbase is the exact node we want. s tbase tree->key@ string-equal if prev tbase tree-link-prev-to-next tbase tree-free else \ Check if the first child is the node we want to remove. tbase tree->children@ dup tree->key@ s string-equal if \ Point tbase's ->children to the ->next of the node we found. dup tree->next@ tbase tree->children! \ Free the node we found. tree-free else \ Go to the next node. dup to tbase if next then then then else \ Go to the next node if non-null. tbase to prev tbase tree->next@ dup to tbase if next then then endfor s string-free ;