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
bfs(parent_pid)
Specs
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]
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
dfs(parent_pid, traversal_type)
Specs
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]
get_data(node_pid)
Specs
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
get_left(node_pid)
Specs
Get the left child of node
Examples
iex> root = GenTree.new(5)
iex> left_child = GenTree.get_left(root)
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}
get_right(node_pid)
Specs
Get the right child of node
Examples
iex> root = GenTree.new(5)
iex> right_child = GenTree.get_right(root)
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}
left?(node_pid)
Specs
Return if data has a left child
## Examples
iex> root = GenTree.new(5)
iex> GenTree.left?(root)
false
new(data)
Specs
New node of GenTree
Examples
iex> root = GenTree.new(5)
iex> is_pid(root) === true
right?(node_pid)
Specs
Return if data has a right child
## Examples
iex> root = GenTree.new(5)
iex> GenTree.right?(root)
false
update_data(node_pid, data)
Specs
Updates the data value of the node
update_node(node_pid, data)
Specs
Updates complete node value