二叉排序树,又叫二叉搜索树,二叉查找树。
二叉搜索树中比较复杂的就是删除操作,我的删除操作写的最笨了,代码比较幼稚,但是对于初学者很容易看懂。看懂了之后再进行优化。因为我也是初学,网上大神的方法看不懂。。。就自己按照操作写了一个,所以写的不是很好。
想验证自己写的对不对,所以又写了一个前序遍历。
class Node:
def __init__(self,data):
self.data = data
self.lchild = None
self.rchild = None
class BST:
def __init__(self,node_list):
self.root = None
for node in node_list:
self.insert(node)
def search(self,data):
bt = self.root
while bt:
entry = bt.data
if data < entry:
bt = bt.lchild
elif data > entry:
bt = bt.rchild
else:
return entry
return False
def insert(self,data):
bt = self.root