1

I need to implement a Binary Search Tree class as homework but I struggle making the insert function. I have looked through Google a lot to find some solutions or possibilities on how to do it but none of them has used a key and value (mostly just value) or if they used a key aswell, they had tons of seperate functions which I am not allowed to do I think.

So the pre-built is simply that:

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

class BinarySearchTree:
    def __init__(self):
        self.root = None
        self.size = 0

    def __len__(self):
        return self.size

    def insert(self, key, value):
        pass

    def remove(self, key):
        pass

    def find(self, key):
        pass

Now the thing is, if I want to check for example whether the value is smaller or bigger than a current Node to put it either right or left, I get Errors such as "root is not defined" or "root.right" has no such attribute etc... And I guess that makes sense because self.root is declared as None.

But how do I actually fix it now to make the insert function work?

I am a little confused by this task as it uses key + value, so I need to insert the value bound to the specific key and in case the key already existed, overwrite its value.

3
  • what is self.root for? Commented May 25, 2017 at 16:14
  • To be honest, I don't know myself... the root is probably the very first of the tree. Commented May 26, 2017 at 1:04
  • ohh i think i got it. the root is just a node. have your insert function check if root is None and if it is, assign a new node to it Commented May 26, 2017 at 1:53

2 Answers 2

1

its 5 in the morning so this might be all wrong, but here goes:
the key is what we are sorting by, the values aren't interesting
your insert function should probably look something like this:

def insert(self, key, value):
        if self.root = None:
            self.root = Node(key,value)
            return
        #regular binary tree traversal (comparing the key) to find where to insert, lets assume we need to insert on the left
        parent.left = Node(key,value)

can you figure it out from here or would you like more direction

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

5 Comments

I also had as starting idea to check if root is None, then assing new Node(key, value) to it. And after that I thought I would need to check if the value is bigger or smaller than something, so it gets either to the left or to the right. But I don't know how/what to check because the key-value part makes me confused.
how about adding the wording of the assignment? maybe we can help understand what is required of you
It's not in English though.. I try my best to translate it: "this semantic should do the following: tree.insert(key, value) : Put in the given value under the specific key. If the key already existed, replace its value with the new value. Otherwise a new Node shall be created in a way that the Binary Search Tree conditions are still properly. tree.remove(key): Remove the node under the specific key, while the BST conidtions are still properly. If the key doesn't exist, you hand in KeyError Exception. found=tree.find(key): Return the node of the specific key or None, if it doesn't exist.
And before it says something about "This is the definition of Node and the basic definition of SearchTree" (insert the code from above here). Implement a fully working implementation of SearchTree. And the parts to be edited are the ones that have 'pass' in the code for now.
ohh, i think its a regular binary tree with the "keys", the values are not computationally interesting
0

You didn't specify, but I'm guessing the point of the keys is to determine if a particular key is already in the tree, and if so, replace the related node's value in O(1) runtime complexity.

So when you're inserting a node, you will first check the dictionary for the key (you will initialize an empty dictionary yourself in __init__). If it already is there, then you simply just need to replace the value of the node for that particular key. Otherwise, you add the new node the same way that you would in any BST, and also remember to update your dictionary to map the key to it's node.

2 Comments

i think the "key" for insert might be 'left' or 'right'
That's the thing, this is pre-built and I have everything I need apparently already in it, I am only allowed to change the functions that are for now just a pass inside.

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.