Python Forum
Tree insertion and variable referencing
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tree insertion and variable referencing
#2
because it recursive function. root.left and root.right need a node.
root.left = self._insert(root.left, val) # need the return

I believe it to be. None is not a reference variable.

you could also do this.
class Root:
	def __init__(self):
		self.root = None
		
	def insert(self, val, root='self'):
		if root == 'self':
			self.root = self.insert(val, self.root)			
		elif root is None:
			return Node(val)
		elif val <= root.value:
			root.left = self.insert(val, root.left)
		else:
			root.right = self.insert(val, root.right)
		return root
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: Tree insertion and variable referencing - by Windspar - Dec-09-2017, 11:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,605 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Dictionary Referencing nickdavis2017 1 1,634 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,392 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Amortized analysis: Insertion in a list quazirfan 1 1,381 Sep-27-2021, 02:06 AM
Last Post: deanhystad
  Referencing a fixed cell Mark17 2 2,098 Dec-17-2020, 07:14 PM
Last Post: Mark17
  Insertion sort algorithm courtesy of YouTuber Joe James Drone4four 3 2,226 Dec-07-2020, 02:11 PM
Last Post: perfringo
Sad need help in referencing a list n00bdev 2 1,868 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,264 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,711 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  insertion sort viku361 1 1,959 Apr-20-2020, 01:47 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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