0
我遇到了BST問題;我寫了一個代表BST的班級,但無論是否在BST中,我都不能返回True
或False
。任何想法爲什麼它總是返回None
?當我問到根時,它只給了我正確的答案。BST類,找到值
class DrzewoBST:
def __init__(self, value = None):
self.val = value
self.left = None
self.right = None
def __str__(self):
return "[%s, %s, %s]" % (self.left, str(self.val), self.right)
def wstawianie(self, wartosc):
if self is None:
self = DrzewoBST(wartosc)
if wartosc < self.val:
if self.left is None:
self.left = DrzewoBST(wartosc)
else:
self.left.wstawianie(wartosc)
elif wartosc > self.val:
if self.right is None:
self.right = DrzewoBST(wartosc)
else:
self.right.wstawianie(wartosc)
def wyszukiwanie(self, liczba):
print(self.val)
if self is None:
return False
if int(self.val) == liczba:
return True
if liczba < self.val:
self.left.wyszukiwanie(liczba)
else:
self.right.wyszukiwanie(liczba)
t = DrzewoBST(2)
t.wstawianie(3)
t.wstawianie(3)
print(t.wyszukiwanie(3))
,當我嘗試t.wyszukiwanie(2)
(這給了我True
),但是當我嘗試t.wyszukiwanie(3)
我False
僅適用;我應該得到True
,因爲3在BST。
的可能的複製[Python函數返回無用於非的emptyList](http://stackoverflow.com/questions/34315989/python-function-returns-none-for-non-emptylist) – Prune