二叉查找树(binary search tree)
顾名思义二叉查找树中每个节点至多有两个子节点,并且还对存储于每个节点中的关键字值有个小小的要求,
即只要这个节点有左节点或右节点,那么其关键字值总的
大于其左节点的关键字值,
小于其右节点的关键字值,如下图:
因为树的结构特性,很适合使用递归的方式去操作,下面的实现中均是以递归的方式实现:
下面仅给出了python的实现,一是因为代码太长,二是python的实现是我对着C语言实现改过来的,基本没什么差别; 主要实现的方法有:
- 遍历:
前序:preorder()——理根节点→处理左子树→处理右子树
中序:inorder()——处理左子树→处理根节点→处理右子树
后序:postorder()——处理左子树→处理右子树→处理根节点
- 插入:
insert(key)——将关键字值为key的节点插入到适当的位置(注释里面的是非递归实现)
- 删除:
delete(key)——将关键字值为key的节点从树中删掉(注释中给了说明)
- 获取高度:
height()
- 获取树中的节点数:
count()
- 查找:
find(key)——查找关键字值为key的节点(二叉查找树的一个重要应用就是在查找中)
- 获取树中最大key值节点和最key值小节点:
find_max()
find_min()
;;;
class tree_node:
def __init__(self, key = None, left = None, right = None):
self.key = key
self.left = left
self.right = right
class binary_search_tree:
def __init__(self):
self.root = None
def preorder(self):
print 'preorder: ',
self.__preorder(self.root)
print
def __preorder(self, root):
if not root:
return
print root.key,
self.__preorder(root.left)
self.__preorder(root.right)
def inorder(self):
print 'inorder: ',
self.__inorder(self.root)
print
def __inorder(self, root):
if not root:
return
self.__inorder(root.left)
print root.key,
self.__inorder(root.right)
def postorder(self):
print 'postorder: ',
self.__postorder(self.root)
print
def __postorder(self, root):
if not root:
return
self.__postorder(root.left)
self.__postorder(root.right)
print root.key,
def insert(self, key):
'''recursion'''
self.root = self.__insert(self.root, key)
def __insert(self, root, key):
if not root:
root = tree_node(key)
else:
if key < root.key:
root.left = self.__insert(root.left, key)
elif key > root.key:
root.right = self.__insert(root.right, key)
else:
print key, 'is already in tree'
return root
##non-recursion
## def insert(self, key):
## if not self.root:
## self.root = tree_node(key)
## else:
## cur = self.root
## while True:
## if key < cur.key:
## if not cur.left:
## cur.left = tree_node(key)
## break
## cur = cur.left
## elif key > cur.key:
## if not cur.right:
## cur.right = tree_node(key)
## break
## cur = cur.right
## else:
## print key, 'in tree'
## break
def height(self):
return self.__height(self.root)
def __height(self, root):
if not root:
return -1
left_height = self.__height(root.left)
right_height = self.__height(root.right)
#return 1+(left_height if left_height>right_height else right_height)#这种方式是自己写的,后面两种高大上的是网上偷学的^_^
#return 1+[left_height,right_height][left_height<right_height]
return 1+(left_height>right_height and [left_height] or [right_height])[0]
def count(self):
'''elements in tree'''
return self.__count(self.root)
def __count(self, root):
if not root:
return 0
return 1+self.__count(root.left)+self.__count(root.right)
def delete(self, key):
self.root = self.__delete(self.root, key)
##
##删除操作:
##首先找到删除的节点,
##1. 如果左右子树都不为空,则找到右子树中最小的节点min,用min.key代替删除节点的key,然后再到右子
## 树中删除min节点,因为min没有左节点,所以删除它的话只需要用它的右节点代替它(如果有右节点);
##2. 如果左子树或者右子树不为空,则直接代替掉
##3. 如果左右均空即叶子节点,直接删掉
def __delete(self, root, key):
if not root:
print 'not find key: ', key
elif key < root.key:
root.left = self.__delete(root.left, key)
elif key > root.key:
root.right = self.__delete(root.right, key)
elif root.left and root.right: #found
right_min = self.__find_min(self.root.right)
root.key = right_min.key
root.right = self.__delete(root.right, right_min.key)
elif root.left:
root = root.left
elif root.right:
root = root.right
else:
root = None #python的GC会在没有引用指向对象的时候销毁对象
return root
def find(self, key):
node = self.__find(self.root, key)
if not node:
print 'not found'
return node
def __find(self, root, key):
if not root:
return None
if key < root.key:
return self.__find(root.left, key)
elif key > root.key:
return self.__find(root.right, key)
else:
return root
def find_min(self):
return self.__find_min(self.root)
def __find_min(self, root):
if not root.left:
return root
return self.__find_min(root.left)
def find_max(self):
return self.__find_max(self.root)
def __find_max(self, root):
if not root.right:
return root
return self.__find_max(root.right)
def main():
import random
root = binary_search_tree()
for i in random.sample([j for j in range(1, 100)], 5):
root.insert(i)
print 'insert: '
root.insert(78)
root.insert(101)
root.insert(14)
root.preorder()
root.inorder()
root.postorder()
print 'height: ', root.height()
print 'count: ', root.count()
print 'min: ', root.find_min().key
print 'max: ', root.find_max().key
print 'delete: '
root.delete(101)
root.delete(12)
root.preorder()
root.inorder()
root.postorder()
print root.find(71)
print root.find(78)
print 'height: ', root.height()
print 'count: ', root.count()
print 'min: ', root.find_min().key
print 'max: ', root.find_max().key
if __name__ == '__main__':
main()