简体   繁体   中英

Instance variable returning None when value is present in Binary search tree

I'm learning my data structures thoroughly again and I've got to implementing the Binary search Tree. I have a Node class with relevant fields and a Tree class which implements the tree. I have two methods - insert and get insert works as expected but the get method returns None when accessing the value of any node other than the root.

Walking through the code using pycharm's debugger shows me that the particular node is reached, but the value returned in still None.

This is the Node class

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

And here is the entire BinarySearchTree class

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

    def insert(self, key, value):
        self.size += 1
        if self.root is None:
            self.root = Node(key, value)
        else:
            self.insert_with_node(self.root, key, value)


    def insert_with_node(self, node, key, value):
        if node.left is None and node.right is None:
            if node.key > key:
                node.left = Node(key, value)
            else:
                node.right = Node(key, value)
        else:
            if node.key > key:
                if node.left is not None:
                    self.insert_with_node(node.left, key, value)
                else:
                    node.left = Node(key, value)
            else:
                if node.right is not None:
                    self.insert_with_node(node.right, key, value)
                else:
                    node.right = Node(key, value)

    def get(self, key):
        if self.root.key == key:
            return self.root.value
        else:
            return self.get_with_node(self.root, key)


    def get_with_node(self, node, key):
        if node.key > key:
            if node.left.key == key:
                return node.left.value
            else:
                self.get_with_node(node.left, key)
        else:
            if node.right.key == key:
                return node.right.value
            else:
                self.get_with_node(node.right, key)

EDIT: Adding testing code and console output:

def main():
    t = BinarySearchTree()
    t.insert(1, "g")
    t.insert(5, "qa")
    t.insert(3, "ac")
    t.insert(6, "cva")
    t.insert(12, "as")
    t.printTree(t.root)
    print(t.get(3))

Output :

C:\Python34\python.exe   C:/Users/home/PycharmProjects/DSandA/BinarySearchTree.py
1 : g
3 : ac
5 : qa
6 : cva
12 : as
None
def get_with_node(self, node, key):
    if node.key > key:
        if node.left.key == key:
            return node.left.value
        else:
            self.get_with_node(node.left, key)
    else:
        if node.right.key == key:
            return node.right.value
        else:
            self.get_with_node(node.right, key)

When a function recursively calls itself, you still need to use the return statement if you want anything to be returned.

def get_with_node(self, node, key):
    if node.key > key:
        if node.left.key == key:
            return node.left.value
        else:
            return self.get_with_node(node.left, key)
    else:
        if node.right.key == key:
            return node.right.value
        else:
            return self.get_with_node(node.right, key)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM