The Search Tree的Python实现(二)

1. The Search Tree ADT-Binary Search Trees

  • The property that makes a binary tree into a binary search tree is that for every node, X, in the tree, the values of all the keys in the left subtree are smaller than the key value in X, and the values of all the keys in the right subtree are larger than the key value in X. Notice that this implies that all the elements in the tree can be ordered in some consistent manner.2叉搜索树的左子树所有key值小于其node,右子树的所有Key都大于其node。
  • In Figure 4.15, the tree on the left is a binary search tree, but the tree on the right is not. The tree on the right has a node with key 7 in the left subtree of a node with key 6 (which happens to be the root).左图是,右图不是2叉搜索树
  • the average depth of a binary search tree is O(log n)笔试题经常考,二叉搜索树的平均深度

这里写图片描述

2. 二叉搜索树的实现

本实现参考《Data Structures and Algorithm Analysis in C》中的C语言实现方式,改编成python

class Tree():
    
    def __init__(self):
        self.key=None
        self.leftree=None
        self.rigtree=None
        
    def find(self,key,tree):
        if tree == None:
            return tree
        if key < tree.key:
            return tree.find(key, tree.leftree)
        elif key > self.key:
            return tree.find(key, tree.rigtree)
        else:
            return tree
            
    def find_min(self,tree):
        if tree == None:
            return None
        elif tree.leftree == None:
            return tree
        else:
            return tree.find_min(tree.leftree)
            
    def find_max(self,tree):
         if tree.key != None:
            while tree.rigtree != None:
                tree=tree.rigtree
         return tree
    
    def insert(self,key,tree):
         if tree == None :
             tree =Tree() 
             tree.key=key
         elif tree.key ==None:
             tree.key=key
         elif key < tree.key:
             tree.leftree=tree.insert(key,tree.leftree)
         elif key > tree.key:
             tree.rigtree=tree.insert(key,tree.rigtree)
         return tree
    
    def delete(self,key,tree):
        if tree == None:
            raise Exception("can't find the key!", key)
        elif key < tree.key:
            tree.leftree=self.delete(key,tree.leftree)
        elif key > tree.key:
            tree.rigtree=self.delete(key,tree.rigtree)
        elif tree.leftree and tree.rigtree:
            tmp_tree=self.find_min(tree.rigtree)
            tree.key=tmp_tree.key
            tree.rigtree=self.delete(tree.key,tree.rigtree)
        else:
            if tree.leftree ==None:
                tree=tree.rigtree
            elif tree.rigtree ==None:
                tree=tree.leftree

        return tree

    #从列表或者元组构建二叉树
    def make_from_arrary(self,x,tree):
        for i in x:
            self.insert(i,tree)
   

    
    #中序遍历
    def print_inorder(self,tree):
         if tree != None :
             tree.print_inorder(tree.leftree)
             print(tree.key)
             tree.print_inorder(tree.rigtree)
    #前序遍历
    def print_preorder(self,tree):
         if tree != None :
             print(tree.key)
             tree.print_preorder(tree.leftree)
             tree.print_preorder(tree.rigtree)
    #后序遍历
    def print_postorder(self,tree):
         if tree != None :
             tree.print_postorder(tree.leftree)
             tree.print_postorder(tree.rigtree)
             print(tree.key)
             

if __name__=='__main__':
    a=Tree()
    a.make_from_arrary([4,3,1,7,8,2,5,10,6],a)
    print("print inorder tree:")
    a.print_inorder(a)
    print("print preorder tree:")
    a.print_preorder(a)
    print("print postorder tree:")
    a.print_postorder(a)
    a.delete(7,a)
    print("print deleted inoder tree:")
    a.print_inorder(a)

结果如下:

>>> 
================== RESTART: C:\Users\dhuang\Desktop\test.py ==================
print inorder tree:
1
2
3
4
5
6
7
8
10
print preorder tree:
4
3
1
2
7
5
6
8
10
print postorder tree:
2
1
3
6
5
10
8
7
4
print deleted inoder tree:
1
2
3
4
5
6
8
10
>>> 

特别思考以下几点,之前没学过数据结构,(本科非计算机,研究生也不是)笔者花了4个小时,可能自己比较笨吧,反复琢磨:

  • 网上有python版本,将Node作为树的单元,笔者按照《DSAA》中的建议,严格遵从递归定义,每个节点都可以看成一棵树,其应该享有树的所有method
  • 如果是c语言的话,比较容易理解,但是python中没有指针的概念,需要理解Nameobject的关系,为此笔者又回顾了以前的pyhon知识。
  • 最后delete函数还是有点缺陷,目前实现了较为简单的树的遍历和初始化。另外理解上述代码,多动手画画递归图,多注意python中的track,能很快调好代码。

NOTE: 关于树的知识和算法很多,现在只是最基本的阶段。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值