Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.17 KB | None | 0 0
  1. sealed trait BT[+A]
  2. case object Empty extends BT[Nothing]
  3. case class Node[+A](elem: (A, A), left: BT[A], right: BT[A]) extends BT[A]
  4.  
  5. object DictionaryTree{
  6.  
  7.   def main(args: Array[String]): Unit = {
  8.     val bt = stworzSlownik()
  9.      println(tlumacz(bt, "słońce"))
  10.      println(tlumacz(dodaj(bt, "ogień", "fire"), "ogień"))
  11.      inorder(bt)
  12.   }
  13.  
  14.   //Z1
  15.   def stworzSlownik(): BT[String] =
  16.     Node(("woda", "water"), Node(("słońce", "sun"), Node(("księżyc", "moon"), Empty, Empty),
  17.       Node(("drzewo", "tree"), Empty, Empty)), Node(("głowa", "head"),
  18.       Node(("telefon", "phone"), Empty, Empty), Empty))
  19.  
  20.   //Z2
  21.   def tlumacz(slownik: BT[String], slowo: String): String =
  22.     slownik match {
  23.       case Empty => throw new Exception("Brak slowa w slowniku!")
  24.       case Node((pol, ang), l, r) =>
  25.         if (pol == slowo) ang
  26.         else if (slowo < pol) tlumacz(l, slowo) else tlumacz(r, slowo)
  27.     }
  28.  
  29.   //Z3
  30.   def dodaj(slownik: BT[String], pol: String, ang: String): BT[String] =
  31.     slownik match {
  32.       case Empty => Node((pol, ang), Empty, Empty)
  33.       case Node((pl, en), l, r) =>
  34.         if (pl == pol) {
  35.           if (en == ang) throw new Exception("Podane slowo wraz z podanym tlumaczeniem istnieje juz w slowniku!")
  36.           else if (en.length() < ang.length()) Node((pl, en), l, r)
  37.           else Node((pl, ang), l, r)
  38.         }
  39.         else if (pol < pl) Node((pl, en), dodaj(l, pol, ang), r)
  40.         else Node((pl, en), l, dodaj(r, pol, ang))
  41.     }
  42.  
  43.   //Z4
  44.   def inorder[A](bt: BT[A]): List[(A, A)] =
  45.     {
  46.       def inord(p: (BT[A], List[(A, A)])): List[(A, A)] = p match {
  47.         case (Empty, labels) => labels
  48.         case (Node(v, t1, t2), labels) => inord(t1, v :: inord(t2, labels))
  49.       }
  50.       inord(bt, Nil)
  51.     }
  52.  
  53.   //Z5
  54.   def sort(list: List[(String, String)]): List[(String, String)] = {
  55.  
  56.     val porz = (a: (String, String), b: (String, String)) => {
  57.       val (_, en1) = a
  58.       val (_, en2) = b
  59.       if (en1 < en2) true else false
  60.     }
  61.  
  62.     def insert(list: List[(String, String)], t: (String, String)): List[(String, String)] = list match {
  63.       case Nil => List[(String, String)](t)
  64.       case x :: r => if (porz(x, t)) x :: insert(r, t)
  65.       else t :: x :: r
  66.     }
  67.  
  68.     (list :\ List[(String, String)]())((h, posortowana) => insert(posortowana, h))
  69.   }
  70.  
  71.   //Z6
  72.   def stworzDrzewo(list: List[(String,String)]): BT[String] = {
  73.     def drzewo(list: List[(String,String)], acc: BT[String]): BT[String] =
  74.       list match {
  75.         case Nil => acc
  76.         case (pl,en) :: t => drzewo(t, dodaj(acc, en, pl))
  77.       }
  78.     drzewo(list, Empty)
  79.   }
  80.  
  81.   def stworzDrzewoWywazone(list: List[(String,String)]):BT[String] = {
  82.     def drzewo(list: List[(String,String)], acc: BT[String]): BT[String] =
  83.       if(list==Nil) acc
  84.       else {
  85.         val (list1,list2) = list.splitAt(list.length/2-1)
  86.         if(list2==Nil) {
  87.           val (pl,en) = list1.head
  88.           dodaj(acc,en,pl)
  89.         }
  90.         else {
  91.         val (pl,en) = list2.head
  92.         val tree1 = dodaj(acc,en,pl)
  93.         val tree2 = drzewo(list1,tree1)
  94.         drzewo(list2.tail,tree2)        
  95.         }
  96.       }
  97.     drzewo(list, Empty)
  98.   }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement