python数据结构 树_Python 数据结构入门 - 二叉搜索树(Binary Search Tree)

【实例简介】基于python对于二叉搜索树的探究

【实例截图】

7011eab4252eb38f0ed1e4653d76ba7c.png

263ae475f900933d840dd2b7d53c51da.png

【核心代码】

bst = BinarySearchTree()

for num in (7, 5, 9, 8, 15, 16, 18, 17):

bst.insert(num)

max_node = bst.find_max()

min_node = bst.find_min()

print(f"Max node: {max_node.data}")

print(f"Min node: {min_node.data}")

bst2 = BinarySearchTree()

for num in (7, 5, 9, 8, 15, 16, 18, 17):

bst2.insert(num)

for n in (17, 3, -2, 8, 5):

if bst2.search(n) is not None:

print(f"{n} found in the binary search tree! :D")

else:

print(f"{n} not found in the tree! :(")

bst3 = BinarySearchTree()

for num in (7, 5, 9, 8, 15, 16, 18, 17):

bst3.insert(num)

print('二叉排序树中数据为:')

mid_traverse(bst2.root)

# 删除没有孩子的结点

bst3.delete(5)

# 删除只有一个孩子的结点

bst3.delete(15)

# 删除有两个孩子的结点

bst3.delete(9)

# 查看中序遍历结果

print('\n删除后,二叉排序树中数据为:')

mid_traverse(bst2.root)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值