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
The idea of
simulateis that each timeupdateTreeis 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:
This will move an item
xfrom the list to the tree for each frame of the simulation.