Pabl0o0

Untitled

Nov 6th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /**
  2. * Created by wyklad on 14.11.16.
  3. */
  4. sealed trait BT[A]
  5. case class Empty[A]() extends BT[A]
  6. case class Node[A](elem:A, left:BT[A], right:BT[A]) extends BT[A]
  7.  
  8. object App5 {
  9. val t1 = Node(1,
  10. Node(2,
  11. Node(4,
  12. Empty[Int],
  13. Empty[Int]
  14. ),
  15. Empty[Int]
  16. ),
  17. Node(3,
  18. Node(5,
  19. Empty[Int],
  20. Node(6,
  21. Empty[Int],
  22. Empty[Int]
  23. )
  24. ),
  25. Empty[Int]
  26. )
  27. )
  28. val t2 = Node(10,
  29. Node(5,
  30. Empty[Int],
  31. Empty[Int]
  32. ),
  33. Node(15,
  34. Node(14,
  35. Empty[Int],
  36. Empty[Int]
  37. ),
  38. Node(16,
  39. Empty[Int],
  40. Empty[Int]
  41. )
  42. )
  43. )
  44. val t3 = Node(1500,
  45. Node(2,
  46. Node(48,
  47. Empty[Int],
  48. Empty[Int]
  49. ),
  50. Empty[Int]
  51. ),
  52. Node(39,
  53. Node(5,
  54. Empty[Int],
  55. Node(64,
  56. Empty[Int],
  57. Empty[Int]
  58. )
  59. ),
  60. Empty[Int]
  61. )
  62. )
  63. val t4 = Empty[Int]
  64.  
  65. def sumOfNodes(node:BT[Int]):Int = {
  66. node match {
  67. case Empty() => 0
  68. case Node(v, l, r) => v + sumOfNodes(l) + sumOfNodes(r)
  69. }
  70. }
  71.  
  72. def main(args: Array[String]): Unit = {
  73. println(sumOfNodes(t1))
  74. println(sumOfNodes(t2))
  75. println(sumOfNodes(t3))
  76. println(sumOfNodes(t4))
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment