As part of a project I have to implement a BST with the purpose of sorting through a big dictionary of keys and values. However, I'm having some problems with my code. Specifically, I get an error message because I've exceeded the maximum recursion limit.
Here is the code:
#dataclass
class Node:
key: Any = None
value: Any = None
left: Any = None
right: Any = None
def put(self, key, value):
new_node = Node(key, value)
if new_node.value < self.value:
if self.left is None:
self.left = Node(key, value, None, None)
else:
self.put(key, value)
elif new_node.value > self.value:
if self.right is None:
self.right = Node(key, value, None, None)
else:
self.put(key, value)
I understand I've created an infinite recursive function, but I dont understand why. Looking at it, it seems to me that it should not be infinite. Can someone tell me what I've done wrong?
As far as I can see, your recursive calls self.put(key, value) are identical (same arguments) to the initial call, which causes the infinite loop; think about how you want your algorithm to behave (simulating the process on a diagram helps).
For future reference, here's the fixed code (only fixes the issue mentioned in the questions; there's still room for improvements).
#dataclass
class Node:
key: Any = None
value: Any = None
left: Any = None
right: Any = None
def put(self, key, value):
new_node = Node(key, value)
if new_node.value < self.value:
if self.left is None:
self.left = Node(key, value, None, None)
else:
self.left.put(key, value)
elif new_node.value > self.value:
if self.right is None:
self.right = Node(key, value, None, None)
else:
self.right.put(key, value)
Related
The logic I tried:
def min_tree_value(self):
while self.left:
self.left = self.left.left
return self.data
Actual Python program Logic:
def min_tree_value(self):
if self.left is None:
return self.data
return self.left.min_tree_value()
The actual Python program logic is in recursion form. I tried the same logic in While loop()
I'm not sure whether my logic is correct. Do help me to figure out the incorrect logic and point where I'm Wrong.
Your logic is almost there, but not quite:
def min_tree_value(self):
node = self
while node.left:
# don't change the structure by rebinding node.left,
# but iterate the tree by moving along nodes!
node = node.left
return node.data
Note that in the original code, you never reassign self before returning its value, so you always returned the root value.
First of all, the question asks about finding the minimum element in a binary tree.
The algorithm you used, will find the minimum element in the Binary Search Tree (as the leftmost element is the minimum).
For finding minimum element in a simple Binary Tree, use the following algorithm:
# Returns the min value in a binary tree
def find_min_in_BT(root):
if root is None:
return float('inf')
res = root.data
lres = find_min_in_BT(root.leftChild)
rres = find_min_in_BT(root.rightChild)
if lres < res:
res = lres
if rres < res:
res = rres
return res
Additions to the answer after OP changed the question:
The logic for the algorithm you tried is correct, with a small correction in the implementation: self = self.data. Both of them find the leftmost element.
I have also tested both the functions which return the same output:
class Node:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.data = data
def findval(self, lkpval):
if lkpval < self.data:
if self.left is None:
return str(lkpval)+" Not Found"
return self.left.findval(lkpval)
elif lkpval > self.data:
if self.right is None:
return str(lkpval)+" Not Found"
return self.right.findval(lkpval)
else:
print(str(self.data) + ' is found')
def PrintTree(self):
if self.left:
self.left.PrintTree()
print( self.data),
if self.right:
self.right.PrintTree()
def min_tree_value_original(self):
if self.left is None:
return self.data
return self.left.min_tree_value_original()
def min_tree_value_custom(self):
while self.left:
self = self.left
return self.data
root = Node(12)
root.insert(6)
root.insert(14)
root.insert(3)
root.insert(3)
root.insert(1)
root.insert(0)
root.insert(-1)
root.insert(-2)
print(root.min_tree_value_original())
print(root.min_tree_value_custom())
Output:
-2
-2
Here -2 is the smallest and the leftmost element in the BST.
So, I am trying to define a function which would invert a binary tree, and when I run the below code, and then execute a_node.binInversion(), I get the error which says, "NameError: name 'binInversion' is not defined". Why is that error happening?
The code I'm executing is as below:
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def insert_left(self, value):
if self.left == None:
self.left = BinaryTree(value)
else:
new_node = BinaryTree(value)
new_node.left = self.left
self.left = new_node
def insert_right(self, value):
if self.right == None:
self.right = BinaryTree(value)
else:
new_node = BinaryTree(value)
new_node.right = self.right
self.right = new_node
def binInversion(self):
if self.left is None and self.right is None:
return 0
else:
binInversion(self.left)
binInversion(self.right)
self.left, self.right = self.right, self.left
a_node = BinaryTree('a')
a_node.insert_left('b')
a_node.insert_right('c')
b_node = a_node.left
b_node.insert_right('d')
c_node = a_node.right
c_node.insert_left('e')
c_node.insert_right('f')
d_node = b_node.right
e_node = c_node.left
f_node = c_node.right
a_node.binInversion()
Also, I know that there are high chances of binInversion() function not working as expected. Please DO NOT disclose the solution for inverting a binary tree, as I want to give it a shot on my own before looking at the solution.
a few bugs here:
binInversion is not a function, it is a class method, remember to call self.binInversion() instead of binInversion()
after the above fix, there will still be an error that says something along the lines of binInversion was given too many arguments. Your method of binInversion takes no arguments, try something like def binInversion(self, node) to pass in another argument or node.
I'm trying to implement insert function of bst but my recursive approach only inserts the first value. My approach was to traverse until the empty child and set that node to new value.
class BSTNode(object):
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(self, val):
if self.root is None:
self.root = BSTNode(val)
else:
self.insertNode(self.root,val)
def insertNode(self,node, val):
if node is None:
node = BSTNode(val)
return
elif val <= node.val:
node.left = self.insertNode(node.left, val)
elif val > node.val:
node.right = self.insertNode(node.right, val)
bst = new BST()
bst.insert(5)
bst.insert(10)
You're not returning BSTNode in insertNode (so you create a node but return None). You should do this instead:
if node is None:
return BSTNode(val)
So the full method becomes:
def insertNode(self, node, val):
if node is None:
return BSTNode(val)
elif val <= node.val:
node.left = self.insertNode(node.left, val)
elif val > node.val:
node.right = self.insertNode(node.right, val)
return node
It might be helpful to first create a node class... it's easier to think of a node alone than it is to think about a Tree.
class Node:
def __init__(self,value):
self.value = value
self.left = None
self.right = None
def insert_value(self,value):
# there are 3 cases
if not self.value: # in one case this node has no value
self.value = value
elif value < self.value: # if value is less than this nodes value
# then insert it to the left
if not self.left:
self.left = Node(value)
else:
self.left.insert_value(value)
else: # otherwise if value is greater than or equal to this nodes value
# then insert it to the right
if not self.right:
self.right = Node(value)
else:
self.right.insert_value(value)
Once you have this Node class the tree implementation becomes trivial.
class BST:
def __init__(self,root_value=None):
self.root = Node(root_value)
def insert_value(self,value):
self.root.insert_value(value)
bst = BST()
bst.insert_value(5)
bst.insert_value(10)
print(bst.root)
Now you certainly do not have to solve this in this fashion ... but it makes it easier to reason about (at least for me).
I'm somewhat new to Python and need help with an issue I'm facing. I'm trying to make a binary search tree. I've written a code and it works, but won't show any result (printed values). I can't figure out what the issue might be. Here is the entire code:
class Node:
def __init__(self, value):
self.value = value
self.left_child = None
self.right_child = None
class binary_search_tree:
def __init__(self):
self.root_node = None
def insert(self, value, curr_node):
if self.root_node == None:
self.root_node == node(value)
elif self.root_node < value:
if self.right_child == None:
self.right.child = value
else:
curr_node == self.right_child
if curr_node < value:
curr_node.right_child = node(value)
elif curr_node > value:
curr_node.left_child = node(value)
else:
print("Error! Value Already Exists!")
elif self.root_node > value:
if self.left_child == None:
self.left.child = value
else:
curr_node == self.left_child
if curr_node < value:
curr_node.right_child = node(value)
elif curr_node > value:
curr_node.left_child = node(value)
else:
print("Error! Value Already Exists!")
else:
print("Error! Value Already Exists!")
def fill_Tree(tree, num_elems = 100, max_int = 1000):
from random import randint
for x in range (num, elems):
curr_elem = randint(0, max_int)
tree.insert(curr_elem)
return tree
I have made a class Node to handle the nodes and a function insert that helps to insert the values. It check for the root node. If its there, it moves onto the leaf based on the values. If not, it adds the value as the root. The program keeps on checking for values and nodes and their differences (less than, greater than etc), just the ways a tree is supposed to function. The program executes, but nothing shows up. Not sure what I'm doing wrong though.
Any sort of help would be appreciated!
Thanks.
If that is your entire code, and if the input and execution is perfect, it would not show any result, because you are not printing any result.
You don't have an apparent main function to create an object of the class binar_search_tree.
The print statements that you have are only when there is an error. If everything works perfectly, your code doesn't print anything
You would need a method that can display the the tree
Currently, your insertion method is assigning values to the left or right children of the root, not traveling to a depth beyond those two nodes, should a value smaller than the left child or greater than the right child be found. Instead, create one class to store the value, left, and right child, along with the necessary insertion method. To determine if a value exists in the tree, it is cleaner to utilize __getitem__ with recursion:
def check_val(f):
def wrapper(cls, _val):
if _val in cls.__class__.seen:
raise ValueError(f"'{_val}' already in '{cls.__class__.__name__}'")
return f(cls, _val)
return wrapper
class Tree:
seen = []
def __init__(self, value=None):
self.left = None
self.value = value
self.right = None
def __lt__(self, _node):
return self.value < getattr(_node, 'value', _node)
#check_val
def insert_val(self, _val):
if self.value is None:
self.value = _val
self.__class__.seen.append(_val)
else:
if _val < self.value:
if self.left is None:
self.left = Tree(_val)
Tree.seen.append(_val)
else:
self.left.insert_val(_val)
else:
if self.right is None:
self.right = Tree(_val)
Tree.seen.append(_val)
else:
self.right.insert_val(_val)
def __getitem__(self, val):
if self.value == val:
return True
if val < self.value:
return getattr(self.left, '__getitem__', lambda _:False)(val)
return getattr(self.right, '__getitem__', lambda _:False)(val)
#classmethod
def load_tree(cls, size = 10):
_t = cls()
import random
for _ in range(size):
_t.insert_val(random.randint(1, 100))
return _t
To run:
t = Tree.load_tree()
print(t.__class__.seen)
#[82, 94, 33, 59, 73, 72, 96, 14, 58, 67]
for i in t.__class__.seen:
assert t[i]
print('all cases passed')
Output:
'all cases passed'
I'm trying to write a min method for a binary search tree, but for some reason the output is always None!
I understand that I always have to go left in order to find the minimum and that's what my code is doing but I don't understand why I always get None as an output!
class Tree_node():
def __init__(self, key, val):
self.key = key
self.val = val
self.left = None
self.right = None
class BSearch_tree():
def __init__(self):
self.root=None
def insert(self, key, val):
if self.root==None:
self.root=Tree_node(key,val)
elif key == self.root.key:
self.val=val
elif key < self.root.key:
left = BSearch_tree()
left.root = self.root.left
self.root.left=BSearch_tree.insert(left,key,val)
elif key > self.root.key:
right = BSearch_tree()
right.root=self.root.right
self.root.right=BSearch_tree.insert(right,key,val)
def min(self):
if self.root==None:
return None
current= self
while current.root.left!=None:
current=current.root.left
return current