Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

My Toy Erlang Programs

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 binary_tree
Octocat-spinner-32 scan_ssh
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.md
README.md

This repository is for the Erlang programs that I write to learn the language.

Programs

binary_tree

node record

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}}.

bfs/1

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]

dfs/1

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]

from_bfs_dfs/2

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}}
Something went wrong with that request. Please try again.