Implement a Binary Search Tree in Larzscript
Insert values into a binary search tree and read them back in sorted order.
A deliberately simple, readable reference BST (no AVL/red-black self-balancing) - the point is seeing exactly how insert/inorder/height work, not raw performance.
larzscript pkg install tree
Code
import "tree" as bst
let t = bst.new()
for x in [5, 3, 8, 1, 4, 7, 9] { bst.insert(t, x) }
print(bst.inorder(t))
print(bst.contains(t, 4))
print(bst.height(t))Output
[1, 3, 4, 5, 7, 8, 9] true 2
Related
larztreeA readable, node-based binary search tree→Implement a Stack and a Queue in LarzscriptThe two most basic linear data structures - last-in-first-out and first-in-first-out - named explicitly instead of just using a bare list.→Implement a Priority Queue (Binary Heap) in LarzscriptAlways get the smallest (or, with a key function, largest/custom-ordered) item next - the classic array-backed binary heap.→