Advertisement
Guest User

GUNWO

a guest
Dec 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.18 KB | None | 0 0
  1. sealed trait BT[+A]
  2. case object Empty extends BT[Nothing]
  3. case class Node[+A](value:A, left:BT[A], right:BT[A]) extends BT[A]
  4.  
  5.  
  6. sealed trait Graph[A]
  7. case class GNode[A](value:A, list:List[Graph[A]]) extends Graph[A]
  8.  
  9. def graphToBT[A](graph: Graph[A]) = {
  10.   def aux(graph1: Graph[A], acc: List[Graph[A]]): Node[A] = {
  11.     graph1 match {
  12.       case GNode(a, Nil) => if(acc.length == 0){
  13.         return Node(a, Empty, Empty)}
  14.       else {
  15.         return Node(a, aux(acc.head, acc.tail), Empty)
  16.       }
  17.       case GNode(a, h::t) =>
  18.         t match {
  19.           case Nil => Node(a, aux(h, acc), Empty)
  20.           case h2::t2 => Node(a, aux(h, (acc ++ t2)), aux(h2, List()))
  21.         }
  22.     }
  23.   }
  24.   aux(graph, List())
  25. }
  26.  
  27. def graph1 = GNode(1, List(GNode(2, List(GNode(3, List(GNode(4, List(GNode(5, List())))))))))
  28. def graph2 = GNode(1, List(GNode(2, List(GNode(3, List(GNode(4, List(GNode(5, List()), GNode(6, List()), GNode(7, List() )) )))))))
  29. def graph3 = GNode(1, List(GNode(2, List(GNode(3, List(GNode(4, List(GNode(5, List()), GNode(6, List()), GNode(7, List() ), GNode(8, List() ), GNode(9, List() ), GNode(10, List() )) )))))))
  30. graphToBT(graph1)
  31. graphToBT(graph2)
  32. graphToBT(graph3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement