Guest User

Untitled

a guest
Dec 9th, 2019
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import *
  2. import random
  3.  
  4.  
  5. class Node:
  6. def __init__(self, val=None, left=None, right=None):
  7. self.value = val
  8. self.left = left
  9. self.right = right
  10.  
  11. def __str__(self):
  12. return str(self.value)
  13.  
  14.  
  15. class BinaryTree:
  16. def __init__(self):
  17. self.root = None
  18. self.output = ""
  19. self.counter = 0
  20.  
  21. # def add(self, val):
  22. # return Node(val, None, None)
  23.  
  24. def new(self, val):
  25. if self.root is None:
  26. self.root = Node(val)
  27. else:
  28. self.new_(self.root, val)
  29.  
  30. def new_(self, node, val):
  31. if node.left is not None and node.right is not None:
  32. s = random.randint(0, 1)
  33. if s == 0:
  34. self.new_(node.left, val)
  35. elif s == 1:
  36. self.new_(node.right, val)
  37. elif node.left is not None and node.right is None:
  38. node.right = Node(val)
  39. else:
  40. node.left = Node(val)
  41.  
  42. def createOutput(self):
  43. self.output += "("
  44. self.createOutput_(self.root)
  45. self.output += ")"
  46. return self.output
  47.  
  48. def createOutput_(self, root):
  49. self.output += " " + str(root.value) + " "
  50. if root.left is not None:
  51. self.output += "("
  52. self.createOutput_(root.left)
  53. if root.left is None and root.right is not None:
  54. self.output += "( "
  55. if root.right is not None:
  56. self.createOutput_(root.right)
  57. if root.right is None and root.left is not None:
  58. self.output += " "
  59. if root.left is not None or root.right is not None:
  60. self.output += ")"
  61.  
  62. def clearOutput(self):
  63. self.output = ""
  64.  
  65. def search(self, val):
  66. self.search_(self.root.left, val)
  67. return self.counter
  68.  
  69. def search_(self, root, val):
  70. if root.value == val:
  71. self.counter += 1
  72. if root.left is not None:
  73. self.search_(root.left, val)
  74. if root.right is not None:
  75. self.search_(root.right, val)
  76.  
  77. def specialSearch(self, val):
  78. s = self.output
  79. st = ""
  80. temp = 0
  81. for i in range(0, len(s)):
  82. if s[i] == "(":
  83. temp += 1
  84. if s[i] == ")":
  85. temp -= 1
  86. if temp == 2:
  87. temp = i
  88. break
  89. for i in range(0, temp):
  90. st += s[i]
  91. print(st)
  92. return st.count(" "+str(val)+" ")
  93.  
  94.  
  95. t = BinaryTree()
  96. # t.root = t.add(8)
  97. # t.root.left = t.add(4)
  98. # t.root.right = t.add(12)
  99. # t.root.left.left = t.add(2)
  100. # t.root.left.right = t.add(3)
  101. # t.root.right.left = t.add(10)
  102. # t.root.right.right = t.add(14)
  103. # t.root.left.left.left = t.add(1)
  104. # t.root.right.right.right = t.add(25)
  105. # t.root.left.left.right = t.add(3)
  106. # t.root.left.left.left.left = t.add(0)
  107. # t.root.right.right.right.right = t.add(100)
  108. # t.root.right.right.right.left = t.add(3)
  109. root = Tk()
  110. root.title("Laba 2")
  111. root.geometry("640x360+0+0")
  112. root.resizable(False, False)
  113. text = Text(wrap=WORD)
  114. text.place(x=10, y=250, height=66, width=600)
  115. txt = Entry()
  116. txt.place(x=50, y=100, height=33, width=133)
  117.  
  118.  
  119. def new():
  120. s = txt.get()
  121. s = int(s)
  122. t.new(s)
  123. txt.delete(0, END)
  124.  
  125.  
  126. def output():
  127. t.clearOutput()
  128. text.delete(1.0, END)
  129. s = t.createOutput()
  130. text.insert(1.0, s)
  131.  
  132.  
  133. def find():
  134. s = txt.get()
  135. s = int(s)
  136. txt.delete(0, END)
  137. s = t.specialSearch(s)
  138. text.delete(1.0, END)
  139. text.insert(1.0, "Данных элементов в правом поддереве " + str(s))
  140.  
  141.  
  142. add = Button(root, text="Add", command=new)
  143. add.place(x=50, y=50, height=33, width=133)
  144. show = Button(root, text="Print", command=output)
  145. show.place(x=200, y=100, height=33, width=133)
  146. replace = Button(root, text="Find", command=find)
  147. replace.place(x=200, y=50, height=33, width=133)
  148. root.mainloop()
RAW Paste Data