0

What is wrong with my insert function? I'm passing along the tr and the element el that I wish to insert, but I keep getting errors...

def insert( tr,el ):
    """ Inserts an element into a BST -- returns an updated tree """
    if tr == None:
        return createEyecuBST( el,None )
    else:
        if el > tr.value:
            tr.left = createEyecuBST( el,tr )
        else:
            tr.right = createEyecuBST( el,tr )
        return EyecuBST( tr.left,tr.right,tr)

Thanks in advance.

ERROR:


ValueError: Not expected BST with 2 elements

It's a test function that basically tells me whether or not what I'm putting in is what I want out.

4
  • your else if else does not seem to make much sense, what is supposed to happen? And what error are you getting? Commented Nov 15, 2014 at 22:08
  • else (if tr != None): if the el (an int) is greater than the parent of the tr, then create a new tree on the left and make the children = None, and if el is less than the parent of the tr then assign it to the right child Commented Nov 15, 2014 at 22:19
  • Perhaps that doesn't make sense though, because then I will never make it down to the child to add another level... I honestly don't get this at all Commented Nov 15, 2014 at 22:23
  • on a side note I would write your functions differently, you have a lot going on that you don't need, pastebin.com/TEQ7YquS Commented Nov 15, 2014 at 22:29

2 Answers 2

1

So, the way insertion in a binary tree usually works is that you start at the root node, and then decide which side, i.e. which subtree, you want to insert your element. Once you have made that decision, you are recursively inserting the element into that subtree, treating its root node as the new root node.

However, what you are doing in your function is that instead of going down towards the tree’s leaves, you are just creating a new subtree with the new value immediately (and generally mess up the existing tree).

Ideally, an binary tree insert should look like this:

def insert (tree, value):
    if not tree:
        # The subtree we entered doesn’t actually exist. So create a
        # new tree with no left or right child.
        return Node(value, None, None)

    # Otherwise, the subtree does exist, so let’s see where we have
    # to insert the value
    if value < tree.value:
        # Insert the value in the left subtree
        tree.left = insert(tree.left, value)
    else:
        # Insert the value in the right subtree
        tree.right = insert(tree.right, value)

    # Since you want to return the changed tree, and since we expect
    # that in our recursive calls, return this subtree (where the
    # insertion has happened by now!).
    return tree

Note, that this modifies the existing tree. It’s also possible that you treat a tree as an immutable state, where inserting an element creates a completely new tree without touching the old one. Since you are using createEyecuBST all the time, it is possible that this was your original intention.

To do that, you want to always return a newly created subtree representing the changed state of that subtree. It looks like this:

def insert (tree, value):
    if tree is None:
        # As before, if the subtree does not exist, create a new one
        return Node(value, None, None)
    if value < tree.value:
        # Insert in the left subtree, so re-build the left subtree and
        # return the new subtree at this level
        return Node(tree.value, insert(tree.left, value), tree.right)
    elif value > tree.value:
        # Insert in the right subtree and rebuild it
        return Node(tree.value, tree.left, insert(tree.right, value))

    # Final case is that `tree.value == value`; in that case, we don’t
    # need to change anything
    return tree

Note: Since I didn’t know what’s the difference in your createEyecuBST function and the EyecuBST type is, I’m just using a type Node here which constructer accepts the value as the first parameter, and then the left and right subtree as the second and third.

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

Comments

0

Since the binary doesn't have the need to balance out anything , you can write as simple logic as possible while traversing at each step .

--> Compare with root value.

--> Is it less than root then go to left node.

--> Not greater than root , then go to right node.

--> Node exists ? Make it new root and repeat , else add the new node with the value

def insert(self, val):
    treeNode = Node(val)
    placed = 0
    tmp = self.root
    if not self.root:
        self.root = treeNode
    else:
        while(not placed):
            if val<tmp.info:
                if not tmp.left:
                    tmp.left = treeNode
                    placed = 1
                else:
                    tmp = tmp.left
            else:
                if not tmp.right:
                    tmp.right = treeNode
                    placed = 1
                else:
                    tmp = tmp.right
    return

You can also make the function recursive , but it shouldn't return anything. It will just attach the node in the innermost call .

Comments

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.