0

I am trying to decipher this code posted a few years back over here: How to implement a binary search tree in Python?

The part that I am confused about is this section:

class Node:
    def __init__(self, val):
        self.l_child = None
        self.r_child = None
        self.data = val

def binary_insert(root, node):
    if root is None:
        root = node
    else:
        if root.data > node.data:
            if root.l_child is None:
                root.l_child = node
            else:
                binary_insert(root.l_child, node)
        else:
            if root.r_child is None:
                root.r_child = node
            else:
                binary_insert(root.r_child, node)

The class and functions are then called by doing this:

r = Node(3)
binary_insert(r, Node(7))
binary_insert(r, Node(1))
binary_insert(r, Node(5))

My question is: what happened to self.data when passed into the binary_insert function? Where did node.data and root.data come from?

Community
  • 1
  • 1
  • `self` is the instance of the `Node` class.`root` and `node` are both instances of the `Node` class as well. Think of it along the lines of.. *self=root; self.data* – Bahrom Nov 17 '16 at 17:23

2 Answers2

0

These are exactly what happened to self.data. root.data accesses the data property of root, which is an instance of the Node class.

Gormador
  • 380
  • 2
  • 15
0

Python uses self as A way for a class to reference its own attributes. Python then implicitly fills self with your class instance once a method is called of that instance.

what happened to self.data when passed into the binary_insert function?

Nothing. An instance of the Node object was passed into the binary_searach function. The Node object which was passed into the function, still has all he attributes of a Node object, including self.data.

Where did node.data and root.data come from?

As you can see, your function takes two instances of your Node object as its arguments. The two node objects that are passed into the function still have all the attributes of your original Node class. They simply use a different alias. This can be observed directly by printing out the type of the root and node parameters:

At the begining of your function we can print the types of root and node:

def binary_insert(root, node):
    print("The type of root is:", type(root))
    print("The type of node is:", type(node))
    ...

Which when called outputs:

The type of root is: <class 'Node'>
The type of node is: <class 'Node'>
The type of root is: <class 'Node'>
The type of node is: <class 'Node'>
The type of root is: <class 'Node'>
The type of node is: <class 'Node'>
The type of root is: <class 'Node'>
The type of node is: <class 'Node'>
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • Thanks this is very helpful...I think I get most of it, but in the past, I would use self.data as its own variable. So what happens if I use self.data in the code? –  Nov 17 '16 at 20:09
  • @jessibird What code are you referring to? – Christian Dean Nov 17 '16 at 22:17