prithivMLmods commited on
Commit
044a50c
1 Parent(s): 059cf3d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import networkx as nx
4
+
5
+ # Node class for tree structures
6
+ class Node:
7
+ def __init__(self, key):
8
+ self.left = None
9
+ self.right = None
10
+ self.val = key
11
+
12
+ # Function to insert nodes in a binary search tree
13
+ def insert(root, key):
14
+ if root is None:
15
+ return Node(key)
16
+ if key < root.val:
17
+ root.left = insert(root.left, key)
18
+ else:
19
+ root.right = insert(root.right, key)
20
+ return root
21
+
22
+ # Function to perform inorder traversal
23
+ def inorder(root):
24
+ return inorder(root.left) + [root.val] + inorder(root.right) if root else []
25
+
26
+ # Function to perform preorder traversal
27
+ def preorder(root):
28
+ return [root.val] + preorder(root.left) + preorder(root.right) if root else []
29
+
30
+ # Function to perform postorder traversal
31
+ def postorder(root):
32
+ return postorder(root.left) + postorder(root.right) + [root.val] if root else []
33
+
34
+ # Function to perform level order traversal
35
+ def level_order(root):
36
+ result = []
37
+ if root is None:
38
+ return result
39
+ queue = [root]
40
+ while queue:
41
+ node = queue.pop(0)
42
+ result.append(node.val)
43
+ if node.left:
44
+ queue.append(node.left)
45
+ if node.right:
46
+ queue.append(node.right)
47
+ return result
48
+
49
+ # Function to visualize the tree using networkx and matplotlib
50
+ def visualize_tree(root):
51
+ graph = nx.DiGraph()
52
+ def add_edges(node, pos=None):
53
+ if node is not None:
54
+ graph.add_node(node.val)
55
+ if pos is not None:
56
+ graph.add_edge(pos, node.val)
57
+ add_edges(node.left, node.val)
58
+ add_edges(node.right, node.val)
59
+ add_edges(root)
60
+
61
+ plt.figure(figsize=(10, 6))
62
+ pos = hierarchy_pos(graph, root.val)
63
+ nx.draw(graph, pos, with_labels=True, node_size=5000, node_color="skyblue", font_size=16, font_weight="bold", font_color="black")
64
+ plt.show()
65
+
66
+ def hierarchy_pos(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5):
67
+ pos = _hierarchy_pos(G, root, width, vert_gap, vert_loc, xcenter)
68
+ return pos
69
+
70
+ def _hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5, pos=None, parent=None, parsed=[]):
71
+ if pos is None:
72
+ pos = {root: (xcenter, vert_loc)}
73
+ else:
74
+ pos[root] = (xcenter, vert_loc)
75
+ children = list(G.neighbors(root))
76
+ if not isinstance(G, nx.DiGraph) and parent is not None:
77
+ children.remove(parent)
78
+ if len(children) != 0:
79
+ dx = width / len(children)
80
+ nextx = xcenter - width/2 - dx/2
81
+ for child in children:
82
+ nextx += dx
83
+ pos = _hierarchy_pos(G, child, width=dx, vert_gap=vert_gap, vert_loc=vert_loc-vert_gap, xcenter=nextx, pos=pos, parent=root, parsed=parsed)
84
+ return pos
85
+
86
+ def perform_tree_operations(values, operation):
87
+ root = None
88
+ for value in values:
89
+ root = insert(root, value)
90
+
91
+ if operation == "Inorder Traversal":
92
+ result = inorder(root)
93
+ elif operation == "Preorder Traversal":
94
+ result = preorder(root)
95
+ elif operation == "Postorder Traversal":
96
+ result = postorder(root)
97
+ elif operation == "Level Order Traversal":
98
+ result = level_order(root)
99
+ elif operation == "Visualize Tree":
100
+ visualize_tree(root)
101
+ return "Tree Visualization"
102
+
103
+ return result
104
+
105
+ # Gradio Interface
106
+ with gr.Blocks() as demo:
107
+ gr.Markdown("## Tree Operations Visualization")
108
+
109
+ with gr.Row():
110
+ with gr.Column():
111
+ input_values = gr.Textbox(label="Enter Values (comma-separated)", placeholder="e.g., 10, 20, 5, 15, 25")
112
+ operation = gr.Radio(choices=["Inorder Traversal", "Preorder Traversal", "Postorder Traversal", "Level Order Traversal", "Visualize Tree"], label="Select Operation")
113
+ submit = gr.Button("Submit")
114
+
115
+ with gr.Column():
116
+ output = gr.Textbox(label="Output", lines=4)
117
+ graph_output = gr.Plot(label="Tree Visualization")
118
+
119
+ def on_submit(values, operation):
120
+ values = list(map(int, values.split(',')))
121
+ result = perform_tree_operations(values, operation)
122
+ return str(result)
123
+
124
+ submit.click(on_submit, inputs=[input_values, operation], outputs=[output, graph_output])
125
+
126
+ demo.launch()