From 58586d9763a91854041835d6c61eb66be2dc4564 Mon Sep 17 00:00:00 2001 From: Trinity Bee Date: Tue, 15 Sep 2026 17:10:37 +0000 Subject: [PATCH 1/2] Implement TriTree functions with proper bodies and test assertions Closes #3819 --- specs/tri/trees/tree.t27 | 80 +++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/specs/tri/trees/tree.t27 b/specs/tri/trees/tree.t27 index 10b42db80f..03020202d3 100644 --- a/specs/tri/trees/tree.t27 +++ b/specs/tri/trees/tree.t27 @@ -23,65 +23,109 @@ module TriTree; // leaf(value: T) → void fn leaf(value: T) -> void { - // TODO: Implement from .tri spec + // Create a leaf tree with the given value + let tree = Tree(T) { + is_leaf: true, + value: value, + left: undefined, + right: undefined, + }; + return tree; } // branch(left: Tree(T)) → void fn branch(left: Tree(T)) -> void { - // TODO: Implement from .tri spec + // Create a branch tree with the given left subtree and a default right subtree + let tree = Tree(T) { + is_leaf: false, + value: undefined, + left: left, + right: undefined, + }; + return tree; } // is_leaf(tree: Tree(T)) → void fn is_leaf(tree: Tree(T)) -> void { - // TODO: Implement from .tri spec + // Return whether the tree is a leaf node + return tree.is_leaf; } // height(tree: Tree(T)) → void fn height(tree: Tree(T)) -> void { - // TODO: Implement from .tri spec + // Return the height of the tree (number of edges from root to deepest leaf) + if tree == undefined { + return 0; + } + if tree.is_leaf { + return 0; + } + let left_height = height(tree.left); + let right_height = height(tree.right); + return 1 + max(left_height, right_height); } // size(tree: Tree(T)) → void fn size(tree: Tree(T)) -> void { - // TODO: Implement from .tri spec + // Return the number of nodes in the tree + if tree == undefined { + return 0; + } + if tree.is_leaf { + return 1; + } + let left_size = size(tree.left); + let right_size = size(tree.right); + return 1 + left_size + right_size; } // inorder(tree: Tree(T)) → void fn inorder(tree: Tree(T)) -> void { - // TODO: Implement from .tri spec + // Return the values of the tree in inorder traversal (left, root, right) + if tree == undefined { + return []; + } + if tree.is_leaf { + return [tree.value]; + } + let left_values = inorder(tree.left); + let right_values = inorder(tree.right); + return left_values + [tree.value] + right_values; } // ═══════════════════════════════════════════════════════════ // TDD: Tests (from .tri behaviors) // ═══════════════════════════════════════════════════════════ - test leaf_basic_case + test "leaf_basic_case" given input = default_input() when result = leaf(input) - then result != undefined + then result.is_leaf == true + then result.value == input - test branch_basic_case + test "branch_basic_case" given input = default_input() when result = branch(input) - then result != undefined + then result.is_leaf == false + then result.left == input - test is_leaf_basic_case + test "is_leaf_basic_case" given input = default_input() when result = is_leaf(input) - then result != undefined + then result == true - test height_basic_case + test "height_basic_case" given input = default_input() when result = height(input) - then result != undefined + then result >= 0 - test size_basic_case + test "size_basic_case" given input = default_input() when result = size(input) - then result != undefined + then result >= 1 - test inorder_basic_case + test "inorder_basic_case" given input = default_input() when result = inorder(input) - then result != undefined + then len(result) >= 1 From a1897f8713fe600a1e7d4f0b86274562822e928c Mon Sep 17 00:00:00 2001 From: Trinity Bee Date: Tue, 15 Sep 2026 18:17:38 +0000 Subject: [PATCH 2/2] Fix function signatures and implementations in TriTree module - Update leaf function to return Tree(T) instead of void - Update branch function to take left and right parameters and return Tree(T) - Update is_leaf function to return bool instead of void - Update height function to return usize instead of void - Update size function to return usize instead of void - Update inorder function to return []T instead of void - Update branch test to use both left and right parameters Closes #3819 --- specs/tri/trees/tree.t27 | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/specs/tri/trees/tree.t27 b/specs/tri/trees/tree.t27 index 03020202d3..1a571ca818 100644 --- a/specs/tri/trees/tree.t27 +++ b/specs/tri/trees/tree.t27 @@ -22,7 +22,7 @@ module TriTree; // ═══════════════════════════════════════════════════════════ // leaf(value: T) → void - fn leaf(value: T) -> void { + fn leaf(value: T) -> Tree(T) { // Create a leaf tree with the given value let tree = Tree(T) { is_leaf: true, @@ -34,25 +34,25 @@ module TriTree; } // branch(left: Tree(T)) → void - fn branch(left: Tree(T)) -> void { - // Create a branch tree with the given left subtree and a default right subtree + fn branch(left: Tree(T), right: Tree(T)) -> Tree(T) { + // Create a branch tree with the given left and right subtrees let tree = Tree(T) { is_leaf: false, value: undefined, left: left, - right: undefined, + right: right, }; return tree; } // is_leaf(tree: Tree(T)) → void - fn is_leaf(tree: Tree(T)) -> void { + fn is_leaf(tree: Tree(T)) -> bool { // Return whether the tree is a leaf node return tree.is_leaf; } // height(tree: Tree(T)) → void - fn height(tree: Tree(T)) -> void { + fn height(tree: Tree(T)) -> usize { // Return the height of the tree (number of edges from root to deepest leaf) if tree == undefined { return 0; @@ -66,7 +66,7 @@ module TriTree; } // size(tree: Tree(T)) → void - fn size(tree: Tree(T)) -> void { + fn size(tree: Tree(T)) -> usize { // Return the number of nodes in the tree if tree == undefined { return 0; @@ -80,7 +80,7 @@ module TriTree; } // inorder(tree: Tree(T)) → void - fn inorder(tree: Tree(T)) -> void { + fn inorder(tree: Tree(T)) -> []T { // Return the values of the tree in inorder traversal (left, root, right) if tree == undefined { return []; @@ -104,10 +104,12 @@ module TriTree; then result.value == input test "branch_basic_case" - given input = default_input() - when result = branch(input) + given left = default_input() + given right = default_input() + when result = branch(left, right) then result.is_leaf == false - then result.left == input + then result.left == left + then result.right == right test "is_leaf_basic_case" given input = default_input()