GenTree (gen_tree v0.1.0)

Documentation for GenTree. Tree data structure for BEAM in BEAM-way. Each node is a process that contains data and children_pids (just like using pointers).

Link to this section Summary

Functions

Traverses a binary tree using BFS.

Builds a Binary tree from a list of data

Traverses a binary tree using DFS. Traversal types

Get the node data value %GenTree.Node{children: children_list, data: any(), left: pid()\:nil, right: pid()\nil}

Get the left child of node

Get the node details %GenTree.Node{children: children_list, data: any(), left: pid()\:nil, right: pid()\nil}

Get the right child of node

Inserts child to the node and returns the child pid. child_type can be :left, :right or omitted.

Return if data has a left child

New node of GenTree

Return if data has a right child

Updates the data value of the node

Updates complete node value

Link to this section Functions

Link to this function

bfs(parent_pid)

Specs

bfs(pid()) :: [any()]

Traverses a binary tree using BFS.

Examples

iex> root = GenTree.build_tree([1,2,3,4,5,6])
iex> GenTree.Traversal.bfs(root)
[1, 2, 3, 4, 5, 6]
Link to this function

build_tree(data_list)

Builds a Binary tree from a list of data

Examples

iex(85)> root = GenTree.build_tree([1,2,3,4,5])
iex(86)> rl = GenTree.get_left(root)
iex(87)> rlr = GenTree.get_right(rl)
iex(87)> GenTree.get_data(rlr)
5
Link to this function

dfs(parent_pid, traversal_type)

Specs

dfs(pid(), :inorder | :postorder | :preorder) :: [any()]

Traverses a binary tree using DFS. Traversal types

  • :inorder
  • :preorder
  • :postorder

Examples

iex> root = GenTree.build_tree([1,2,3,4,5,6])
iex> GenTree.Traversal.dfs(root, :inorder)
[4, 2, 5, 1, 6, 3]
iex> GenTree.Traversal.dfs(root, :preorder)
[1, 2, 4, 5, 3, 6]
iex> GenTree.Traversal.dfs(root, :postorder)
[4, 5, 2, 6, 3, 1]
Link to this function

get_data(node_pid)

Specs

get_data(pid()) :: any()

Get the node data value %GenTree.Node{children: children_list, data: any(), left: pid()\:nil, right: pid()\nil}

Examples

iex> root = GenTree.new(5)
iex> root |> GenTree.get_data()
5
Link to this function

get_left(node_pid)

Specs

get_left(pid()) :: pid()

Get the left child of node

Examples

iex> root = GenTree.new(5)
iex> left_child = GenTree.get_left(root)
Link to this function

get_node(node_pid)

Specs

get_node(pid()) :: GenTree.Node.t()

Get the node details %GenTree.Node{children: children_list, data: any(), left: pid()\:nil, right: pid()\nil}

Examples

iex> root = GenTree.new(5)
iex> root |> GenTree.get_node()
%GenTree.Node{children: [], data: 5, left: nil, right: nil}
Link to this function

get_right(node_pid)

Specs

get_right(pid()) :: pid()

Get the right child of node

Examples

iex> root = GenTree.new(5)
iex> right_child = GenTree.get_right(root)
Link to this function

insert_child(node_pid, data, child_type \\ nil)

Inserts child to the node and returns the child pid. child_type can be :left, :right or omitted.

Examples

iex(21)> root = GenTree.new("a")
iex(23)> left_child = GenTree.insert_child(root, "b", :left)
iex(25)> GenTree.get_node(left_child)
%GenTree.Node{children: [], data: "b", left: nil, right: nil}
Link to this function

left?(node_pid)

Specs

left?(pid()) :: boolean()

Return if data has a left child

## Examples

iex> root = GenTree.new(5)
iex> GenTree.left?(root)
false

Specs

new(any()) :: pid()

New node of GenTree

Examples

iex> root = GenTree.new(5)
iex> is_pid(root) === true
Link to this function

right?(node_pid)

Specs

right?(pid()) :: boolean()

Return if data has a right child

## Examples

iex> root = GenTree.new(5)
iex> GenTree.right?(root)
false
Link to this function

update_data(node_pid, data)

Specs

update_data(pid(), any()) :: :ok

Updates the data value of the node

Link to this function

update_node(node_pid, data)

Specs

update_node(pid(), any()) :: :ok

Updates complete node value