1

I am implementing BST in python and having some trouble on insertion function.

class Node:
    def __init__(self, val):
        self.data = val
        self.Leftchild = self.Rightchild = None


class Tree:
    def __init__(self):
        self.root = None

    def insert(self, val):
        if self.root is None:
            self.root = Node(val)
            return self.root
        else:
            if self.root.data <= val:
                self.root.Rightchild = self.insert(self.root.Rightchild, val)
            else:
                self.root.Leftchild = self.insert(self.root.Leftchild, val)
            return self.root
if __name__ == '__main__':
    tree = Tree()
    for i in range(10):
        tree.insert(random.randint(0,100))

I got TypeError on recursive.

TypeError: insert() takes 2 positional arguments but 3 were given

Isn't self.root.Rightchild or self.root.Leftchild considered same as self? If my thought is wrong, how can I implement recursive insertion function in this case? Thanks in advance!

2
  • 2
    No, they aren't considered same as self. If you want to call that method on the instance, you need e.g. self.root.Rightchild.insert(val). Commented Aug 12, 2017 at 16:20
  • Or let the insert take 3 arguments... something like (self, obj, val) and change the logic accordingly Commented Aug 12, 2017 at 16:25

3 Answers 3

4
  1. You should have insert take another argument, root, and do your operations on that. You'll need to modify the recursive logic too. Have insert work exclusively with the Node data.

  2. You should handle cases where an item already exists. You don't want duplicates put into the tree.

  3. In the recursive case, you are calling insert on the wrong object.

Also, the two are not the same. self refers to the current Tree object, and self.root refers to the current Tree's Node object, and so on.


This is how you'd modify your function:

def insert(self, root, val):
    if root is None:
        return Node(val)

    else:
        if root.data <= val:
            root.Rightchild = self.insert(root.Rightchild, val)
        else:
            root.Leftchild = self.insert(root.Leftchild, val)

        return root
Sign up to request clarification or add additional context in comments.

3 Comments

Then, don't I need to have another instance of Node()? to use as root? Is there any method that I have only two parameters, def insert(self, val)?
@jaykodeveloper You can, but you would have to perform recursion on Tree. Right now, your code says - a tree is composed of a node and sub nodes. If you change your code to - a tree is composed of sub trees - then you can do that. Think about it.
I see, that makes sense! Thank you!
2

Try this one if you need class and its instance

import random
class Node:
    def __init__(self, val):
        self.data = val
        self.Leftchild = self.Rightchild = None


class Tree:

    def insert(self,root, val):
        if root is None:
            root = Node(val)
            return root
        else:
            if root.data <= val:
                root.Rightchild = self.insert(root.Rightchild, val)
            else:
                root.Leftchild = self.insert(root.Leftchild, val)
            return root

    def inorder(self, root):
        if root:
            self.inorder(root.Leftchild)
            print root.data,
            self.inorder(root.Rightchild)


if __name__ == '__main__':
    tree = Tree()
    root = None
    for i in range(10):
        root = tree.insert(root, random.randint(0,100))
    tree.inorder(root)

1 Comment

Thank you, it helps! But I think it is basically same as @coldspeed 's one.
1

Try this..

class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None


def inorder(root):
    if root:
        inorder(root.left)
        arr.append(root.data)
        print root.data,
        inorder(root.right)


def insert(root, data):
    node = Node(data)
    if root is None:
        root = node
    elif root.data >= data:
        if root.left is None:
            root.left = node
        else:
            insert(root.left, data)
    else:
        if root.right is None:
            root.right = node
        else:
            insert(root.right, data)
if __name__ == '__main__':
    root = Node(50)
    insert(root, 30)
    insert(root, 20)
    insert(root, 40)
    insert(root, 70)
    insert(root, 60)
    insert(root, 80)

    inorder(root)

2 Comments

Thank you, but I want to have Tree class and its instance.
try the next one then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.