Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:23:50+00:00 2026-06-11T17:23:50+00:00

So I was asked to make a Binary Tree in Haskell taking as input

  • 0

So I was asked to make a Binary Tree in Haskell taking as input a list of Integers. Below is my code. My problem is that the last element of the list is not getting inserted in the Tree. For example [1,2,3,4] it only inserts to the tree until “3” and 4 is not inserted in the Tree.

    data ArbolBinario a = Node a (ArbolBinario a) (ArbolBinario a) | EmptyNode
deriving(Show)

    insert(x) EmptyNode= insert(tail x) (Node (head x) EmptyNode EmptyNode)

    insert(x) (Node e izq der)
     |x == [] = EmptyNode --I added this line to fix the Prelude.Head Empty List error, after I added this line the last element started to be ignored and not inserted in the tree
     |head x == e = (Node e izq der)
     |head x < e = (Node e (insert x izq) der)
     |head x > e = (Node e izq (insert x der))

Any ideas on whats going on here? Help is much appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:23:51+00:00Added an answer on June 11, 2026 at 5:23 pm

    To solve this problem, I have used another function

    data Tree a = Node a (Tree a) (Tree a) | EmptyNode deriving(Show)
    
    insert :: Ord a => a -> Tree a -> Tree a
    insert x EmptyNode = Node x EmptyNode EmptyNode
    insert x (Node y left right)
        | x == y = (Node y left right)
        | x <  y = (Node y (insert x left) right)
        | x >  y = (Node y left (insert x right))
    
    buildtree :: Ord a => [a] -> Tree a
    buildtree []     = EmptyNode
    buildtree (x:xs) = insert x (buildtree xs)
    
    tree :: Ord a => [a] -> Tree a
    tree xs = buildtree (reverse xs)
    

    and to construct a binary tree, you need to call buildtree function. the problem is that you need to reverse the list first. So, tree will do the job.

    *Main> insert 5 (insert 12 (insert 2 (insert 10 EmptyNode)))
    Node 10 (Node 2 EmptyNode (Node 5 EmptyNode EmptyNode)) (Node 12 EmptyNode EmptyNode)
    
    *Main> tree [10,2,12,5]
    Node 10 (Node 2 EmptyNode (Node 5 EmptyNode EmptyNode)) (Node 12 EmptyNode EmptyNode)
    
    *Main> buildtree [5,12,2,10]
    Node 10 (Node 2 EmptyNode (Node 5 EmptyNode EmptyNode)) (Node 12 EmptyNode EmptyNode)
    

    UPDATE

    you can avoid using another function, this way:

    insert2 :: Ord a => [a] -> Tree a -> Tree a
    insert2 []     t = t
    insert2 (x:xs) EmptyNode = insert2 xs (Node x EmptyNode EmptyNode)
    insert2 (x:xs) (Node y left right)
                | x == y = insert2 xs (Node y left right)
                | x <  y = insert2 xs (Node y (insert2 [x] left) right)
                | x >  y = insert2 xs (Node y left (insert2 [x] right))
    

    however, it is not as efficient as using foldl or the other way, the only good thing about it is using only one function to do everything, no reverse is required.

    *Main> insert2 [10,2,12,5] EmptyNode
    Node 10 (Node 2 EmptyNode (Node 5 EmptyNode EmptyNode)) (Node 12 EmptyNode EmptyNode)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am being asked to make an haskell function that computes something like 1^2
I have a strange issue that I am asked to make a Spring MVC
I been asked to make a selenium test that checks the local database of
I have a quite difficult problem with binary files. I have been asked to
I have been asked to make a program that gets the Sql database settings
I've been asked to make an ETL-style application that transfers information from one data
I was asked to make a software that will encrypt and decrypt a normal
I've been asked to make a website with VB.NET server-side code, and I cannot
A client has asked me to make their YouTube channel look similar this one:
Just asked a question about linking Boost libraries in the make file. Thanks to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.