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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
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' ) |