Jul-13-2020, 04:08 AM
I have the following class which stores bit strings in a tree structure. The exists() method should return if the queried string is matched in the tree or not. However, I can't get my method return a boolean result. Can someone help?
class Node: def __init__(self, data): self.left = None self.right = None self.data = data def insert(self, data): if self.data and data != '': if data[0] == '0': if self.left is None: self.left = Node(data[0]) self.left.insert(data[1:]) elif data[0] == '1': if self.right is None: self.right = Node(data[0]) self.right.insert(data[1:]) def exists(self, data): if data == '': print("Match") return True if data[0] == '0': if self.left is None: print("No match") return False else: self.left.exists(data[1:]) elif data[0] == '1': if self.right is None: print("No match") return False else: self.right.exists(data[1:]) b = '1001' r = Node('root') r.insert(b) r.exists(b) r.exists('1111')