Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Last active September 19, 2015 19:05
Show Gist options
  • Save TheSeamau5/6b99695b0ae09d093525 to your computer and use it in GitHub Desktop.
Save TheSeamau5/6b99695b0ae09d093525 to your computer and use it in GitHub Desktop.
Visual representation of binary trees in Elm
import Text (asText)
import Graphics.Collage (collage, toForm, filled, circle, move, scale, Form)
import Graphics.Element (Element)
import Color (red)
import List (map, (::), reverse)
type BinaryTree a = Nil | Node (BinaryTree a) a (BinaryTree a)
insert : comparable -> BinaryTree comparable -> BinaryTree comparable
insert node tree =
case tree of
Nil -> Node Nil node Nil
Node left leaf right ->
if | leaf == node -> Node left leaf right
| leaf < node -> Node left leaf (insert node right)
| leaf > node -> Node (insert node left) leaf right
push : BinaryTree comparable -> comparable -> BinaryTree comparable
push = flip insert
isEmpty : BinaryTree a -> Bool
isEmpty tree =
case tree of
Nil -> True
_ -> False
contains : comparable -> BinaryTree comparable -> Bool
contains node tree =
case tree of
Nil -> False
Node left leaf right ->
if | leaf == node -> True
| leaf < node -> contains leaf left
| leaf > node -> contains leaf right
drawTree : BinaryTree a -> List Form
drawTree tree =
case tree of
Nil -> []
Node left leaf right ->
[filled red (circle 30), scale 2 <| toForm <| asText leaf] ++
map (move (-40,-60)) (drawTree left) ++
map (move (40, -60)) (drawTree right)
renderTree : BinaryTree a -> Element
renderTree = collage 600 600 << map (move (0, 260)) << drawTree
t1 = Nil
t2 = insert 2 t1
t3 = insert 3 t2
t4 = insert 1 t3
tree : BinaryTree Int
tree =
Nil `push` 5
`push` 6
`push` 4
`push` 3
`push` 1
`push` 8
`push` 0
`push` 7
`push` 2
`push` 9
fromList : List comparable -> BinaryTree comparable
fromList list =
let reversedList = reverse list
innerFromList l =
case l of
[] -> Nil
x :: xs -> insert x (innerFromList xs)
in innerFromList reversedList
treeList = fromList [4,2,0,8,9,10,1,-1,23,8,9,7,11,13,-4,0,9, -2, -3]
main : Element
main = renderTree treeList
@fbonetti
Copy link

Updated for Elm 0.15.1:

import Graphics.Collage exposing (collage, toForm, filled, circle, move, scale, Form)
import Graphics.Element exposing (Element, show)
import Color exposing (red)
import List exposing (map, (::), reverse)

type BinaryTree a = Nil | Node (BinaryTree a) a (BinaryTree a)

insert : comparable -> BinaryTree comparable -> BinaryTree comparable
insert node tree = 
  case tree of 
    Nil -> Node Nil node Nil
    Node left leaf right ->
      if  | leaf == node -> Node left leaf right  
          | leaf <  node -> Node left leaf (insert node right)
          | leaf >  node -> Node (insert node left) leaf right


push : BinaryTree comparable -> comparable -> BinaryTree comparable
push = flip insert 

isEmpty : BinaryTree a -> Bool
isEmpty tree = 
  case tree of 
    Nil -> True
    _ -> False


contains : comparable -> BinaryTree comparable -> Bool
contains node tree = 
  case tree of 
    Nil -> False
    Node left leaf right ->
      if | leaf == node -> True
         | leaf <  node -> contains leaf left
         | leaf >  node -> contains leaf right



drawTree : BinaryTree a -> List Form
drawTree tree = 
  case tree of 
    Nil -> []
    Node left leaf right ->
      [filled red (circle 30), scale 2 <| toForm <| show leaf] ++
      map (move (-40,-60)) (drawTree left) ++
      map (move (40, -60)) (drawTree right)


renderTree : BinaryTree a -> Element
renderTree = collage 600 600 << map (move (0, 260)) << drawTree


t1 = Nil
t2 = insert 2 t1
t3 = insert 3 t2
t4 = insert 1 t3


tree : BinaryTree Int
tree = 
  Nil `push` 5
      `push` 6
      `push` 4
      `push` 3
      `push` 1
      `push` 8
      `push` 0
      `push` 7
      `push` 2
      `push` 9

fromList : List comparable -> BinaryTree comparable 
fromList list = 
  let reversedList = reverse list
      innerFromList l = 
        case l of 
          [] -> Nil
          x :: xs -> insert x (innerFromList xs)
  in innerFromList reversedList


treeList = fromList [4,2,0,8,9,10,1,-1,23,8,9,7,11,13,-4,0,9, -2, -3]


main : Element
main = renderTree treeList

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment