Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. var node = {
  2. value: 0,
  3. left: null,
  4. right: null
  5. }
  6.  
  7. var create_node = function(val, l, r) {
  8. return {value: val, left: l, right: r}
  9. }
  10.  
  11.  
  12. var n3 = {
  13. value: 15,
  14. left: null,
  15. right: null
  16. }
  17. var n2 = {
  18. value: 13,
  19. left: null,
  20. right: n3
  21. }
  22. var n1 = {
  23. value: 5,
  24. left: null,
  25. right: null
  26. }
  27. var n0 = {
  28. value: 10,
  29. left: n1,
  30. right: n2
  31. }
  32.  
  33. console.log('tree: ', n0)
  34.  
  35. function print_tree(root) {
  36. if (root != null) {
  37. print_tree(root.left)
  38. console.log(root.value)
  39. print_tree(root.right)
  40. }
  41. }
  42.  
  43.  
  44. function find(k, root) {
  45. // base
  46. if (root != null) {
  47. if (root.value == k) {
  48. return root
  49. } else if (k < root.value) {
  50. return find(k, root.left)
  51. } else {
  52. return find(k, root.right)
  53. }
  54. } else {
  55. return null
  56. }
  57. }
  58.  
  59. function pretty_print(root) {
  60. if (root != null) {
  61. console.log(root.value)
  62. }
  63. }
  64.  
  65. function insert(k, root) {
  66. // base case
  67. if (root.left === null && k < root.value) {
  68. root.left = {
  69. value: k,
  70. left: null,
  71. right: null,
  72. }
  73. return
  74. }
  75. if (root.right === null && k > root.value) {
  76. root.right = {
  77. value: k,
  78. left: null,
  79. right: null,
  80. }
  81. return
  82. }
  83.  
  84. // recursive case
  85. if (k < root.value) {
  86. insert(k, root.left)
  87. } else if (k > root.value) {
  88. insert(k, root.right)
  89. }
  90. }
  91.  
  92. function test_insert() {
  93. insert(8, n0)
  94. insert(9, n0)
  95. print_tree(n0)
  96. console.log(n0)
  97. }
  98.  
  99.  
  100. function delete_node(k, root, parent) {
  101. if (k < root.value) {
  102. // left
  103. console.log('go left')
  104. delete_node(k, root.left, root)
  105. } else if (k > root.value) {
  106. // right
  107. console.log('go right')
  108. delete_node(k, root.right, root)
  109. } else {
  110. console.log('time to delete')
  111. // base
  112. if (root.left === null && root.right === null) {
  113. // 1. no children
  114. if (root.value < parent.value) {
  115. parent.left = null
  116. } else {
  117. parent.right = null
  118. }
  119. } else if (root.left !== null && root.right !== null) {
  120. // 2. two children
  121. console.log('two children')
  122.  
  123. // find successor
  124. var current_parent = root
  125. var current = root.right
  126. while (current.left !== null) {
  127. current_parent = current
  128. current = current.left
  129. }
  130. console.log('current: ', current)
  131.  
  132. // delete successor and attach successor's children
  133. if (current_parent == root) {
  134. current_parent.right = current.right
  135. } else {
  136. current_parent.left = current.right
  137. }
  138.  
  139. // swap successor and delete root
  140. current.left = root.left
  141. current.right = root.right
  142. if (parent !== null) {
  143. if (root.value < parent.value) {
  144. parent.left = current
  145. } else {
  146. parent.right = current
  147. }
  148. }
  149.  
  150. // replace root
  151. root = current
  152. console.log('done')
  153. } else {
  154. // 3. one child
  155. console.log('one child')
  156. if (root.left !== null && root.right === null) {
  157. if (root.value < parent.value) {
  158. parent.left = root.left
  159. } else {
  160. parent.right = root.left
  161. }
  162. } else if (root.left === null && root.right !== null) {
  163. if (root.value < parent.value) {
  164. parent.left = root.right
  165. } else {
  166. parent.right = root.right
  167. }
  168. }
  169. }
  170. }
  171. return root
  172. }
  173.  
  174. function test_delete_node() {
  175. var new_tree = delete_node(10, n0, null)
  176. console.log(new_tree)
  177. print_tree(new_tree)
  178. }
  179.  
  180. console.log('test_delete_node')
  181. test_delete_node()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement