prithivMLmods commited on
Commit
d7b259a
1 Parent(s): 2bb209d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import gradio as gr
2
  import matplotlib.pyplot as plt
3
  import networkx as nx
4
- import io
5
- from PIL import Image
6
 
7
  # Node class for tree structures
8
  class Node:
@@ -22,16 +20,28 @@ def insert(root, key):
22
  return root
23
 
24
  # Function to perform inorder traversal
25
- def inorder(root):
26
- return inorder(root.left) + [root.val] + inorder(root.right) if root else []
 
 
 
 
27
 
28
  # Function to perform preorder traversal
29
- def preorder(root):
30
- return [root.val] + preorder(root.left) + preorder(root.right) if root else []
 
 
 
 
31
 
32
  # Function to perform postorder traversal
33
- def postorder(root):
34
- return postorder(root.left) + postorder(root.right) + [root.val] if root else []
 
 
 
 
35
 
36
  # Function to perform level order traversal
37
  def level_order(root):
@@ -48,9 +58,10 @@ def level_order(root):
48
  queue.append(node.right)
49
  return result
50
 
51
- # Function to visualize the tree structure
52
- def visualize_tree(root):
53
  graph = nx.DiGraph()
 
54
 
55
  def add_edges(node, pos=None):
56
  if node is not None:
@@ -62,15 +73,15 @@ def visualize_tree(root):
62
 
63
  add_edges(root)
64
 
 
 
 
 
65
  plt.figure(figsize=(10, 6))
66
  pos = hierarchy_pos(graph, root.val)
67
- nx.draw(graph, pos, with_labels=True, node_size=5000, node_color="skyblue", font_size=16, font_weight="bold", font_color="black")
68
- buf = io.BytesIO()
69
- plt.savefig(buf, format='png')
70
- buf.seek(0)
71
- img = Image.open(buf)
72
- plt.close()
73
- return img
74
 
75
  # Helper function for hierarchical positioning
76
  def hierarchy_pos(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5):
@@ -94,25 +105,24 @@ def _hierarchy_pos(G, root, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.
94
  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)
95
  return pos
96
 
97
- # Function to perform tree operations and visualization
98
  def perform_tree_operations(values, operation):
99
  root = None
100
  for value in values:
101
  root = insert(root, value)
102
 
 
103
  if operation == "Inorder Traversal":
104
- result = inorder(root)
105
  elif operation == "Preorder Traversal":
106
- result = preorder(root)
107
  elif operation == "Postorder Traversal":
108
- result = postorder(root)
109
  elif operation == "Level Order Traversal":
110
- result = level_order(root)
111
- else:
112
- result = None
113
 
114
- img = visualize_tree(root)
115
- return result, img
116
 
117
  # Gradio interface setup
118
  with gr.Blocks() as demo:
@@ -125,16 +135,15 @@ with gr.Blocks() as demo:
125
  submit = gr.Button("Submit")
126
 
127
  with gr.Column():
128
- output = gr.Textbox(label="Output", lines=4)
129
- graph_output = gr.Image(label="Tree Visualization")
130
 
131
  # Function to handle Gradio interaction
132
  def on_submit(values, operation):
133
  values = list(map(int, values.split(',')))
134
- result, img = perform_tree_operations(values, operation)
135
- return str(result), img
136
 
137
- submit.click(on_submit, inputs=[input_values, operation], outputs=[output, graph_output])
138
 
139
  # Launch the Gradio app
140
  demo.launch()
 
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:
 
20
  return root
21
 
22
  # Function to perform inorder traversal
23
+ def inorder(root, traversal=[]):
24
+ if root:
25
+ inorder(root.left, traversal)
26
+ traversal.append(root.val)
27
+ inorder(root.right, traversal)
28
+ return traversal
29
 
30
  # Function to perform preorder traversal
31
+ def preorder(root, traversal=[]):
32
+ if root:
33
+ traversal.append(root.val)
34
+ preorder(root.left, traversal)
35
+ preorder(root.right, traversal)
36
+ return traversal
37
 
38
  # Function to perform postorder traversal
39
+ def postorder(root, traversal=[]):
40
+ if root:
41
+ postorder(root.left, traversal)
42
+ postorder(root.right, traversal)
43
+ traversal.append(root.val)
44
+ return traversal
45
 
46
  # Function to perform level order traversal
47
  def level_order(root):
 
58
  queue.append(node.right)
59
  return result
60
 
61
+ # Function to visualize the tree structure with traversal
62
+ def visualize_tree_with_traversal(root, traversal):
63
  graph = nx.DiGraph()
64
+ labels = {}
65
 
66
  def add_edges(node, pos=None):
67
  if node is not None:
 
73
 
74
  add_edges(root)
75
 
76
+ # Label the nodes with their position in the traversal
77
+ for idx, val in enumerate(traversal):
78
+ labels[val] = f'{val}\n({idx+1})'
79
+
80
  plt.figure(figsize=(10, 6))
81
  pos = hierarchy_pos(graph, root.val)
82
+ nx.draw(graph, pos, with_labels=True, labels=labels, node_size=5000, node_color="skyblue", font_size=16, font_weight="bold", font_color="black")
83
+ plt.show()
84
+ return plt
 
 
 
 
85
 
86
  # Helper function for hierarchical positioning
87
  def hierarchy_pos(G, root=None, width=1., vert_gap = 0.2, vert_loc = 0, xcenter = 0.5):
 
105
  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)
106
  return pos
107
 
108
+ # Function to perform tree operations and visualization with traversal order
109
  def perform_tree_operations(values, operation):
110
  root = None
111
  for value in values:
112
  root = insert(root, value)
113
 
114
+ traversal = []
115
  if operation == "Inorder Traversal":
116
+ traversal = inorder(root)
117
  elif operation == "Preorder Traversal":
118
+ traversal = preorder(root)
119
  elif operation == "Postorder Traversal":
120
+ traversal = postorder(root)
121
  elif operation == "Level Order Traversal":
122
+ traversal = level_order(root)
 
 
123
 
124
+ plt = visualize_tree_with_traversal(root, traversal)
125
+ return None, plt
126
 
127
  # Gradio interface setup
128
  with gr.Blocks() as demo:
 
135
  submit = gr.Button("Submit")
136
 
137
  with gr.Column():
138
+ graph_output = gr.Plot(label="Tree Visualization")
 
139
 
140
  # Function to handle Gradio interaction
141
  def on_submit(values, operation):
142
  values = list(map(int, values.split(',')))
143
+ _, plt = perform_tree_operations(values, operation)
144
+ return plt
145
 
146
+ submit.click(on_submit, inputs=[input_values, operation], outputs=[graph_output])
147
 
148
  # Launch the Gradio app
149
  demo.launch()