一、二叉搜索树
二、二叉搜索树初步操作
1、创建
class BiTreeNode: def __init__(self,data): self.data = data self.lchild =None self.rchild = None class BST: def __init__(self,li=None): self.root = None if li: self.root = self.insert(self.root,li[0]) for val in li[1:]: self.insert(self.root,val)
2、遍历
for i in [1,5,9,8,7,6,4,3,2]: tree.insert_no_rec(i) tree.in_order(tree.root) print(tree.query_no_rec(12))
中序序列是直接排好序的
三、二叉树的查询、插入、删除
1、查询
def query(self,root,val): if not root: return False if root.data == val: return False elif root.data > val: return self.query(root.lchild, val) else: return self.query(root.rchild,val) def query_no_rec(self,val): p = self.root while p: if p.data == val: return True elif p.data > val: p = p.lchild else: p = p.rchild return False
2、插入
class BiTreeNode: def __init__(self,data): self.data = data self.lchild =None self.rchild = None class BST: def __init__(self,li=None): self.root = None if li: self.root = self.insert(self.root,li[0]) for val in li[1:]: self.insert(self.root,val) def insert(self,root,val): if root is None: root = BiTreeNode(val) elif val < root.data: root.lchild = self.insert(root.lchild,val) else: root.rchild = self.insert(root.rchild,val) return root def insert_no_rec(self,val): p = self.root if not p: self.root = BiTreeNode(val) return while True: if val < p.data: if p.lchild: p = p.lchild else: p.lchild = BiTreeNode(val) break
3、删除
动画演示地址:https://visualgo.net/en/bst
四、二叉搜索树的问题
1、随机化的二叉树搜索树
1、对列表先随机打乱
2、软后在建树
2、avl树
五、B-tree
MYSQL的数据库索引用的就是B-Tree索引
六、二叉搜索树代码测试
1、测试
1、实现代码
class BiTreeNode: def __init__(self,data): self.data = data self.lchild = None self.rchild = None class BST: def __init__(self,li=None): self.root = None if li: self.root = self.insert(self.root,li[0]) for val in li[1:]: self.insert(self.root,val) def insert(self,root,val): if root is None: root = BiTreeNode(val) elif val < root.data: root.lchild = self.insert(root.lchild,val) else: root.rchild = self.insert(root.rchild,val) return root def insert_no_rec(self,val): p = self.root if not p: self.root = BiTreeNode(val) return while True: if val < p.data: if p.lchild: p = p.lchild else: p.lchild = BiTreeNode(val) break else: if p.rchild: p = p.rchild else: p.rchild = BiTreeNode(val) break def query(self,root,val): if not root: return False if root.data == val: return False elif root.data > val: return self.query(root.lchild, val) else: return self.query(root.rchild,val) def query_no_rec(self,val): p = self.root while p: if p.data == val: return True elif p.data > val: p = p.lchild else: p = p.rchild return False def in_order(self,root): if root: self.in_order(root.lchild) print(root.data,end=',') self.in_order(root.rchild) tree = BST() for i in [1,5,9,8,7,6,4,3,2]: tree.insert_no_rec(i) tree.in_order(tree.root) print(tree.query_no_rec(12))
2、输出
"C:Program FilesPython35python.exe" E:/工作目录/python/test/B-tree.py 1,2,3,4,5,6,7,8,9,False Process finished with exit code 0
2、不打印结果
1、实现代码
#print(tree.query_no_rec(12))
2、输出
"C:Program FilesPython35python.exe" E:/工作目录/python/test/B-tree.py 1,2,3,4,5,6,7,8,9, Process finished with exit code 0