Spaces:
Sleeping
Sleeping
Download Trees/trees_app.py from swetha-coding/Dsa-hub: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/swetha-coding/Dsa-hub/resolve/main/Trees/trees_app.py
- Command line
-
hf download hf://spaces/swetha-coding/Dsa-hub/Trees/trees_app.py
-
curl -L -o trees_app.py https://huggingface.co/spaces/swetha-coding/Dsa-hub/resolve/main/Trees/trees_app.py
1.5 kB
| from flask import Blueprint, render_template, request, jsonify | |
| trees_app = Blueprint("trees_app", __name__, template_folder="templates") | |
| class Node: | |
| def __init__(self, key): | |
| self.key = key | |
| self.left = None | |
| self.right = None | |
| class BST: | |
| def __init__(self): | |
| self.root = None | |
| def insert(self, key): | |
| if self.root is None: | |
| self.root = Node(key) | |
| else: | |
| self._insert(self.root, key) | |
| def _insert(self, root, key): | |
| if key < root.key: | |
| if root.left is None: | |
| root.left = Node(key) | |
| else: | |
| self._insert(root.left, key) | |
| else: | |
| if root.right is None: | |
| root.right = Node(key) | |
| else: | |
| self._insert(root.right, key) | |
| def to_dict(self, root): | |
| if root is None: | |
| return None | |
| children = [] | |
| if root.left: | |
| children.append(self.to_dict(root.left)) | |
| if root.right: | |
| children.append(self.to_dict(root.right)) | |
| return {"name": root.key, "children": children} | |
| bst = BST() | |
| def home(): | |
| return render_template("tree.html") | |
| def insert(): | |
| value = request.form.get("value") | |
| if value: | |
| bst.insert(int(value)) | |
| return jsonify({"status": "ok"}) | |
| def tree(): | |
| return jsonify(bst.to_dict(bst.root)) | |