-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion Tree Visualizer.py
More file actions
109 lines (82 loc) · 3.37 KB
/
Recursion Tree Visualizer.py
File metadata and controls
109 lines (82 loc) · 3.37 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
100
101
102
103
104
105
106
107
108
109
import tkinter as tk
from tkinter import messagebox
class RecursionTreeVisualizer:
def __init__(self, root):
self.root = root
self.root.title("Recursion Tree Visualizer")
self.root.geometry("1000x700")
title = tk.Label(root, text="Recursion Tree Visualizer",
font=("Arial", 16, "bold"))
title.pack(pady=10)
control_frame = tk.Frame(root)
control_frame.pack()
tk.Label(control_frame, text="Enter n:").grid(row=0, column=0, padx=5)
self.input_entry = tk.Entry(control_frame, width=10)
self.input_entry.grid(row=0, column=1, padx=5)
tk.Button(control_frame, text="Visualize Factorial",
command=self.visualize_factorial).grid(row=0, column=2, padx=5)
tk.Button(control_frame, text="Visualize Fibonacci",
command=self.visualize_fibonacci).grid(row=0, column=3, padx=5)
tk.Button(control_frame, text="Clear",
command=self.clear_canvas).grid(row=0, column=4, padx=5)
self.canvas = tk.Canvas(root, bg="white", width=950, height=550)
self.canvas.pack(pady=20)
# -------------------------
# Clear Canvas
# -------------------------
def clear_canvas(self):
self.canvas.delete("all")
# -------------------------
# Factorial Tree
# -------------------------
def visualize_factorial(self):
self.clear_canvas()
try:
n = int(self.input_entry.get())
if n < 0:
raise ValueError
except:
messagebox.showerror("Error", "Please enter a valid non-negative integer.")
return
self.draw_factorial(n, 500, 50, 200)
def draw_factorial(self, n, x, y, offset):
self.canvas.create_oval(x-25, y-20, x+25, y+20, fill="lightblue")
self.canvas.create_text(x, y, text=f"fact({n})")
if n <= 1:
return
child_x = x
child_y = y + 100
self.canvas.create_line(x, y+20, child_x, child_y-20)
self.draw_factorial(n-1, child_x, child_y, offset // 2)
# -------------------------
# Fibonacci Tree
# -------------------------
def visualize_fibonacci(self):
self.clear_canvas()
try:
n = int(self.input_entry.get())
if n < 0 or n > 8: # limit to avoid too large tree
raise ValueError
except:
messagebox.showerror("Error", "Enter integer between 0 and 8 (Fibonacci grows fast).")
return
self.draw_fibonacci(n, 500, 50, 250)
def draw_fibonacci(self, n, x, y, offset):
self.canvas.create_oval(x-25, y-20, x+25, y+20, fill="lightgreen")
self.canvas.create_text(x, y, text=f"fib({n})")
if n <= 1:
return
left_x = x - offset
right_x = x + offset
child_y = y + 100
self.canvas.create_line(x, y+20, left_x, child_y-20)
self.canvas.create_line(x, y+20, right_x, child_y-20)
self.draw_fibonacci(n-1, left_x, child_y, offset // 2)
self.draw_fibonacci(n-2, right_x, child_y, offset // 2)
# -------------------------
# Run App
# -------------------------
if __name__ == "__main__":
root = tk.Tk()
app = RecursionTreeVisualizer(root)
root.mainloop()