Advertisement
Guest User

czy to drzewo? czy to graf?

a guest
Dec 5th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.99 KB | None | 0 0
  1. sealed trait BT[+A,+B]
  2. case class Node[+A,+B] (value:(A,B),right:List[BT[A,B]]) extends BT[A,B]
  3.  
  4. val a=Node(("Wrocław",20.0),List())
  5. val b=Node(("Warszawa",10.0),List())
  6. val c=Node(("Poznań",5.0),List(a))
  7. val d=Node(("Gdańsk",7.0),List(b))
  8. val graf = Node(("Kraków",0.0),List(c,d))
  9.  
  10.   /*toooooooo nie działa od tego momemntu w dół*/
  11. def mediumRoad(g:Node[String,Double]): Double ={
  12.   def road(visited: List[BT[String,Double]])(toVisit: List[BT[String,Double]])(suma:Double): Double =toVisit match{
  13.     case Nil => suma/visited.length
  14.     case h::t=>{
  15.                  val Node((nazwa,odleglosc),lista)=h
  16.                  if (visited contains h) road(visited)(t)(suma)
  17.                  else road(h::visited)(t ++ lista)(suma+
  18.                                                     lista.foldLeft(0)((acc,Node((a,miasto),b)=>acc+Math.abs(odleglosc-miasto)
  19.                                                   )
  20.                }    
  21.   }
  22.   road(Nil)(List(g))(0)
  23.  }
  24. mediumRoad (graf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement