-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from BenakDeepak/main
added visualisaton file in the tree
- Loading branch information
Showing
6 changed files
with
596 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Visualizing tools | ||
|
||
# 🎯 Goal | ||
|
||
The primary objective of this project is to explain your specific project's goal here. It includes implementing, visualizing, and analyzing relevant key features based on the files and project you shared. | ||
|
||
# 🧵 Dataset / Input | ||
|
||
|
||
This project does not rely on a predefined dataset. Instead, it takes user input in the following formats: | ||
1. File Upload: Users can upload a file containing the input data. Make sure to follow the input file format described below. | ||
2. Manual Entry: Users can manually input the data. | ||
|
||
# File Format Requirements: | ||
• File1: Accepts input in format (e.g., CSV, TXT), containing fields such as field_1, field_2, and so on. | ||
• File2: Requires format (e.g., JSON), with key fields such as key_1, key_2, etc. | ||
• File3: Describes format, with structure <key>: <value>. | ||
• File4: Takes file types or formats, and expects the following fields... | ||
|
||
Example Input: | ||
• Manual Input Format Example: | ||
plaintext | ||
Copy code | ||
field_1: value | ||
field_2: value | ||
... | ||
|
||
• File Input Example: | ||
csv | ||
Copy code | ||
column_1,column_2,column_3 | ||
value_1,value_2,value_3 | ||
|
||
|
||
# 🧾 Description | ||
This project provides a web-based platform developed using Streamlit that allows users to interact with the system through manual input or file uploads. The tool implements specific project functionalities described in each of the four files (e.g., algorithm, database operations, graph construction). | ||
The interface dynamically updates based on the input, offering real-time feedback on the actions performed, such as building structures, visualizations, or other functional operations (adjust based on your project). | ||
Features: | ||
• Real-time visualization of data structures or operations based on user input. | ||
• Multiple input modes: manual and file-based. | ||
• Dynamic operations that are applied step-by-step to the input data. | ||
|
||
|
||
# 🧮 What I Had Done! | ||
1. Developed an interactive web interface using Streamlit. | ||
2. Implemented features for file uploading and manual input handling. | ||
3. Visualized the operations or data structures in real-time. | ||
4. Provided feedback on every step of the process (e.g., rotations, rebalancing in trees, graph construction). | ||
5. Output examples based on the input data. | ||
|
||
Sample Output (based on the project): | ||
• After processing the input, the system generates visualizations, processed data, or other outputs relevant to the task. For example: | ||
o Tree Visualization or Graph Construction output: | ||
plaintext | ||
Copy code | ||
[Graph or tree] | ||
o Processed Data Output: | ||
plaintext | ||
Copy code | ||
key_1: result_1 | ||
key_2: result_2 | ||
|
||
|
||
# 📚 Libraries Needed | ||
To run this project, install the following libraries: | ||
• streamlit: for building the web interface. | ||
• graphviz (or other visualization libraries as per your needs). | ||
• Any other dependencies needed by your project. | ||
Install them using: | ||
bash | ||
Copy code | ||
pip install -r requirements.txt | ||
Requirements File (requirements.txt): | ||
plaintext | ||
Copy code | ||
streamlit | ||
graphviz | ||
|
||
|
||
# 📊 Exploratory Data Analysis Results | ||
No EDA is required as the project is based on user inputs. However, real-time graphical outputs or visual representations of the data processing steps provide insights into how the system operates based on user inputs. | ||
📈 Performance of the Algorithms | ||
Since the project focuses on algorithms like trees, graphs, or other relevant structures (depending on the files), no accuracy metrics are required. Performance can be verified based on the correct execution of: | ||
• Rotations and balance maintenance in trees. | ||
• Graph construction or other algorithmic outputs. | ||
|
||
|
||
# 📢 Conclusion | ||
The project effectively demonstrates how to process user inputs and visualize results in real-time. The system allows for dynamic interaction through both manual entry and file upload, offering flexibility in how users interact with the system. | ||
|
||
|
||
# ✒️ Your Signature | ||
Benak Deepak | ||
|
125 changes: 125 additions & 0 deletions
125
Algorithms_and_Data_Structures/visualizer/binary_search_tree_visualizer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import streamlit as st | ||
import graphviz as gv | ||
|
||
# TreeNode class definition for Binary Search Tree (BST) | ||
class TreeNode: | ||
def __init__(self, key): | ||
self.left = None | ||
self.right = None | ||
self.val = key | ||
|
||
# Binary Search Tree (BST) class definition | ||
class BinarySearchTree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
def insert(self, key): | ||
if self.root is None: | ||
self.root = TreeNode(key) | ||
else: | ||
self._insert_rec(self.root, key) | ||
|
||
def _insert_rec(self, node, key): | ||
if key < node.val: | ||
if node.left is None: | ||
node.left = TreeNode(key) | ||
else: | ||
self._insert_rec(node.left, key) | ||
else: | ||
if node.right is None: | ||
node.right = TreeNode(key) | ||
else: | ||
self._insert_rec(node.right, key) | ||
|
||
def delete(self, key): | ||
self.root = self._delete_rec(self.root, key) | ||
|
||
def _delete_rec(self, node, key): | ||
if node is None: | ||
return node | ||
|
||
if key < node.val: | ||
node.left = self._delete_rec(node.left, key) | ||
elif key > node.val: | ||
node.right = self._delete_rec(node.right, key) | ||
else: | ||
# Node with only one child or no child | ||
if node.left is None: | ||
return node.right | ||
elif node.right is None: | ||
return node.left | ||
|
||
# Node with two children, find the inorder successor (smallest in the right subtree) | ||
temp = self._min_value_node(node.right) | ||
node.val = temp.val | ||
node.right = self._delete_rec(node.right, temp.val) | ||
|
||
return node | ||
|
||
def _min_value_node(self, node): | ||
current = node | ||
while current.left is not None: | ||
current = current.left | ||
return current | ||
|
||
def inorder(self, node): | ||
result = [] | ||
if node: | ||
result = self.inorder(node.left) | ||
result.append(node.val) | ||
result = result + self.inorder(node.right) | ||
return result | ||
|
||
def visualize_tree(self): | ||
dot = gv.Digraph() | ||
|
||
def add_edges(node): | ||
if node: | ||
dot.node(str(node.val)) | ||
if node.left: | ||
dot.edge(str(node.val), str(node.left.val), label="L") | ||
add_edges(node.left) | ||
if node.right: | ||
dot.edge(str(node.val), str(node.right.val), label="R") | ||
add_edges(node.right) | ||
|
||
if self.root: | ||
add_edges(self.root) | ||
return dot | ||
|
||
# Streamlit UI code | ||
def app(): | ||
st.title("Binary Search Tree (BST) Visualization") | ||
|
||
# Initialize Binary Search Tree | ||
if 'tree' not in st.session_state: | ||
st.session_state.tree = BinarySearchTree() | ||
|
||
# Insert node input | ||
insert_value = st.number_input("Insert a value into the BST:", value=0, step=1) | ||
if st.button("Insert"): | ||
st.session_state.tree.insert(insert_value) | ||
st.success(f"Inserted {insert_value} into the BST") | ||
|
||
# Delete node input | ||
delete_value = st.number_input("Delete a value from the BST:", value=0, step=1) | ||
if st.button("Delete"): | ||
st.session_state.tree.delete(delete_value) | ||
st.success(f"Deleted {delete_value} from the BST") | ||
|
||
# Inorder Traversal display | ||
if st.button("Inorder Traversal"): | ||
result = st.session_state.tree.inorder(st.session_state.tree.root) | ||
st.write("Inorder Traversal:", result) | ||
|
||
# Tree visualization using Graphviz | ||
if st.button("Visualize Tree"): | ||
if st.session_state.tree.root: | ||
dot = st.session_state.tree.visualize_tree() | ||
st.graphviz_chart(dot.source) | ||
else: | ||
st.write("The tree is empty!") | ||
|
||
# Run the Streamlit app | ||
if __name__ == "__main__": | ||
app() |
140 changes: 140 additions & 0 deletions
140
Algorithms_and_Data_Structures/visualizer/binary_tree_visualizer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import streamlit as st | ||
import graphviz as gv | ||
from collections import deque | ||
|
||
# TreeNode class definition for Binary Tree | ||
class TreeNode: | ||
def __init__(self, key): | ||
self.left = None | ||
self.right = None | ||
self.val = key | ||
|
||
# Binary Tree class definition | ||
class BinaryTree: | ||
def __init__(self): | ||
self.root = None | ||
|
||
def insert(self, key): | ||
new_node = TreeNode(key) | ||
if self.root is None: | ||
self.root = new_node | ||
else: | ||
# Level-order traversal to insert at the first available position | ||
q = deque([self.root]) | ||
while q: | ||
current = q.popleft() | ||
if not current.left: | ||
current.left = new_node | ||
break | ||
else: | ||
q.append(current.left) | ||
if not current.right: | ||
current.right = new_node | ||
break | ||
else: | ||
q.append(current.right) | ||
|
||
def delete(self, key): | ||
if self.root is None: | ||
return None | ||
|
||
if self.root.left is None and self.root.right is None: | ||
if self.root.val == key: | ||
self.root = None | ||
return self.root | ||
|
||
# Level-order traversal to find the node to delete | ||
q = deque([self.root]) | ||
node_to_delete = None | ||
last_node = None | ||
while q: | ||
last_node = q.popleft() | ||
if last_node.val == key: | ||
node_to_delete = last_node | ||
if last_node.left: | ||
q.append(last_node.left) | ||
if last_node.right: | ||
q.append(last_node.right) | ||
|
||
# If node_to_delete is found, replace it with the last node and remove last node | ||
if node_to_delete: | ||
node_to_delete.val = last_node.val | ||
self._delete_last_node(self.root, last_node) | ||
|
||
def _delete_last_node(self, root, last_node): | ||
# Level-order traversal to remove the last node | ||
q = deque([root]) | ||
while q: | ||
current = q.popleft() | ||
if current.left: | ||
if current.left == last_node: | ||
current.left = None | ||
return | ||
q.append(current.left) | ||
if current.right: | ||
if current.right == last_node: | ||
current.right = None | ||
return | ||
q.append(current.right) | ||
|
||
def inorder(self, node): | ||
result = [] | ||
if node: | ||
result = self.inorder(node.left) | ||
result.append(node.val) | ||
result = result + self.inorder(node.right) | ||
return result | ||
|
||
def visualize_tree(self): | ||
dot = gv.Digraph() | ||
|
||
def add_edges(node): | ||
if node: | ||
dot.node(str(node.val)) | ||
if node.left: | ||
dot.edge(str(node.val), str(node.left.val), label="L") | ||
add_edges(node.left) | ||
if node.right: | ||
dot.edge(str(node.val), str(node.right.val), label="R") | ||
add_edges(node.right) | ||
|
||
if self.root: | ||
add_edges(self.root) | ||
return dot | ||
|
||
# Streamlit UI code | ||
def app(): | ||
st.title("Binary Tree Visualization") | ||
|
||
# Create a BinaryTree instance | ||
if 'tree' not in st.session_state: | ||
st.session_state.tree = BinaryTree() | ||
|
||
# Insert node input | ||
insert_value = st.number_input("Insert a value into the Binary Tree:", value=0, step=1) | ||
if st.button("Insert"): | ||
st.session_state.tree.insert(insert_value) | ||
st.success(f"Inserted {insert_value} into the tree") | ||
|
||
# Delete node input | ||
delete_value = st.number_input("Delete a value from the Binary Tree:", value=0, step=1) | ||
if st.button("Delete"): | ||
st.session_state.tree.delete(delete_value) | ||
st.success(f"Deleted {delete_value} from the tree") | ||
|
||
# Traversals | ||
if st.button("Inorder Traversal"): | ||
result = st.session_state.tree.inorder(st.session_state.tree.root) | ||
st.write("Inorder Traversal:", result) | ||
|
||
# Tree visualization using Graphviz | ||
if st.button("Visualize Tree"): | ||
if st.session_state.tree.root: | ||
dot = st.session_state.tree.visualize_tree() | ||
st.graphviz_chart(dot.source) | ||
else: | ||
st.write("The tree is empty!") | ||
|
||
# Run the Streamlit app | ||
if __name__ == "__main__": | ||
app() |
Oops, something went wrong.