Pabl0o0

Untitled

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