elevatika
I am learning Elixir and wanted to try implementing simple tree traversal algorithms. However, I am not getting the desired results. What am I doing wrong?
defmodule Algos do
#pre_order function, prints the root, the left then the right
def pre_order(node, nodes) do
next = [nodes | node.data]
if (node && node.left) do
Algos.pre_order(node.left, next)
end
if (node && node.right) do
Algos.pre_order(node.right, next)
end
next
end
#in_order function, prints the left, the root then the right
def in_order(node, nodes) do
if (node && node.left) do
Algos.in_order(node.left, nodes)
end
next = [nodes | node.data]
if (node && node.right) do
Algos.in_order(node.right, next)
end
next
end
#post_order function, prints the left, the right then the root
def post_order(node, nodes) do
if (node && node.left) do
Algos.post_order(node.left, nodes)
end
if (node && node.right) do
Algos.post_order(node.right, nodes)
end
[nodes | node.data]
end
end
defmodule Traverse do
def print do
root = %{
data: "A",
left: %{
data: "B",
left: %{
data: "C",
left: nil,
right: nil
},
right: %{
data: "D",
left: nil,
right: nil
}
},
right: %{
data: "E",
left: nil,
right: nil
}
}
result = Algos.pre_order(root, "")
IO.puts(result)
result2 = Algos.in_order(root, "")
IO.puts(result2)
result3 = Algos.post_order(root, "")
IO.puts(result3)
end
end
Traverse.print
The output looks as follows
[Running] elixir "/Volumes/MAC/d/Algorithms/tree_traversal.exs"
A
A
A
[Done] exited with code=0 in 3.891 seconds
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
Hello and welcome,
I did not read all the code, but this is not working as You expect, it should be an element first, then a list. If needed, You can reverse the list at the end.
Remember a list is [head | tail], with head being an element pushed at the head, while tails is a list.
NobbZ
What output would you expect instead?
You don’t use the return values of th recursive calls, they won’t have any effect besides heating your room.
elevatika
Hi. Thanks.
I have edited the section of the code that way but still not working.
NobbZ
What does “not working” mean? Do you get an error, if yes which? Is the output different from what you expect, if yes how?
And how does your current code look like?
elevatika
I expect the function to return a list after completing the traversal
elevatika
That is the current code.
I still get only the first node “A” returned by each function.
I expect the functions to traverse the tree, create a list, and then return that list
NobbZ
You are not using the return values of the recursive calls. They do not have an effect.
elevatika
What would be the best way to return the list after completing the recursive calls?
…
I’m still struggling to understand Elixir
NobbZ
You already return it. Though you are not using the returned list.
NobbZ
Have you been able to make any progress?
You “liked” my previous post, so I can assume you read it. If you have any problems understanding the hints, please feel free to ask more questions.