Advertisement
Guest User

jef

a guest
Apr 2nd, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Apr 1 17:30:17 2020
  4.  
  5. @author: joshm
  6. """
  7.  
  8. class Node(object):
  9. def __init__(self, key):
  10. self.left_child = None
  11. self.right_child = None
  12. self.value = key
  13.  
  14.  
  15. def setValue(self, new_value):
  16. self.value = new_value
  17.  
  18. def getValue(self):
  19. return self.value
  20.  
  21. def setLeft(self, new_value):
  22. self.left_child = new_value
  23.  
  24. def getLeft(self):
  25. return self.left_child
  26.  
  27. def setRight(self, new_value):
  28. self.right_child = new_value
  29.  
  30. def getRight(self):
  31. return self.right_child
  32.  
  33.  
  34. class BST(object):
  35. def __init__(self):
  36. self.root = None
  37.  
  38. def insertNode(self,node):
  39. if self is None:
  40. self = node
  41. self.left_child = None
  42. self.right_child = None
  43. else:
  44. current = self.root
  45. while current is not None:
  46. if node.key < current.key:
  47. if current.left_child is None:
  48. current.left_child = node
  49. current = None
  50. else:
  51. current = current.left_child
  52. else:
  53. if current.right_child is None:
  54. current.right_child = node
  55. current = None
  56. else:
  57. current = current.right_child
  58. node.left_child = None
  59. node.right_child = None
  60.  
  61.  
  62. def searchNode(self,key):
  63. current = self.root
  64. while current is not None:
  65. if key == current.key:
  66. return current
  67. elif key < current.key:
  68. current = current.left_child
  69. else:
  70. current = current.right_child
  71. return None
  72.  
  73. def deleteNode(self,key):
  74. par = None
  75. current = self.root
  76. while current is not None:
  77. if current.key == key:
  78. if not current.left_child and not current.right_child:
  79. if not par:
  80. self.root = None
  81. elif par.left_child == current:
  82. par.left_child = None
  83. else:
  84. par.right_child = None
  85. elif current.left_child and not current.right_child:
  86. if not par:
  87. self.root = current.left_child
  88. elif par.left_child == current:
  89. par.left_child = current.left_child
  90. else:
  91. par.right_child = current.left_child
  92. elif not current.left_child and current.right_child:
  93. if not par:
  94. self.root = current.right_child
  95. elif par.left_child == current:
  96. par.left_child = current.right_child
  97. else:
  98. par.right_child = current.left_child
  99. else:
  100. successor = current.right_child
  101. while successor.left_child is not None:
  102. successor = successor.left_child
  103. successorData = successor.value
  104. deleteNode(self,successor.key)
  105. current.value = successorData
  106. return
  107. elif current.key < key:
  108. par = current
  109. current = current.right_child
  110. else:
  111. par = current
  112. current = current.left_child
  113. return
  114.  
  115. def createNode(parent, i, created, root):
  116. if created[i] is not None:
  117. return
  118.  
  119. created[i] = Node(i)
  120.  
  121. if parent[i] == -1:
  122. root[0] = created[i]
  123. return
  124.  
  125. if created[parent[i]] is None:
  126. createNode(parent, parent[i], created, root)
  127. p = created[parent[i]]
  128.  
  129. if p.left is None:
  130. p.left = created[i]
  131.  
  132. else:
  133. p.right = created[i]
  134.  
  135. def createTree(parent):
  136. n = len(parent)
  137.  
  138. created = [None for i in range(n+1)]
  139.  
  140. root = [None]
  141. for i in range(n):
  142. createNode(parent, i, created, root)
  143.  
  144. return root[0]
  145.  
  146. #Inorder traversal of tree
  147. def inorder(root):
  148. if root is not None:
  149. inorder(root.left)
  150. print(root.key,end="")
  151. inorder(root.right)
  152.  
  153. # Driver Method
  154. parent = [-1, 0, 0, 1, 1, 3, 5]
  155. root = createTree(parent)
  156. print("Inorder Traversal of constructed tree")
  157. inorder(root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement