class Node:
def __init__(self,value = None):
self.value = value
self.left = None
self.right = None
class binary_search_tree:
def __init__(self):
self.root = None
def insert(self,current,value):
if current == None:
current = Node(value)
elif value < current.value:
self.insert(current.left,value)
elif value > current.value:
self.insert(current.right,value)
tree = binary_search_tree()
for i in (12,5,18,3,4,6):
tree.insert(tree.root,i)
The insert method for my binary search tree is not working can someone help me out.
(Post is mostly code, please add more details.)
Related
I'm writing a function to check if a binary tree satisfies the Height-Balance Property. This is my code but I'm having trouble calling the height function for left and right from my given LinkedBinaryTree class. The main thing that's confusing me is that the nested function takes the root a parameter but height() doesn't. For reference, bin_tree is a LinkedBinaryTree() not a node. Thank you in advance for any help!
My Code
from LinkedBinaryTree import LinkedBinaryTree
def is_height_balanced(bin_tree):
if bin_tree.root is None:
return True
left = bin_tree.height()
right = bin_tree.height()
if abs(left - right) <= 1:
if is_height_balanced(bin_tree.root.left) is True and is_height_balanced(bin_tree.root.right) is True:
return True
return False
Portion of LinkedBinaryTree class
class LinkedBinaryTree:
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.parent = None
self.left = left
if (self.left is not None):
self.left.parent = self
self.right = right
if (self.right is not None):
self.right.parent = self
def __init__(self, root=None):
self.root = root
self.size = self.count_nodes()
# assuming count_nodes() and is_empty() works as expected
def height(self):
def subtree_height(root):
if (root.left is None and root.right is None):
return 0
elif (root.left is None):
return 1 + subtree_height(root.right)
elif (root.right is None):
return 1 + subtree_height(root.left)
else:
left_height = subtree_height(root.left)
right_height = subtree_height(root.right)
return 1 + max(left_height, right_height)
if(self.is_empty()):
raise Exception("Tree is empty")
return subtree_height(self.root)
I am trying to write a code which the output returns either True or False, if the value exists within the Binary Tree.
Here is my attempt:
Defining a class called Node:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
Defining a class called BinaryTree + LOOKUP function:
class BinaryTree:
def __init__(self, rootdata):
self.root = Node(rootdata)
def LOOKUP(self, lookupval):
if lookupval < self.data:
if (self.left == None):
self.left.LOOKUP(lookupval)
return False
elif lookupval > self.data:
if (self.right == None):
self.right.LOOKUP(lookupval)
return False
else:
return True
The rest, inputting values into the Binary Tree:
Tree = BinaryTree(24)
Tree.root.left = Node(11)
Tree.root.left.left = Node(199)
Tree.root.left.right = Node(167)
Tree.root.right = Node(2)
Tree.root.right.right = Node(8)
print(Tree.LOOKUP(11))
print(Tree.LOOKUP(13))
However, I keep getting the error 'BinaryTree' object has no attribute 'data'..
I understand there will be some errors in the definition of the function LOOKUP,
but is there any chance that I could keep this format and still return the outputs:
True
False
Thank you,
The issues in your code are:
you're trying to reference self.right (should be self.root.right instead - because we're not on the Node instance)
the nested if check are wrong. You should recursively check the left/right tree if NOT None or return False there.
Do something like this:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinaryTree:
def __init__(self, rootdata):
self.root = Node(rootdata)
def LOOKUP(self, lookupval):
if lookupval < self.root.data:
if self.root.left:
return BinaryTree(self.root.left.data).LOOKUP(lookupval) # recursively check the left tree
else:
return False # can't go any further- so return false
elif lookupval > self.root.data:
if self.root.right:
return BinaryTree(self.root.right.data).LOOKUP(lookupval)
else:
return False
else:
return True
Tree = BinaryTree(24)
Tree.root.left = Node(11)
Tree.root.left.left = Node(199)
Tree.root.left.right = Node(167)
Tree.root.right = Node(2)
Tree.root.right.right = Node(8)
print(Tree.LOOKUP(11))
print(Tree.LOOKUP(13))
output:
True
False
class Node:
def __init__(self,data=None):
self.data=data
self.left_child=None
self.right_child=None
self.parent=None
self.root = None
class BinarySearchTree:
def __init__(self):
self.root=None
def add(self, data):
if self.root == None:
self.root = Node(data)
else:
self.add_helper(data, self.root)
def add_helper(self, data, cur_node):
if data < cur_node.data:
if cur_node.left_child == None:
cur_node.left_child = Node(data)
cur_node.left_child.parent = cur_node # set parent
else:
self.add_helper(data, cur_node.left_child)
elif data > cur_node.data:
if cur_node.right_child == None:
cur_node.right_child = Node(data)
cur_node.right_child.parent = cur_node # set parent
else:
self.add_helper(data, cur_node.right_child)
else:
print("data already in tree!")
def __len__(self):
if self.root is None:
return 0
else:
return (self.__len__(self.left_child) + 1 +self. __len__(self.right_child))
So i am trying to return the length of the binary search tree list, so i tried using the len method for my binary search tree class. However, this is not working correctly. I need it to be able to not take in any parameters, and just return an integer for the length of the binary search tree list. What am i missing and what am i doing wrong here?
You will need a helper function that takes a Node argument. Then do the recursion on the left and right of the node.
def __len__(self):
return self.tree_len(self.root)
def tree_len(self, node):
if node is None:
return 0
else:
return 1 + max(self.tree_len(node.right_child), self.tree_len(node.left_child))
I am trying to implement Binary Tree, I think the tree implementation is working fine as I am able to find my elements using if 8 in tree. But I when I am trying to perform inOrder Traversal I am running into Recursion Error.
class BinaryNode:
def init(self,value):
self.value = value
self.left = None
self.right = None
class BinaryTree:
def __init__(self):
self.root = None
def add(self, value):
if self.root == None:
self.root = BinaryNode(value)
else:
current = self.root
while 1:
if value <= current.value:
if current.left:
current = current.left
else:
current.left = BinaryNode(value)
break
elif value > current.value:
if current.right:
current = current.right
else:
current.right = BinaryNode(value)
break
else:
break
def __contains__(self, target):
node = self.root
while node is not None:
if node is not None:
if target < node.value:
node = node.left
elif target > node.value:
node = node.right
else:
return True
return False
def inorder(self,node):
node = self.root
if node is not None:
self.inorder(node.left)
print (node.value)
self.inorder(node.right)
</code>
tree = BinaryTree()
arr = [8,3,1,6]
for i in arr:
tree.add(i)
print (tree.root.value)
print ('Inorder Traversal')
tree.inorder(tree.root)
Error I am getting is "RecursionError: maximum recursion depth exceeded" :( I am trying to check that node is not none before the call, not sure where I am going wrong
I am a trying to implement a binary tree using two classes - Node and Binary Tree. When I am inserting the nodes (left or right), I am using the methods insert_left_node and insert_right_node which are class BinaryTree's methods, but I am also using class Node to create a node. After every node insertion, the current object is returned.
Now, how do I call class BinaryTree's insertion methods using the returned object - current. E.g. In the second last line of the code, statement n3 = n1.insert_left_node(33) fails with AttributeError: 'Node' object has no attribute 'insert_left_node'
I need an alternative way to achieve this.
Code:
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinaryTree(object):
def __init__(self, root=None):
self.root = Node(root)
def insert_left_node(self, data):
if not self.root:
self.root = Node(data)
else:
current = self.root
while True:
if current.left:
current = current.left
else:
current.left = Node(data)
break
return current
def insert_right_node(self, data):
if not self.root:
self.root = Node(data)
else:
current = self.root
while True:
if current.right:
current = current.right
else:
current.right = Node(data)
break
return current
if __name__ == '__main__':
r = BinaryTree(34) # root
n1 = r.insert_left_node(22)
n2 = r.insert_right_node(45)
n3 = n1.insert_left_node(33) # Fails
print n3
Your request literally doesn't make any sense. To achieve what you want you should just add the needed methods to the class you want to use. Try something similar to the following:
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert_left_node(self, data):
self.left = Node(data)
def insert_right_node(self, data):
self.right = Node(data)