Printing a Path to a Leaf in an AVL Tree - python-3.x

I am trying to print the path to every single leaf in this AVL Tree. My question is: why does the program spit out the memory locations and file names of the element instead of the element?
from BinaryTree import BinaryTree
from BinaryTree import TreeNode
class AVLTree(BinaryTree):
def __init__(self):
BinaryTree.__init__(self)
# Override the createNewNode method to create an AVLTreeNode
def createNewNode(self, e):
return AVLTreeNode(e)
def getPath(self, o):
path = BinaryTree.path(self, o);
return path
# Override the insert method to balance the tree if necessary
def insert(self, o):
successful = BinaryTree.insert(self, o)
if not successful:
return False # o is already in the tree
else:
self.balancePath(o) # Balance from o to the root if necessary
return True # o is inserted
# Update the height of a specified node
def updateHeight(self, node):
if node.left == None and node.right == None: # node is a leaf
node.height = 0
elif node.left == None: # node has no left subtree
node.height = 1 + (node.right).height
elif node.right == None: # node has no right subtree
node.height = 1 + (node.left).height
else:
node.height = 1 + max((node.right).height, (node.left).height)
# Balance the nodes in the path from the specified
# node to the root if necessary
def balancePath(self, o):
path = BinaryTree.path(self, o)
for i in range(len(path) - 1, -1, -1):
A = path[i]
self.updateHeight(A)
parentOfA = None if (A == self.root) else path[i - 1]
if self.balanceFactor(A) == -2:
if self.balanceFactor(A.left) <= 0:
self.balanceLL(A, parentOfA) # Perform LL rotation
else:
self.balanceLR(A, parentOfA) # Perform LR rotation
elif self.balanceFactor(A) == +2:
if self.balanceFactor(A.right) >= 0:
self.balanceRR(A, parentOfA) # Perform RR rotation
else:
self.balanceRL(A, parentOfA) # Perform RL rotation
# Return the balance factor of the node
def balanceFactor(self, node):
if node.right == None: # node has no right subtree
return -node.height
elif node.left == None: # node has no left subtree
return +node.height
else:
return (node.right).height - (node.left).height
# Balance LL (see Figure 14.2)
def balanceLL(self, A, parentOfA):
B = A.left # A is left-heavy and B is left-heavy
if A == self.root:
self.root = B
else:
if parentOfA.left == A:
parentOfA.left = B
else:
parentOfA.right = B
A.left = B.right # Make T2 the left subtree of A
B.right = A # Make A the left child of B
self.updateHeight(A)
self.updateHeight(B)
def getSearch(self, o):
return BinaryTree.search(self, o)
# Balance LR (see Figure 14.2(c))
def balanceLR(self, A, parentOfA):
B = A.left # A is left-heavy
C = B.right # B is right-heavy
if A == self.root:
self.root = C
else:
if parentOfA.left == A:
parentOfA.left = C
else:
parentOfA.right = C
A.left = C.right # Make T3 the left subtree of A
B.right = C.left # Make T2 the right subtree of B
C.left = B
C.right = A
# Adjust heights
self.updateHeight(A)
self.updateHeight(B)
self.updateHeight(C)
# Balance RR (see Figure 14.2(b))
def balanceRR(self, A, parentOfA):
B = A.right # A is right-heavy and B is right-heavy
if A == self.root:
self.root = B
else:
if parentOfA.left == A:
parentOfA.left = B
else:
parentOfA.right = B
A.right = B.left # Make T2 the right subtree of A
B.left = A
self.updateHeight(A)
self.updateHeight(B)
# Balance RL (see Figure 14.2(d))
def balanceRL(self, A, parentOfA):
B = A.right # A is right-heavy
C = B.left # B is left-heavy
if A == self.root:
self.root = C
else:
if parentOfA.left == A:
parentOfA.left = C
else:
parentOfA.right = C
A.right = C.left # Make T2 the right subtree of A
B.left = C.right # Make T3 the left subtree of B
C.left = A
C.right = B
# Adjust heights
self.updateHeight(A)
self.updateHeight(B)
self.updateHeight(C)
# Delete an element from the binary tree.
# Return True if the element is deleted successfully
# Return False if the element is not in the tree
def delete(self, element):
if self.root == None:
return False # Element is not in the tree
# Locate the node to be deleted and also locate its parent node
parent = None
current = self.root
while current != None:
if element < current.element:
parent = current
current = current.left
elif element > current.element:
parent = current
current = current.right
else:
break # Element is in the tree pointed by current
if current == None:
return False # Element is not in the tree
# Case 1: current has no left children (See Figure 23.6)
if current.left == None:
# Connect the parent with the right child of the current node
if parent == None:
root = current.right
else:
if element < parent.element:
parent.left = current.right
else:
parent.right = current.right
# Balance the tree if necessary
self.balancePath(parent.element)
else:
# Case 2: The current node has a left child
# Locate the rightmost node in the left subtree of
# the current node and also its parent
parentOfRightMost = current
rightMost = current.left
while rightMost.right != None:
parentOfRightMost = rightMost
rightMost = rightMost.right # Keep going to the right
# Replace the element in current by the element in rightMost
current.element = rightMost.element
# Eliminate rightmost node
if parentOfRightMost.right == rightMost:
parentOfRightMost.right = rightMost.left
else:
# Special case: parentOfRightMost is current
parentOfRightMost.left = rightMost.left
# Balance the tree if necessary
self.balancePath(parentOfRightMost.element)
self.size -= 1 # One element deleted
return True # Element inserted
def getLeafNodes(self):
return BinaryTree.leafNodes(self)
# AVLTreeNode is TreeNode plus height
class AVLTreeNode(TreeNode):
def __init__(self, e):
self.height = 0 # New data field
TreeNode.__init__(self, e)
this is the binary tree it calls
class BinaryTree:
def __init__(self):
self.counter = -1
self.root = None
self.size = 0
# Return True if the element is in the tree
def search(self, e):
current = self.root # Start from the root
while current != None:
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else: # element matches current.element
return True # Element is found
return False
def __iter__(self):
return self
def __next__(self):
x = self.inorder()
self.num = []
for i in range(x.getSize()):
self.num.append(x.pop())
self.num.reverse()
while True:
self.counter += 1
break
return self.num[self.counter]
# Insert element e into the binary search tree
# Return True if the element is inserted successfully
def insert(self, e):
if self.root == None:
self.root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.size += 1 # Increase tree size
return True # Element inserted
# Create a new TreeNode for element e
def createNewNode(self, e):
return TreeNode(e)
# Return the size of the tree
def getSize(self):
return self.size
# Inorder traversal from the root
def inorder(self):
self.inorderHelper(self.root)
# Inorder traversal from a subtree
def inorderHelper(self, r):
if r != None:
self.inorderHelper(r.left)
print(r.element, end = " ")
self.inorderHelper(r.right)
# Postorder traversal from the root
def postorder(self):
self.postorderHelper(self.root)
# Postorder traversal from a subtree
def postorderHelper(self, root):
if root != None:
self.postorderHelper(root.left)
self.postorderHelper(root.right)
print(root.element, end = " ")
# Preorder traversal from the root
def preorder(self):
self.preorderHelper(self.root)
# Preorder traversal from a subtree
def preorderHelper(self, root):
if root != None:
print(root.element, end = " ")
self.preorderHelper(root.left)
self.preorderHelper(root.right)
# Returns a path from the root leading to the specified element
def path(self, e):
path = []
current = self.root # Start from the root
while current != None:
path.append(current) # Add the node to the list
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else:
break
return path # Return an array of nodes
# Delete an element from the binary search tree.
# Return True if the element is deleted successfully
# Return False if the element is not in the tree
def delete(self, e):
# Locate the node to be deleted and its parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
break # Element is in the tree pointed by current
if current == None:
return False # Element is not in the tree
# Case 1: current has no left children
if current.left == None:
# Connect the parent with the right child of the current node
if parent == None:
self.root = current.right
else:
if e < parent.element:
parent.left = current.right
else:
parent.right = current.right
else:
# Case 2: The current node has a left child
# Locate the rightmost node in the left subtree of
# the current node and also its parent
parentOfRightMost = current
rightMost = current.left
while rightMost.right != None:
parentOfRightMost = rightMost
rightMost = rightMost.right # Keep going to the right
# Replace the element in current by the element in rightMost
current.element = rightMost.element
# Eliminate rightmost node
if parentOfRightMost.right == rightMost:
parentOfRightMost.right = rightMost.left
else:
# Special case: parentOfRightMost == current
parentOfRightMost.left = rightMost.left
self.size -= 1
return True # Element deleted
def leafNodes(self):
self.leaves = []
return self.leafNodesHelper(self.root)
def leafNodesHelper(self, root):
if root != None:
if root.left == None and root.right == None:
self.leaves.append(root.element)
else:
self.leafNodesHelper(root.left)
self.leafNodesHelper(root.right)
return self.leaves
# Return true if the tree is empty
def isEmpty(self):
return self.size == 0
# Remove all elements from the tree
def clear(self):
self.root == None
self.size == 0
# Return the root of the tree
def getRoot(self):
return self.root
class TreeNode:
def __init__(self, e):
self.element = e
self.left = None # Point to the left node, default None
self.right = None # Point to the right node, default None
this is the test program that is frustrating me
from AVLTree20_3 import AVLTree
tree = AVLTree()
for i in range(1, 101):
tree.insert(i)
x = tree.getLeafNodes()
for i in range(len(x)):
path = tree.getPath(x[i])
print(path)
Please help me figure out why this won't work. I have tried a debugger and printing from all three files but none seem to print out properly why is this?

Your problem is that the path method returns a list of nodes.
have two solutions define the 1st override method (str) in the node class.
Or cross it as follows:
from AVLTree20_3 import AVLTree
tree = AVLTree()
for i in range(1, 101):
tree.insert(i)
x = tree.getLeafNodes()
for i in range(len(x)):
path = [x.element for x in tree.getPath(x[i])]
print(path,"\n---------------------")

Related

Python3 syntax in Lowest common ancestor

Classic problem of LCA in BT: Given a Binary Tree find the lowest common ancestor.
I found a Python2 solution but don't know how to fix it in Python3.
# This is Python2, but having error in Python3 such that
# parent += node if all(subs) else max(subs),
# TypeError: '>' not supported between instances of 'NoneType' and 'NoneType'`
# lca.py
class Solution:
def lowestCommonAncestor(self, root, p, q):
answer = []
stack = [[root, answer]]
while stack:
top = stack.pop()
(node, parent), subs = top[:2], top[2:]
if node in (None, p, q):
parent += node,
elif not subs:
stack += top, [node.right, top], [node.left, top]
else:
parent += node if all(subs) else max(subs), # this is the problem in Python3
return answer[0]
s = Solution()
root = tree.create_tree3()
p = tree.find(root, 6)
q = tree.find(root, 1)
print(f'p = {p}, q = {q}')
lca = s.lowestCommonAncestor(root, p, q)
print(f"lca = {lca}")
# tree.py
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
return str(self.val)
def find(root, val):
if root is None:
return None
if root.val == val:
return root
left = find(root.left, val)
right = find(root.right, val)
if left is not None:
return left
else:
return right
"""
3
5 1
6 2 0 8
7 4
"""
def create_tree3():
n3 = TreeNode(3)
n5 = TreeNode(5)
n1 = TreeNode(1)
n6 = TreeNode(6)
n2 = TreeNode(2)
n0 = TreeNode(0)
n8 = TreeNode(8)
n7 = TreeNode(7)
n4 = TreeNode(4)
n3.left, n3.right = n5, n1
n5.left, n5.right = n6, n2
n1.left, n1.right = n0, n8
n2.left, n2.right = n7, n4
return n3
I'm a newbie of Python3 and trying to fix this error in Python3, and I'm trying below, but not working.
class Solution:
def lowestCommonAncestor(self, root, p, q):
answer = []
stack = [[root, answer]]
while stack:
top = stack.pop()
(node, parent), subs = top[:2], top[2:]
if node in (None, p, q):
parent += node,
elif not subs:
stack += top, [node.right, top], [node.left, top]
else:
if subs[0] and subs[1]:
parent += node
else: # not sure how to fix the below part which is not working either
if subs[0] and not subs[1]:
parent += subs[0]
elif not subs[0] and subs[1]:
parent += subs[1]
elif not subs[0] and not subs[1]:
parent += None
return answer[0]

AVL Tree, delete mininum node (Python 3)

I want to implement a huffman coding using AVL tree. I perform some insertion, then I delete some node and everything is fine, but suddenly after another deletion the tree delete every nodes on the right side from root. It happens when the tree looks like this (the right side may be a bit different, but the height is correct):
after deleting the minim node (M) the tree has only nodes (S, R).
I think that it is because of my mistake in rotation function. Could you help me understand where is the mistake.
import random
from collections import Counter
class HuffmanCoding:
def __init__(self, data=None, space=False):
# Transform data to the form of dictionary
if data is None:
data = input("Enter string for coding: ")
self.data = Counter(data)
elif isinstance(data, str):
self.data = Counter(data)
elif isinstance(data, dict):
self.data = data
else:
raise TypeError("Type of data is incorrect!")
# Check whether to delete the space or not
if not space:
self.data.pop(' ', None)
self.my_tree = AVL_Tree()
self.root = None
for k, v in self.data.items():
self.root = self.my_tree.insert(self.root, TreeNode(k, v))
def sth(self):
n1 = self.my_tree.delete_min(self.root)
n2 = self.my_tree.delete_min(self.root)
self._create_new_internal_node(n1, n2)
def _create_new_internal_node(self, n1, n2):
new_node = HuffmanNode(n1, n2)
self.my_tree.insert(self.root, new_node)
class HuffmanNode:
def __init__(self, n1, n2):
self.character = n1.character + n2.character
self.freq = n1.freq + n2.freq
self.left = None
self.right = None
self.height = 1
class TreeNode:
def __init__(self, character, freq):
self.character = character
self.freq = freq
self.left = None
self.right = None
self.height = 1
class AVL_Tree:
def insert(self, root, n):
character = n.character
freq = n.freq
# Normal insert, like in BST
if not root:
return TreeNode(character, freq)
elif freq < root.freq:
root.left = self.insert(root.left, n)
else:
root.right = self.insert(root.right, n)
# Update the height of the ancestor node
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
# Get the balance
balance = self.get_balance(root)
# Left left
if balance > 1 and freq < root.left.freq:
return self.right_rotate(root)
# Right right
if balance < -1 and freq > root.right.freq:
return self.left_rotate(root)
# Left right
if balance > 1 and freq > root.left.freq:
root.left = self.left_rotate(root.left)
return self.right_rotate(root)
# Right left
if balance < -1 and freq < root.right.freq:
root.right = self.right_rotate(root.right)
return self.left_rotate(root)
return root
def delete_min(self, root):
if not root:
return root
# Get parent of most left node
min_parent = self.get_parent_of_min_value_node(root)
min_node = min_parent.left
# Check if most left node has a right son
if min_parent.left.right is not None:
min_parent.left = min_parent.left.right
else:
min_parent.left = None
# Update the height
root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))
balance = self.get_balance(root)
# Left left
if balance > 1 and min_parent.freq < root.left.freq:
self.right_rotate(root)
# Right right
if balance < -1 and min_parent.freq > root.right.freq:
self.left_rotate(root)
# Left right
if balance > 1 and min_parent.freq > root.left.freq:
root.left = self.left_rotate(root.left)
self.right_rotate(root)
# Right left
if balance < -1 and min_parent.freq < root.right.freq:
root.right = self.right_rotate(root.right)
self.left_rotate(root)
return min_node
def get_height(self, root):
if not root:
return 0
return root.height
def get_balance(self, root):
if not root:
return 0
return self.get_height(root.left) - self.get_height(root.right)
def right_rotate(self, z):
y = z.left
T3 = y.right
y.right = z
z.left = T3
z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
return y
def left_rotate(self, z):
y = z.right
T2 = y.left
# Perform rotation
y.left = z
z.right = T2
# Update heights
z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))
y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))
# Return the new root
return y
def pre_order(self, root):
if not root:
return
print("C: {0}, F: {1}".format(root.character, root.freq))
self.pre_order(root.left)
self.pre_order(root.right)
def in_order(self, root):
if not root:
return
self.in_order(root.left)
print("C: {0}, F: {1}".format(root.character, root.freq))
self.in_order(root.right)
def post_order(self, root):
if not root:
return
self.post_order(root.left)
self.post_order(root.right)
print("C: {0}, F: {1}".format(root.character, root.freq))
def get_min_value_node(self, root):
if root is None or root.left is None:
return root
return self.get_min_value_node(root.left)
def get_parent_of_min_value_node(self, root):
if root.left is None or root.left.left is None:
return root
return self.get_parent_of_min_value_node(root.left)
if __name__ == "__main__":
huffman_coding = HuffmanCoding("Blogoslawieni milosierni")
huffman_coding.my_tree.in_order(huffman_coding.root)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
huffman_coding.my_tree.in_order(huffman_coding.root)
print(huffman_coding.my_tree.delete_min(huffman_coding.root).character)
huffman_coding.my_tree.in_order(huffman_coding.root)
x = huffman_coding.root

Python - 'int' object is not callable

I'm writing a singly linked list that counts the number of words that get imported from a test file.
I initialized count inside of my class.
I defined the print function, that prints out each of the nodes, after they are sorted.
I defined a count class to iterate through the list and count up all the occurences of a word fed to it.
I want to pass in every node into count to count how many times it appears in my text file. When I try to do that I end up with 'int' object is not callable. Here is my code
class Linked_List:
def __init__(self):
self.head = None
self.count = 1
def print(self):
p = self.head
head = Linked_List_node(p.data)
while p is not None:
print(p.data, '-', self.count(p.data)) # This is where the error appears
p = p.next
def count(self, x):
# loop thru list for all x, if find x add 1 to count. Assign final count to that word.
with open('cleaned_test.txt', 'r') as f:
for line in f:
for word in line.split():
if word == x:
self.count += 1
def insert(self, x):
""""""
p = self.head
q = None
done = False
while not done:
if self.head == x:
done = True
elif p == None:
head = Linked_List_node(x)
q.next = head
done = True
elif x == p.data:
#head = Linked_List_node(x)
#head.counter += 1
done = True
elif x < p.data:
if self.head == p:
head = Linked_List_node(x)
head.next = p
self.head = head
done = True
else:
head = Linked_List_node(x)
head.next = p
q.next = head
done = True
q = p
if p is not None:
p = p.next
class Linked_List_node:
def __init__(self, value):
self.data = value
self.next = None
Relevant part from your code is:
class Linked_List:
def __init__(self):
# ...
self.count = 1
def count(self, x):
# ...
The self.count = 1 assignment overwrites the value of self.count for every Linked_List object that's created, so it refers to 1 instead of the method you defined on the class. Rename either the variable or the function.

Sort a unordered list class in python? solution?

I have a Unordered list class like this :
from node import Node
class UnorderedList:
"""
Unordered list
"""
def __init__(self):
self.head = None
def is_empty(self):
return self.head == None
def __len__(self):
"""
returns the length of the list O(1)
"""
return self.size()
def add(self, item):
"""
Add item to list
"""
temp = Node(item)
temp.set_next(self.head)
self.head = temp
def size(self):
"""
Return size of list
"""
current = self.head
count = 0
while current != None:
count = count + 1
current = current.get_next()
return count
def set(self, index, newdata):
"""
Set node-data in list at specific index
"""
current = self.head
previous = None
for i in range(index):
previous = current
current = current.get_next()
if current != None:
temp = Node(newdata)
temp.set_next(current)
if previous is None:
self.head = temp
else:
previous.set_next(temp)
else:
raise("index out of range")
def getIndex(self, item):
"""get the index of an item, assume the first one (head pointing to)
is 0"""
index = 0
current = self.head
found = False
while current != None:
if current.get_data() == item:
found = True
break
else:
current = current.get_next()
index += 1
if not found:
index = None
return index
def get(self, index):
"""
Returns node data based on index
"""
current = self.head
for i in range(index):
current = current.get_next()
if current != None:
return current.get_data()
else:
raise("index out of range")
def search(self, item):
"""
Returns True if item found, else return False
"""
# Här ska du returnera en bool (True/False)
# beroende på om 'item' finns i listan
current = self.head
found = False
while current != None and not found:
if current.get_data() == item:
found = True
else:
current = current.get_next()
return found
def print_list(self):
"""
Prints each item in list
"""
# Traversera listan och gör en print() på varje element
result = "["
node = self.head
if node != None:
result += str(node.data)
node = node.next
while node:
result += ", " + str(node.data)
node = node.next
result += "]"
return result
def remove(self, item):
"""
Removes item from list
"""
current = self.head
previous = None
found = False
while not found:
if current.get_data() == item:
found = True
else:
previous = current
current = current.get_next()
if previous == None:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
I want to create a class list and then i can sort the list with my "bubblesort" function.
My "bubblesort" function look like this :
def bubble_sort(items):
""" Bubble sort """
Size = items.size()
for i in range(Size):
for j in range(Size-1-i):
if items.get(j) > items.get(j+1):
tmps = items.get(j+1)
items.set(j, items.get(j+1))
items.set(j+1, tmps)
return items
Now let us create a list :
// create the list
myListTwo = UnorderedList()
// Add the elements to the list
myListTwo.add(4)
myListTwo.add(50)
myListTwo.add(6)
myListTwo.add(10)
myListTwo.add(60)
//print the list :
print(myListTwo.print_list())
[60, 10, 6, 50, 4]
In this stage all work very well but the problem is when I want to sort the list with my bubble_sort function I got this result :
// bubble_sort my list
sorte = bubble_sort(myListTwo)
//print the list
print(sorte.print_list())
[10, 10, 10, 10, 60, 10, 6, 50, 4]
Any idea?
Thanx/Georges
So, you have two issues to your implementation.
The first one is the inner code of bubble sort. Change this
tmps = items.get(j+1)
to
tmps = items.get(j)
See here for more about swapping Python Simple Swap Function
The second issue is the set method. You should remove this
temp = Node(newdata)
temp.set_next(current)
if previous is None:
self.head = temp
else:
previous.set_next(temp)
and you should write something like this
current.set_data(newData)
(I don't know how exactly you implemented the Node class)

How do I get my insert function for a binary tree to run for efficiently

It seems as if my function runs very slowly. It should only take around 10 seconds to insert 100,000 elements, but it actually takes minutes.
Note: rit_object is a private class which puts types for each parameter
from rit_object import *
class EyecuBST(rit_object):
__slots__ = ('left', 'right', 'parent',
'value', 'height', 'size', 'imbalance')
_types = ('EyecuBST', 'EyecuBST', 'EyecuBST',
int, int, int, int)
def createEyecuBST(el, parent):
""" creates a BST containing just this node, and connected
to the given parent node, which is None if this is the root.
Returns the tree node.
"""
return EyecuBST(None, None, parent, el, 0, 1, 0)
def eyecuToString(tr):
""" takes an EyecuBST tree and generates a string containing
an inorder processing of the nodes of the tree. For each
node, the string contains the following information:
value, height, size, imbalance.
Returns the string
"""
if tr == None:
return ""
else:
thisNodeStr = "Value: " + str(tr.value) + ", Height: " + \
str(tr.height) + ", Size: " + str(tr.size) + ", Imbalance: " + \
str(tr.imbalance) + "\n"
return eyecuToString(tr.left) + thisNodeStr + eyecuToString(tr.right)
def insert(tr, el):
""" function to insert an element into a binary search tree
following the rules of binary search trees.
return: an updated tree
precondition: assumed all elements unique
"""
if tr == None:
#print('inserting node root')
return createEyecuBST(el, None)
tr.height = 1
else:
if tr.value > el: #if node is greater than element
if tr.left == None:
tr.left = createEyecuBST(el, tr) #create new node
#print('inserting node left')
tr.size += 1 # size of tree + 1
return tr # return new tree
else:
insert(tr.left, el)
return tr
if tr.left == None:
if tr.right == None:
tr.height = 1
else:
tr.height = tr.right.height + 1
else:
if tr.right == None:
tr.height = tr.left.height + 1
else:
tr.height = max(tr.left.height, tr.right.height) + 1
else:
if tr.right == None:
tr.right = createEyecuBST(el, tr)
#print('inserting node right')
tr.size += 1
return tr
else:
insert(tr.right, el)
return tr
if tr.right == None:
if tr.left == None:
tr.height = 1
else:
tr.height = tr.left.height + 1
else:
if tr.left == None:
tr.height = tr.right.height + 1
else:
tr.height = max(tr.left.height, tr.right.height) + 1
def treeHeight(tr):
"""
Returns the height of the tree rooted at this node. Returns -1
if input tr is an empty tree (None).
"""
if tr is None:
return -1
else:
return tr.height
def treeSize(tr):
"""
Returns the size of the tree rooted at target node. Returns 0
is input tr is an empty tree (None)
"""
if tr is None:
return 0
else:
return tr.size
def treeImbalance(tr):
"""
Returns the imbalance of the tree rooted at target node. Returns 0
if input tr is an empty tree (None)
"""
#if tr is None:
# return 0
#else:
def findNode(tr, val):
""" finds the target node in the tree. Returns the node reference.
Returns None if the node is not in the tree.
precondtion: val is non-negative integer.
"""
# replace with your findNode function code
return None
Any suggestions? I'm trying to get the computation time to speed up. on my insert function, as well as how to keep track of the imbalance.
Can you post the whole code? Is your insert function inside of a class? I feel like you did way too much work. I need to see more of your code in order to help you.

Resources