Nov-07-2017, 09:17 PM
Hallowings,
I am having following code giving me errors. I see everything is all right and don't know why it does. Says smt. about formating, butt formatting is right!
Thonx.
I am having following code giving me errors. I see everything is all right and don't know why it does. Says smt. about formating, butt formatting is right!
Thonx.
class node:
def __init__(self,value=None):
self.value=value
self.left_child=None
self.right_child=None
self.parent=None # pointer to parent node in tree
self.height = None
def set_parent(self, parent_node):
self.parent = parent_node
self.height = parent_node.height + 1
class binary_search_tree:
def __init__(self):
self.root=None
def insert(self,value):
if self.root==None:
self.root=node(value)
self.root.height = 0
else:
self._insert(value,self.root)
def _insert(self,value,cur_node):
if value<cur_node.value:
if cur_node.left_child==None:
cur_node.left_child=node(value)
cur_node.left_child.set_parent(cur_node) # set parent
else:
self._insert(value,cur_node.left_child)
elif value>cur_node.value:
if cur_node.right_child==None:
cur_node.right_child=node(value)
cur_node.right_child.set_parent(cur_node) # set parent
else:
self._insert(value,cur_node.right_child)
else:
print "Value already in tree!"
def print_tree(self, max_height = float('Inf')):
if self.root!=None:
self._print_tree(self.root, max_height)
def _print_tree(self, cur_node, max_height, accumulated_sum = 0):
if cur_node!=None and cur_node.height < max_height:
self._print_tree(cur_node.left_child, max_height)
value = cur_node.value
print str(cur_node.value )
self._print_tree(cur_node.right_child, max_height)
print "sum of values on all nodes with max high %s is %d" % (max_height, accumulated_sum))
return (accumulated_sum + value)
def height(self):
if self.root!=None:
return self._height(self.root,0)
else:
return 0
def _height(self,cur_node,cur_height):
if cur_node==None: return cur_height
left_height=self._height(cur_node.left_child,cur_height+1)
right_height=self._height(cur_node.right_child,cur_height+1)
return max(left_height,right_height)
def find(self,value):
if self.root!=None:
return self._find(value,self.root)
else:
return None
def _find(self,value,cur_node):
if value==cur_node.value:
return cur_node
elif value<cur_node.value and cur_node.left_child!=None:
return self._find(value,cur_node.left_child)
elif value>cur_node.value and cur_node.right_child!=None:
return self._find(value,cur_node.right_child)
def delete_value(self,value):
return self.delete_node(self.find(value))
def delete_node(self,node):
# returns the node with min value in tree rooted at input node
def min_value_node(n):
current=n
while current.left_child!=None:
current=current.left_child
return current
# returns the number of children for the specified node
def num_children(n):
num_children=0
if n.left_child!=None: num_children+=1
if n.right_child!=None: num_children+=1
return num_children
# get the parent of the node to be deleted
node_parent=node.parent
# get the number of children of the node to be deleted
node_children=num_children(node)
# break operation into different cases based on the
# structure of the tree & node to be deleted
# CASE 1 (node has no children)
if node_children==0:
# remove reference to the node from the parent
if node_parent.left_child==node:
node_parent.left_child=None
else:
node_parent.right_child=None
# CASE 2 (node has a single child)
if node_children==1:
# get the single child node
if node.left_child!=None:
child=node.left_child
else:
child=node.right_child
# replace the node to be deleted with its child
if node_parent.left_child==node:
node_parent.left_child=child
else:
node_parent.right_child=child
# correct the parent pointer in node
child.set_parent(node_parent)
# CASE 3 (node has two children)
if node_children==2:
# get the inorder successor of the deleted node
successor=min_value_node(node.right_child)
# copy the inorder successor's value to the node formerly
# holding the value we wished to delete
node.value=successor.value
# delete the inorder successor now that it's value was
# copied into the other node
self.delete_node(successor)
def search(self,value):
if self.root!=None:
return self._search(value,self.root)
else:
return False
def _search(self,value,cur_node):
if value==cur_node.value:
return True
elif value<cur_node.value and cur_node.left_child!=None:
return self._search(value,cur_node.left_child)
elif value>cur_node.value and cur_node.right_child!=None:
return self._search(value,cur_node.right_child)
return False
tree = binary_search_tree()
for i in range(0, 10):
tree.insert(i)
tree.print_tree(2)
