Python Forum
Tree insertion and variable referencing
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tree insertion and variable referencing
#1
Hey all!
This is probably very basic for some people , but i really cant get my head around this.

The code below performs binary tree insertion. The thing i cant understand is, you'll notice in the _insert function return root. I really dont see the point of that return. However with out that return root. The tree doesn't parse. But however in the calling function insert i dont assign that returned value from _insert to anything. Yet its required. Please help me understand this?

    def insert(self, val):
        if self.root is None:
            self.root = Node(val)
        else:
            self._insert(self.root, val)

    def _insert(self, root, val):
        print(id(root))
        if root is None:
            return Node(val)
        if val <= root.val:
            root.left = self._insert(root.left, val)
        else:
            root.right = self._insert(root.right, val)
        return root
Reply
#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
#3
def insert(self, val, root='self'):[b][/b]

Clever - I didn't know you can do that. Assign self as default argument as a string.
Reply
#4
'self' is just a string. This just check it.
def insert(self, val, root='self'):
        if root == 'self':
            self.root = self.insert(val, self.root)
As long as root is not None or a Node. You can have it be anything else.
def insert(self, val, root=True):
        if root is True:
            self.root = self.insert(val, self.root)
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,470 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Dictionary Referencing nickdavis2017 1 1,566 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,328 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Amortized analysis: Insertion in a list quazirfan 1 1,312 Sep-27-2021, 02:06 AM
Last Post: deanhystad
  Referencing a fixed cell Mark17 2 2,019 Dec-17-2020, 07:14 PM
Last Post: Mark17
  Insertion sort algorithm courtesy of YouTuber Joe James Drone4four 3 2,154 Dec-07-2020, 02:11 PM
Last Post: perfringo
Sad need help in referencing a list n00bdev 2 1,809 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,178 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,587 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  insertion sort viku361 1 1,902 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