Skip to content

Binary Tree

Spice
import "std/data/binary-tree";

Node<T> struct

Node of a BinaryTree

Constructors

ctor

Spice
public p Node.ctor(const T& value, heap Node<T>* childLeft = nil<heap Node<T>*>, heap Node<T>* childRight = nil<heap Node<T>*>)

Construct a binary tree node holding the given value and optional children

Parameters

Name Type Description
value const T& Value to store in the node
childLeft heap Node<T>* Left child node, or nil if there is none (default: nil<heap Node<T>*>)
childRight heap Node<T>* Right child node, or nil if there is none (default: nil<heap Node<T>*>)

BinaryTree<T> struct

A binary tree is a data structure to fasten up search speeds. Binary trees (when balanced) can be searched in O(log n). Insert operations, on the other hand, are rather slow, because the tree might get re-balanced.

Time complexity:
Insert: O(n * m); n = inserted elements, m = moved elements
Delete: O(n * m); n = deleted elements, m = moved elements
Search: O(log n)

Methods

insert

Spice
public p BinaryTree.insert<T>(const T& value)

Insert a new value into the binary tree

Parameters

Name Type Description
value const T& The value to insert

contains

Spice
public f<bool> BinaryTree.contains<T>(const T& value)

Search for a value in the binary tree

Parameters

Name Type Description
value const T& The value to search for

Returns: bool — True if the value was found, false otherwise

delete

Spice
public p BinaryTree.delete<T>(const T& value)

Delete a value from the binary tree

Parameters

Name Type Description
value const T& The value to delete