Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potentially Done the Tree Builder #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions lab/tree_builder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

from anytree import Node
from anytree.exporter import DotExporter
from anytree.search import find_by_attr
from anytree import RenderTree

class TreeNode(Node):

Expand All @@ -15,19 +18,47 @@ class TreeBuilder(object):
def __init__(self, root):
# root will be type TreeNode
self.root = root
self.all = []
self.size = 0

# Function to push to tree
def push(self, node):
# TODO
pass
def insert(current_node, node):
if node.value < current_node.val:
if current_node.left != None: # Left child
insert(current_node.left, node)
else:
current_node.left = node
self.size += 1
elif node.value > current_node.val:
if current_node.right != None: # Right child
insert(current_node.right, node)
else:
current_node.right = node
self.size += 1
else:
# Value already exists in the tree, do nothing
pass

insert(self.root, node)
self.all.append(node.name)

# Function to remove node from tree
def pop(self, name):
# TODO
pass
def delete(nodeDel):
node = find_by_attr(self.root, name)
node.parent = None
return

# Find the node to delete
nodeDel = name in self.all
if nodeDel:
delete(name)

# Function to export tree as png(Use DotExporter)
def export(self, file="tree"):
# TODO
pass
file_path = f"{file}.png"
DotExporter(self.root).to_picture(file_path)
print(f"Tree exported to {file_path}")