educative.io

Object in Binary Tree

Where does the root come from inside the constructor of binary tree. I only know it is because of object parent class but how is it working and effective in the creation of tree
class BinaryTree(object):

def __init__(self, root):

    self.root = Node(root)

Type your question above this line.

Course: https://www.educative.io/collection/10370001/5474278013140992
Lesson: https://www.educative.io/collection/page/10370001/5474278013140992/6495994092978176

Notice in the example below
tree = BinaryTree(1)
tree.root.left = Node(2)
when we create an object of binary tree we pass a value for the root. A tree without any node does not make sense so whenever a tree is created it must have at least one node which we call root and pass the value of root to the constructor.

I agree but we dont do the same for Linked List and create a node from the scratch instead of passing an object as parameter

Correct me sir if I am wrong

You are not wrong. We don’t do this in linked list because in linked list we always append a new node at the end so an empty linked list makes sense. But in tress we add new node either to left or right so if we create an empty tree then we have to make an extra check.