-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
99 lines (81 loc) · 3.5 KB
/
script.py
File metadata and controls
99 lines (81 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import tkinter as tk
from node import Node
from bst import AVLTree
# ===========================================================
# AVL VISUALIZER USING TKINTER
# ===========================================================
class BSTVisualizer:
def __init__(self, window):
self.window = window
self.window.title("Binary Search Tree Visualizer")
self.window.geometry("1000x600")
self.window.configure(bg="white")
self.tree = AVLTree()
self.root = None
# Entry widget for input
self.entry = tk.Entry(self.window, width=15, font=('Arial', 14))
self.entry.pack(pady=10)
# Insert button
self.insert_button = tk.Button(
self.window, text="Insert", command=self.insert_value,
font=('Arial', 12), bg="#4CAF50", fg="black"
)
self.insert_button.pack(pady=5)
#Deletion button
self.clear_button = tk.Button(
self.window, text="Clear Tree", command=self.clear_tree,
font=('Arial', 12), bg="#f44336", fg="white"
)
self.clear_button.pack(pady=5)
# Canvas for drawing the tree
self.canvas = tk.Canvas(self.window, width=980, height=500, bg="white", highlightthickness=1, highlightbackground="gray")
self.canvas.pack(pady=10)
# Label for messages
self.status = tk.Label(self.window, text="Enter a number and click Insert", font=('Arial', 12), bg="white")
self.status.pack()
# Function to handle inserting values
def insert_value(self):
value = self.entry.get().strip()
# Validate input
if not value.lstrip('-').isdigit():
self.status.config(text="Please enter a valid integer.", fg="red")
return
key = int(value)
self.root = self.tree.insert(self.root, key)
self.entry.delete(0, tk.END)
self.status.config(text=f"Inserted {key} into the BST.", fg="green")
#insert function herreeeeeee---------------
# Redraw tree after insertion
self.redraw_tree()
# Recursive function to draw the tree
def draw_tree(self, node, x, y, x_offset):
if node is not None:
# Draw left child and connecting line
if node.left:
self.canvas.create_line(x, y, x - x_offset, y + 60, fill="gray", width=2)
self.draw_tree(node.left, x - x_offset, y + 60, x_offset / 2)
# Draw right child and connecting line
if node.right:
self.canvas.create_line(x, y, x + x_offset, y + 60, fill="gray", width=2)
self.draw_tree(node.right, x + x_offset, y + 60, x_offset / 2)
# Draw node (circle)
self.canvas.create_oval(x - 20, y - 20, x + 20, y + 20, fill="#4CAF50", outline="black", width=2)
self.canvas.create_text(x, y, text=str(node.key), fill="white", font=('Arial', 12, 'bold'))
# Function to refresh the canvas
def redraw_tree(self):
self.canvas.delete("all")
if self.root:
self.draw_tree(self.root, 500, 50, 250)
else:
self.status.config(text="Tree is empty.", fg="black")
def clear_tree(self):
self.root = None
self.redraw_tree()
self.status.config(text="Tree cleared.", fg="blue")
# ===========================================================
# MAIN PROGRAM
# ===========================================================
if __name__ == "__main__":
window = tk.Tk()
app = BSTVisualizer(window)
window.mainloop()