elevatika

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

Showing Posts 1 to 10

kokolegorille

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.

[node.data | nodes]

Remember a list is [head | tail], with head being an element pushed at the head, while tails is a list.

NobbZ

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

elevatika OP

Hi. Thanks.

I have edited the section of the code that way but still not working.

NobbZ

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

elevatika OP

I expect the function to return a list after completing the traversal

elevatika

elevatika OP

defmodule Algos do
    #pre_order function, prints the root, the left then the right
    def pre_order(node, nodes) do
        next = [node.data | nodes]
        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 = [node.data | nodes]
        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
        [node.data | nodes]
    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

That is the current code.

I still get only the first node “A” returned by each function.

[Running] elixir "/Volumes/MAC/d/Algorithms/tree_traversal.exs"
A
A
A

[Done] exited with code=0 in 3.304 seconds

I expect the functions to traverse the tree, create a list, and then return that list

NobbZ

NobbZ

You are not using the return values of the recursive calls. They do not have an effect.

elevatika

elevatika OP

What would be the best way to return the list after completing the recursive calls?

I’m still struggling to understand Elixir

NobbZ

NobbZ

You already return it. Though you are not using the returned list.

NobbZ

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.

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
kpanic
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
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 Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews