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


Messages In This Thread
Tree insertion and variable referencing - by hshivaraj - Dec-09-2017, 08:58 PM

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