This repository is for the Erlang programs that I write to learn the language.
A node is composed of a value, a left branch and a right branch.
If a branch is empty, the node has the atom undefined.
A single node, with value 5 and no branches:
{node,5,undefined,undefined}This binary tree:
1
/ \
/ \
/ \
2 6
/ \ /
7 3 8
/ \
4 5
can be represented as
>Tree = {node,1,
> {node,2,
> {node,7,
> {node,4,undefined,undefined},
> {node,5,undefined,undefined}},
> {node,3,undefined,undefined}},
> {node,6,{node,8,undefined,undefined},undefined}}.Converts a node into an array of elements in breadth-first search order.
> binary_tree:bfs(Tree).
[1,2,6,7,3,8,4,5]Converts a node into an array of elements in depth-first search order.
> binary_tree:dfs(Tree).
[4,7,5,2,3,1,8,6]Reconstructs a binary tree from its breadth-first and depth-first searches.
> binary_tree:from_bfs_dfs([1,2,6,7,3,8,4,5], [4,7,5,2,3,1,8,6]).
{node,1,
{node,2,
{node,7,
{node,4,undefined,undefined},
{node,5,undefined,undefined}},
{node,3,undefined,undefined}},
{node,6,{node,8,undefined,undefined},undefined}}