Spaces:
Runtime error
Runtime error
File size: 1,504 Bytes
cf69cfa | 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 | 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()
@trees_app.route("/", methods=["GET"])
def home():
return render_template("tree.html")
@trees_app.route("/insert", methods=["POST"])
def insert():
value = request.form.get("value")
if value:
bst.insert(int(value))
return jsonify({"status": "ok"})
@trees_app.route("/tree", methods=["GET"])
def tree():
return jsonify(bst.to_dict(bst.root))
|