aboutsummaryrefslogtreecommitdiff
path: root/avl.kpl
diff options
context:
space:
mode:
authornodist <kevin.comas.git@gmail.com>2026-11-13 16:28:06 -0500
committernodist <kevin.comas.git@gmail.com>2026-11-13 16:28:06 -0500
commitc5a80c0c4a1da4665da90a98eaf9f3870d18dd6b (patch)
treeaececbc71669dee6546e80164501b22a3a14ed0b /avl.kpl
parent26dfb9719d8101cb12611209ac1004e66902aa86 (diff)
rewriteHEADmain
Diffstat (limited to 'avl.kpl')
-rw-r--r--avl.kpl125
1 files changed, 0 insertions, 125 deletions
diff --git a/avl.kpl b/avl.kpl
deleted file mode 100644
index db53e60..0000000
--- a/avl.kpl
+++ /dev/null
@@ -1,125 +0,0 @@
-
-// AVL Tree Example
-
-Node `alias Nullable[Table[Node left; Node right; I64 height; Any key; Any value]]
-
-Avl_Compare_Fn `alias Fn[Result[I64 value; Error error]; Any key_a; Any key_b]
-
-AVL `alias Table[Node root; U64 size; Avl_Compare_Fn compare_fn]
-
-height : `{[Node node] ? node {[node] node.height } { 0 } }
-
-max : `{[I64 a; I64 b] ? a > b { a } { b } }
-
-get_balance : `{[Node n]
- ? n {
- - (height `call n.left; height `call n.right)
- } {
- 0
- }
-}
-
-min_value_node : `{[Node n]
- @ n.left { n : n.left }
- n
-}
-
-new_node : `{[Any key; Any value]
- Node (.left `null; .right `null; .height 1; .key key; .value value)
-}
-
-right_rotate : `{[Node y]
- ? !y { `return `null }
- x : y.left;
- t2 : x.right;
- y.height : 1 + max `call (height `call y.left; height `call y.right)
- x.height : 1 + max `call (height `call x.left; height `call x.right)
- x
-}
-
-left_rotate : `{[Node x]
- ? !x { `return `null }
- y : x.right;
- t2 : y.left;
- x.height : 1 + max `call (height `call x.left; height `call x.right)
- y.height : 1 + max `call (height `call y.left; height `call y.right)
- y
-}
-
-`export avl_init : `{[Avl_Compare_Fn compare_fn]
- AVL (.root `null; .size 0; .compare_fn compare_fn)
-}
-
-`export avl_insert : `{[AVL avl; Any key; Any value]
- insert : `{[Node n]
- ? !n {
- avl.size +: 1
- `return new_node `call (key; value)
- }
- cmp : avl.compare_fn `call (n.key; key)
- ? {
- cmp < 0 { n.left : insert `call n.left }
- cmp > 0 { n.right : insert `call n.right }
- {
- n.value : value
- `return n
- }
- }
- n.height : 1 + max `call (height `call n.left; height `call n.right)
- balance : get_balance `call n
- ? {
- & (balance > 1; cmp < 0) { right_rotate `call n }
- & (balance < -1; cmp > 0) { left_rotate `call n }
- & (balance > 1; cmp > 0) {
- n.left : left_rotate `call n.left
- right_rotate `call n
- }
- & (balance < -1; cmp < 0) {
- n.right : right_rotate `call n.right
- left_rotate `call n
- }
- { n }
- }
- }
- avl.root : insert `call avl.root
- Void
-}
-
-`export avl_delete : `{[Avl avl; Any key]
- delete : `{[Node n; Any key]
- ? !n { `return n }
- cmp : avl.compare_fn `call (n.key; key)
- ? {
- cmp < 0 { n.left : delete `call (n.left; key)
- cmp > 0 { n.right : delete `call (n.right; key)
- {
- ? | (!n.left; !n.right) {
- n : ? n.left { n.left } { n.right }
- } {
- tmp : min_value_node `call n.right
- n.key : tmp.key
- n.value : tmp.value
- n.right : delete `call (n.right; tmp.key)
- }
- }
- }
- ? !n { `return n }
- n.height : 1 + max `call (height `call n.left; height `call n.right)
- balance : get_balance `call n
- ? {
- & (balance > 1; 0 <= get_balance `call n.left) { right_rotate `call n }
- & (balance > 1; 0 > get_balance `call n.left {
- n.left : left_rotate `call n.left
- right_rotate `call n
- }
- & (balance < -1; 0 >= get_balance `call n.right) { left_rotate `call n }
- & (balance < -1; 0 > get_balance `call n.right) {
- n.right : right_rotate `call n.right
- left_rotate `call n
- }
- { n }
- }
- }
- avl.root : delete `call (avl.root; key)
- Void
-}