forked from jyx-fyh/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitreePreVisualizer.py
More file actions
69 lines (55 loc) · 1.91 KB
/
BitreePreVisualizer.py
File metadata and controls
69 lines (55 loc) · 1.91 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
import tkinter as tk
from tkinter import Canvas, ttk
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def deserialize_preorder(data):
def helper(queue):
val = next(queue)
if val == "#":
return None
node = TreeNode(int(val))
node.left = helper(queue)
node.right = helper(queue)
return node
data = data.split(",")
queue = iter(data)
return helper(queue)
def draw_tree(canvas, root, x, y, x_spacing):
if root:
canvas.create_oval(x - 10, y - 10, x + 10, y + 10, fill="white")
canvas.create_text(x, y, text=str(root.val))
if root.left:
x_left = x - x_spacing
y_left = y + 50
canvas.create_line(x, y + 10, x_left, y_left - 10, arrow=tk.LAST)
draw_tree(canvas, root.left, x_left, y_left, x_spacing / 2)
if root.right:
x_right = x + x_spacing
y_right = y + 50
canvas.create_line(x, y + 10, x_right, y_right - 10, arrow=tk.LAST)
draw_tree(canvas, root.right, x_right, y_right, x_spacing / 2)
def visualize_binary_tree():
def visualize():
serialized_tree = input_entry.get()
root = deserialize_preorder(serialized_tree)
canvas.delete("all")
x_start = 600
y_start = 80
x_spacing = 300
draw_tree(canvas, root, x_start, y_start, x_spacing)
window = tk.Tk()
window.title("Binary Tree Visualization")
input_label = ttk.Label(window, text="Enter Preorder Serialization:")
input_label.pack()
input_entry = ttk.Entry(window)
input_entry.pack()
visualize_button = ttk.Button(window, text="Visualize", command=visualize)
visualize_button.pack()
canvas = Canvas(window, width=1200, height=600)
canvas.pack()
window.mainloop()
if __name__ == "__main__":
visualize_binary_tree()