Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. text1 = open('keysInsert.txt').read()
  2. text2 = open('keysDelete.txt').read()
  3. print (text1)
  4. print (text2)
  5. posplit1 = text1.split()
  6. posplit2 = text2.split()
  7. print (posplit1)
  8. print (posplit2)
  9. h = len(posplit1)
  10. g = len(posplit2)
  11. print(h)
  12. for x in range(h):
  13. posplit1[x] = int(posplit1[x])
  14. for x in range(g) :
  15. posplit1[x] = int(posplit1[x])
  16.  
  17. print(posplit2[1])
  18.  
  19.  
  20.  
  21. class BSTreeNode:
  22. def __init__(self, value):
  23. self.value = value
  24. self.left = None
  25. self.right = None
  26.  
  27. def insert(self,key):
  28. if self.value == key:
  29. self.right = BSTreeNode(key)
  30. print ("Został dodany wierzchołek:", posplit1[x])
  31.  
  32. elif self.value > key:
  33. if self.left is not None:
  34. return self.left.insert(key)
  35. print("Został dodany wierzchołek:", posplit1[x])
  36. else:
  37. self.left = BSTreeNode(key)
  38. print("Został dodany wierzchołek:", posplit1[x])
  39. return True
  40. else:
  41. if self.right is not None:
  42. return self.right.insert(key)
  43. print("Został dodany wierzchołek:", posplit1[x])
  44. else:
  45. self.right = BSTreeNode(key)
  46. print("Został dodany wierzchołek:", posplit1[x])
  47. return False
  48.  
  49. def delete(self, key) :
  50. """ delete the node with the given key and return the
  51. root node of the tree """
  52.  
  53. if self.key == key :
  54.  
  55.  
  56. if self.right and self.left :
  57.  
  58.  
  59. [psucc, succ] = self.right._findMin(self)
  60.  
  61.  
  62.  
  63. if psucc.left == succ :
  64. psucc.left = succ.right
  65. else :
  66. psucc.right = succ.right
  67.  
  68.  
  69.  
  70. succ.left = self.left
  71. succ.right = self.right
  72.  
  73. return succ
  74.  
  75. else :
  76.  
  77. if self.left :
  78. return self.left
  79. else :
  80. return self.right
  81. else :
  82. if self.key > key :
  83. if self.left :
  84. self.left = self.left.delete(key)
  85.  
  86.  
  87. else :
  88. if self.right :
  89. self.right = self.right.delete(key)
  90.  
  91. return self
  92.  
  93. def _findMin(self, parent) :
  94. """ return the minimum node in the current tree and its parent """
  95.  
  96.  
  97. if self.left :
  98. return self.left._findMin(self)
  99. else :
  100. return [parent, self]
  101.  
  102. class BSTree:
  103. def __init__(self):
  104. self.root = None
  105.  
  106. def delete(self,key):
  107. self.root.delete(self.root,key)
  108.  
  109. def insert(self,data):
  110. if self.root:
  111. self.root.insert(data)
  112. else:
  113. self.root = BSTreeNode(data)
  114. return True
  115. def find_min(self,node):
  116. current_node = node
  117. while current_node.left:
  118. current_node = current_node.left
  119. return current_node
  120. #znalazłem nie wiem jak przerobić
  121. def height(bst) :
  122. if isempty(bst) :
  123. return 0
  124. else :
  125. return 1 + max(height(bst.left), height(bst.right))
  126.  
  127.  
  128. def get_min(node):
  129. current_node = node
  130. while current_node.left:
  131. current_node = current_node.left
  132. return str(current_node.value)
  133.  
  134. def print_helper(root, indent):
  135. if root is not None:
  136. print_helper(root.right, indent + " ")
  137. print (indent + str(root.value))
  138. print_helper(root.left, indent + " ")
  139.  
  140. def print_tree(root):
  141. print_helper(root, "")
  142.  
  143.  
  144.  
  145.  
  146. bst = BSTree()
  147. for x in range(h):
  148. bst.insert(posplit1[x])
  149. #nie działa
  150. # for x in range(g) :
  151. # bst.delete(posplit2[x])
  152.  
  153. print_tree(bst.root)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement