Python Forum
What is wrong with code? Bin. Tree counting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is wrong with code? Bin. Tree counting
#1
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.

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)
Reply
#2
Please show full error traceback.
Hard to work without it.
Reply
#3
def delete_node(self,node):

that line has indented definitions underneath it? I never have a use for nested definitions anyway if it's even python-legal. Looks like if that def is blank, it should have a "pass" in it at least, then the others defs should be de-dented to same level. I could be wrong.
Reply
#4
Line 47: wrong indentation
Line 50: missing "("

(As a newbie you should use Python 3.x instead of Python2.x)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 484 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 597 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  Something wrong with my code FabianPruitt 5 855 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,559 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,257 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,785 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,550 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,529 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,631 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,507 Jun-09-2021, 10:01 AM
Last Post: topfox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020