title: Building a Binary Tree (not BST) in Haskell Breadth-First
body: <p>I recently started using Haskell and it will probably be for a <em>short</em> while. Just being asked to use it to better understand functional programming for a class I am taking at Uni.</p>
  
  <p>Now I have a slight problem I am currently facing with what I am trying to do. I want to build it breadth-first but I think I got my conditions messed up or my conditions are also just wrong.</p>
  
  <p>So essentially if I give it
  <code>[“A1-Gate”, “North-Region”, “South-Region”, “Convention Center”, “Rectorate”, “Academic Building1”, “Academic Building2”]</code> and <code>[0.0, 0.5, 0.7, 0.3, 0.6, 1.2, 1.4, 1.2]</code>, my tree should come out like</p>
  
  <p><img src="https://i.stack.imgur.com/XUlJJ.png" alt="Tree Layout I am trying to achieve"></p>
  
  <p>But my test run results are haha not what I expected. So an extra sharp expert in Haskell could possibly help me spot what I am doing wrong.
  Output:</p>
  
  <pre><code>*Main&gt; l1 = ["A1-Gate", "North-Region", "South-Region", "Convention Center", 
               "Rectorate", "Academic Building1", "Academic Building2"]
  *Main&gt; l3 = [0.0, 0.5, 0.7, 0.3, 0.6, 1.2, 1.4, 1.2]
  *Main&gt; parkingtree = createBinaryParkingTree l1 l3
  *Main&gt; parkingtree
  Node "North-Region" 0.5 
     (Node "A1-Gate" 0.0 EmptyTree EmptyTree) 
     (Node "Convention Center" 0.3 
       (Node "South-Region" 0.7 EmptyTree EmptyTree) 
       (Node "Academic Building2" 1.4 
         (Node "Academic Building1" 1.2 EmptyTree EmptyTree) 
         (Node "Rectorate" 0.6 EmptyTree EmptyTree)))
  </code></pre>
  
  <p>A-1 Gate should be the root but it ends up being a child with no children so pretty messed up conditions.</p>
  
  <p>If I could get some guidance it would help. Below is what I've written so far::</p>
  
  <pre><code>data Tree = EmptyTree | Node [Char] Float Tree Tree deriving (Show,Eq,Ord)
  
  insertElement location cost EmptyTree = 
     Node location cost EmptyTree EmptyTree
  insertElement newlocation newcost (Node location cost left right) = 
     if (left == EmptyTree &amp;&amp; right == EmptyTree)
     then Node location cost (insertElement newlocation newcost EmptyTree) 
                             right
     else if (left == EmptyTree &amp;&amp; right /= EmptyTree)
          then Node location cost (insertElement newlocation newcost EmptyTree) 
                                  right
          else if (left /= EmptyTree &amp;&amp; right == EmptyTree)
               then Node location cost left 
                                  (insertElement newlocation newcost EmptyTree)
               else Node newlocation newcost EmptyTree
                                  (Node location cost left right)
  
  buildBPT [] = EmptyTree
  --buildBPT (xs:[]) = insertElement (fst xs) (snd xs) (buildBPT [])
  buildBPT (x:xs) = insertElement (fst x) (snd x) (buildBPT xs)
  
  createBinaryParkingTree a b = buildBPT (zip a b)
  </code></pre>
  
  <p>Thank you for any guidance that might be provided. Yes I have looked at <strong>some</strong> of the similar questions I do think my problem is different but if you think a certain post has a clear answer that will help I am willing to go and take a look at it.</p>