Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 74 additions & 28 deletions specs/tri/trees/tree.t27
Original file line number Diff line number Diff line change
Expand Up @@ -22,66 +22,112 @@ module TriTree;
// ═══════════════════════════════════════════════════════════

// leaf(value: T) → void
fn leaf(value: T) -> void {
// TODO: Implement from .tri spec
fn leaf(value: T) -> Tree(T) {
// 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
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: right,
};
return tree;
}

// is_leaf(tree: Tree(T)) → void
fn is_leaf(tree: Tree(T)) -> void {
// TODO: Implement from .tri spec
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 {
// TODO: Implement from .tri spec
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;
}
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
fn size(tree: Tree(T)) -> usize {
// 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
fn inorder(tree: Tree(T)) -> []T {
// 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

test branch_basic_case
given input = default_input()
when result = branch(input)
then result != undefined

test is_leaf_basic_case
then result.is_leaf == true
then result.value == input

test "branch_basic_case"
given left = default_input()
given right = default_input()
when result = branch(left, right)
then result.is_leaf == false
then result.left == left
then result.right == right

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

Loading