Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- class Node:
- def __init__(self, val=None, left=None, right=None):
- self.value = val
- self.left = left
- self.right = right
- def __str__(self):
- return str(self.value)
- class BinaryTree:
- def __init__(self):
- self.root = None
- self.output = ""
- self.counter = 0
- # def add(self, val):
- # return Node(val, None, None)
- def new(self, val):
- if self.root is None:
- self.root = Node(val)
- else:
- self.new_(self.root, val)
- def new_(self, node, val):
- if node.left is not None and node.right is not None:
- s = random.randint(0, 1)
- if s == 0:
- self.new_(node.left, val)
- elif s == 1:
- self.new_(node.right, val)
- elif node.left is not None and node.right is None:
- node.right = Node(val)
- else:
- node.left = Node(val)
- def createOutput(self):
- self.output += "("
- self.createOutput_(self.root)
- self.output += ")"
- return self.output
- def createOutput_(self, root):
- self.output += " " + str(root.value) + " "
- if root.left is not None:
- self.output += "("
- self.createOutput_(root.left)
- if root.left is None and root.right is not None:
- self.output += "( "
- if root.right is not None:
- self.createOutput_(root.right)
- if root.right is None and root.left is not None:
- self.output += " "
- if root.left is not None or root.right is not None:
- self.output += ")"
- def clearOutput(self):
- self.output = ""
- def search(self, val):
- self.search_(self.root.left, val)
- return self.counter
- def search_(self, root, val):
- if root.value == val:
- self.counter += 1
- if root.left is not None:
- self.search_(root.left, val)
- if root.right is not None:
- self.search_(root.right, val)
- def specialSearch(self, val):
- s = self.output
- st = ""
- temp = 0
- for i in range(0, len(s)):
- if s[i] == "(":
- temp += 1
- if s[i] == ")":
- temp -= 1
- if temp == 2:
- temp = i
- break
- for i in range(0, temp):
- st += s[i]
- print(st)
- return st.count(" "+str(val)+" ")
- t = BinaryTree()
- # t.root = t.add(8)
- # t.root.left = t.add(4)
- # t.root.right = t.add(12)
- # t.root.left.left = t.add(2)
- # t.root.left.right = t.add(3)
- # t.root.right.left = t.add(10)
- # t.root.right.right = t.add(14)
- # t.root.left.left.left = t.add(1)
- # t.root.right.right.right = t.add(25)
- # t.root.left.left.right = t.add(3)
- # t.root.left.left.left.left = t.add(0)
- # t.root.right.right.right.right = t.add(100)
- # t.root.right.right.right.left = t.add(3)
- root = Tk()
- root.title("Laba 2")
- root.geometry("640x360+0+0")
- root.resizable(False, False)
- text = Text(wrap=WORD)
- text.place(x=10, y=250, height=66, width=600)
- txt = Entry()
- txt.place(x=50, y=100, height=33, width=133)
- def new():
- s = txt.get()
- s = int(s)
- t.new(s)
- txt.delete(0, END)
- def output():
- t.clearOutput()
- text.delete(1.0, END)
- s = t.createOutput()
- text.insert(1.0, s)
- def find():
- s = txt.get()
- s = int(s)
- txt.delete(0, END)
- s = t.specialSearch(s)
- text.delete(1.0, END)
- text.insert(1.0, "Данных элементов в правом поддереве " + str(s))
- add = Button(root, text="Add", command=new)
- add.place(x=50, y=50, height=33, width=133)
- show = Button(root, text="Print", command=output)
- show.place(x=200, y=100, height=33, width=133)
- replace = Button(root, text="Find", command=find)
- replace.place(x=200, y=50, height=33, width=133)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement