python 二叉排序树

2021-06-20 08:03

阅读:446

标签:sel   div   range   tno   nod   lis   tac   __iter__   ret   

class BSTNode:

    def __init__(self, data, left=None, right=None):
  
        self.data = data
        self.left = left
        self.right = right
 
 
class BinarySortTree:
  
    def __init__(self):
        self._root = None
 
    def is_empty(self):
        return self._root is None
 
    def search(self, key):
      
        bt = self._root
        while bt:
            entry = bt.data
            if key  entry:
                bt = bt.right
            else:
                return entry
        return None
 
    def insert(self, key):
    
        bt = self._root
        if not bt:
            self._root = BSTNode(key)
            return
        while True:
            entry = bt.data
            if key  entry:
                if bt.right is None:
                    bt.right = BSTNode(key)
                    return
                bt = bt.right
            else:
                bt.data = key
                return
 
    def delete(self, key):
     
        p, q = None, self._root   
        if not q:
            print("empty tree")
            return
        while q and q.data != key:
            p = q
            if key 

  

python 二叉排序树

标签:sel   div   range   tno   nod   lis   tac   __iter__   ret   

原文地址:https://www.cnblogs.com/sea-stream/p/9689120.html


评论


亲,登录后才可以留言!