Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by wyklad on 14.11.16.
- */
- sealed trait BT[A]
- case class Empty[A]() extends BT[A]
- case class Node[A](elem:A, left:BT[A], right:BT[A]) extends BT[A]
- object App5 {
- val t1 = Node(1,
- Node(2,
- Node(4,
- Empty[Int],
- Empty[Int]
- ),
- Empty[Int]
- ),
- Node(3,
- Node(5,
- Empty[Int],
- Node(6,
- Empty[Int],
- Empty[Int]
- )
- ),
- Empty[Int]
- )
- )
- val t2 = Node(10,
- Node(5,
- Empty[Int],
- Empty[Int]
- ),
- Node(15,
- Node(14,
- Empty[Int],
- Empty[Int]
- ),
- Node(16,
- Empty[Int],
- Empty[Int]
- )
- )
- )
- val t3 = Node(1500,
- Node(2,
- Node(48,
- Empty[Int],
- Empty[Int]
- ),
- Empty[Int]
- ),
- Node(39,
- Node(5,
- Empty[Int],
- Node(64,
- Empty[Int],
- Empty[Int]
- )
- ),
- Empty[Int]
- )
- )
- val t4 = Empty[Int]
- def sumOfNodes(node:BT[Int]):Int = {
- node match {
- case Empty() => 0
- case Node(v, l, r) => v + sumOfNodes(l) + sumOfNodes(r)
- }
- }
- def main(args: Array[String]): Unit = {
- println(sumOfNodes(t1))
- println(sumOfNodes(t2))
- println(sumOfNodes(t3))
- println(sumOfNodes(t4))
- }
- }
Add Comment
Please, Sign In to add comment