二叉搜索树(BST)

二叉搜索树的实现以及相关操作(python)

# 树节点数据结构的定义
class TreeNode(object):
    def __init__(self,val):
        self.val = val
        self.left = None
        self.right = None

        
# 一些基本的搜索二叉树的操作
class Solution(object):
    def insert(self,root,val):
        """
        :type root : TreeNode
        :type x : int
        :rtype root : TreeNode
        """
        if not root:
            root = TreeNode(val) 
        elif x < root.val:
            root.left = self.insert(root.left,val)
        else:
            root.right = self.insert(root.right,val)
        return root
    def query(self,root,val):
        """
        :type root : TreeNode
        :type x : int
        :rtype : bool 
        """
        if root == None:
            return False
        if root.val == val:
            return True
        elif val < root.val:
            return self.query(root.left, val)
        elif val > root.val:
            return self.query(root.right, val)

    def delete(self,root,val):
        """
        :type root : TreeNode
        :type x : int
        :rtype root :TreeNode 
        """
        if root == None:
            return 
        if val < root.val:
            root.left = self.delNode(root.left, val)
        elif val > root.val:
            root.right = self.delNode(root.right, val)
        # 当val == root.val时,分为三种情况:只有左子树或者只有右子树、有左右子树、即无左子树又无右子树
        else:
            if root.left and root.right:
                # 既有左子树又有右子树,则需找到右子树中最小值节点
                temp = self.findMin(root.right)
                root.val = temp.val
                # 再把右子树中最小值节点删除
                root.right = self.delNode(root.right, temp.val)
            elif root.right == None and root.left == None:
                # 左右子树都为空
                root = None
            elif root.right == None:
                # 只有左子树
                root = root.left
            elif root.left == None:
                # 只有右子树
                root = root.right
        return root
    
    def findMin(self,root):
        """
        :type root : TreeNode
        :rtype : int
        """
        if root.left:
            return self.findMin(root.left)
        else:
            return root
        
    def findMax(self,root):
        """
        :type root : TreeNode
        :rtype : int
        """
        if root.right:
            return self.findMax(root.right)
        else:
            return root
        
    def printTree(self,root):
        """
        :type root : TreeNode
        :rtype : None
        """
        if not root:
            return 
        # 按照中序遍历输出,其为有序序列
        self.printTree(root.left)
        print(root.val,end = ' ')
        self.printTree(root.right)
        
    
# 利用二叉搜索树的一些性质去解决一些实际问题(多数是leetcode上的)
class Solution_2(object):
    # 二叉搜索树的最近公共祖先
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        # 解决思路:利用BST的一些性质
        if root.val > p.val and root.val > q.val:
            return self.lowestCommonAncestor(root.left,p,q)
        elif root.val < p.val and root.val < q.val:
            return self.lowestCommonAncestor(root.right,p,q)
        else:
            return root
        
    # 二叉搜索树中的众数
    def findMode(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        # 解决思路:中序遍历获得一个排好序的数组,再在数组中寻找众数
        def inorder(root,res = []):
            if not root:
                return 
            inorder(root.left,res)
            res.append(root.val)
            inorder(root.right,res)
            return res

        if not root:
            return 
        temp = inorder(root)
        dic = {}
        res = []
        for index,num in enumerate(temp):
            if num not in dic:
                dic[num] = 1
            else:
                dic[num] +=1
        n = max(dic.values())
        for index,num in dic.items():
            if dic[index] == n:
                res.append(index)
        return res
    
    # 二叉搜索树的最小绝对差
    def getMinimumDifference(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        # 解决思路
        # 目标就是找到最小的元素与第二小的元素,二者做差即可,中序遍历(错)
        # 注意题目中给的是差的绝对值的最小
        def inorder(root,res = []):
            if not root:
                return 
            inorder(root.left)
            res.append(root.val)
            inorder(root.right)
            return res
        if not root:
            return 
        temp = inorder(root)
        temp_2 =[]
        for i in range(1,len(temp)):
            temp_2.append(abs(temp[i]-temp[i-1]))

        return min(temp_2)
    
    # 把二叉搜索树转变成累加树
    def convertBST(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        # 解决思路
        # 使用栈
        total = 0
        
        node = root
        stack = []
        while stack or node is not None:
            while node is not None:
                stack.append(node)
                node = node.right

            node = stack.pop()
            total += node.val
            node.val = total
            node = node.left
        return root
    
    # 修剪二叉搜索树
     def trimBST(self, root, L, R):
        """
        :type root: TreeNode
        :type L: int
        :type R: int
        :rtype: TreeNode
        """
        # 解决思路
        pass
    

if __name__ == '__main__':
    a = TreeNode(17)
    b = TreeNode(5)
    c = TreeNode(35)
    d = TreeNode(2)
    e = TreeNode(16)
    f = TreeNode(29)
    g = TreeNode(38)
    h = TreeNode(33)
    a.left = b
    a.right = c
    b.left = d
    b.right = e
    c.left = f
    c.right = g
    f.right = h
    demo = Solution()
    demo.printTree(a)
    demo.insert(b,19)
    demo.print(b)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值