1

Hello i am using Haskell gloss, to create a picture of a binary tree and simulate the insertion of the values.

I was able to create a draw function to create the picture of the tree and it works fine.

The problem is on creating a simulation. I have a list containing all trees after each value insertion on the tree and wanted the update-function to pick the tree in the position i of the list in some time past and keep switching the picture generate by each tree in position i in the list.

Is there a way to do that?

drawTree::(Show a)=>BTree a->Picture

updateTree :: ViewPort->Float->[BTree a]->[BTree a]
updateTree _ dt list=[list!!toInt dt]

main::IO()
main = do
  --receives all values to be inserted
  values <- getLine                    
  let list = read values :: [Int]      
  --temp is a list that stores all the tree generated in each insertion
  let temp =insertValues list Leaf                                 
  --stores the tree contained in the last inserction
  let tree = last temp
  --stores the tree after the first insertion                 
  let fir=first temp                   
  simulate window background fps [fir] drawTree updateTree
Random Dev
  • 51,810
  • 9
  • 92
  • 119

1 Answers1

0

The idea of simulate is that each time updateTree is called it moves the simulation along by one step. Your simulation state is not just the tree, it is also the list of items to put in the tree. The float argument is the time into the simulation so you can animate the movement of things if you want. However for a first pass you probably just want the values to appear in the tree one per frame.

So you want something like this:

type SimState a = ([a], BTree a)   -- Or maybe "data" if you prefer.

drawTree :: SimState a -> Picture

updateTree :: ViewPort -> Float -> SimState a -> SimState a
updateTree _ _ ([], t) = ([], t)  -- Static once everything is finished.
updateTree _ _ (x:xs, t) = (xs, addNewBtreeNode t x)

This will move an item x from the list to the tree for each frame of the simulation.

Paul Johnson
  • 17,438
  • 3
  • 42
  • 59