Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. val tt = Node(1,Node(2,Node(4,Empty,Empty),Empty),Node(3,Node(5,Empty,Node(6,Empty,Empty)),Empty));;
  2.  
  3.  
  4. sealed trait BT[+A]
  5. case object Empty extends BT[Nothing]
  6. case class Node[+A](elem:A, left:BT[A], right:BT[A]) extends BT[A]
  7.  
  8. def breadthBT[A](tree: BT[A]) = {
  9. def helper[A](nodeQueue: List[BT[A]]): List[A] = nodeQueue match {
  10. case Nil => Nil
  11. case Empty :: tail => helper(tail)
  12. case Node(value, leftSubtree, rightSubtree) :: tail => value :: helper(tail ++ List(leftSubtree, rightSubtree))
  13. }
  14. helper (List(tree))
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement