aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Tree
diff options
context:
space:
mode:
authorMarvin Borner2023-03-07 00:20:52 +0100
committerMarvin Borner2023-03-07 00:22:22 +0100
commitea98cabbe4515bd5248f44214ad870858f1594aa (patch)
tree85b5a52a10dec556729f8c04a73e6b3e415ed07b /std/Tree
parent9ef10406c067d0a0532d609212a94519af402b87 (diff)
Useful additions
hehe
Diffstat (limited to 'std/Tree')
-rw-r--r--std/Tree/Balanced.bruijn13
1 files changed, 13 insertions, 0 deletions
diff --git a/std/Tree/Balanced.bruijn b/std/Tree/Balanced.bruijn
index fe4a155..ab0a921 100644
--- a/std/Tree/Balanced.bruijn
+++ b/std/Tree/Balanced.bruijn
@@ -5,6 +5,7 @@
:import std/Combinator .
:import std/List L
+:import std/Logic .
:import std/Number .
:import std/Option .
:import std/Pair .
@@ -82,13 +83,25 @@ insert z [[[rec]]] ⧗ Number → (Option BalancedTree) → (Option BalancedTree
lt some (node (2 1 //(!0)) ?(!0) \\(!0))
gt some (node //(!0) ?(!0) (2 1 \\(!0)))
+# returns true if a number is in a tree
+has? z [[[rec]]] ⧗ Number → (Option BalancedTree) → Boolean
+ rec none? 0 false (u 1 ?(!0))
+ u compare-case eq lt gt
+ eq true
+ lt 2 1 //(!0)
+ gt 2 1 \\(!0)
+
+:test (has? (+42) empty) (false)
+
# number of elements in tree (slow)
size z [[rec]] ⧗ (Option BalancedTree) → Number
rec none? 0 case-empty case-full
case-full (1 //(!0)) + (1 \\(!0)) + (+1)
case-empty (+0)
+# converts a tree to a list
list! z [[map-or L.empty go 0]] ⧗ (Option BalancedTree) → (List Number)
go [L.append (L.append (2 //0) L.{}(?0)) (2 \\0)]
+# converts a list to a tree
from-list L.foldr insert empty ⧗ (List Number) → (Option BalancedTree)