python 二叉搜索树相关代码

Posted c-x-a

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 二叉搜索树相关代码相关的知识,希望对你有一定的参考价值。

class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left = None
        self.right = None


class OperationTree:
    def insert(self, root, val):
        if root is None:
            root = TreeNode(val)

        elif val < root.val:
            root.left = self.insert(root.left, val)
        elif val > root.val:
            root.right = self.insert(root.right, val)
        return root

    def printTree(self, root):
        if root is None:
            return
        self.printTree(root.left)
        print(root.val, end=‘ ‘)
        self.printTree(root.right)

    def query(self, root):
        if root is None:
            return
        if val == root.val:
            return True
        elif val < root.val:
            self.query(root.left)
        elif val > root.right:
            self.query(root.right)

    def find_min(self, root):
        if root.left:
            return self.find_min(root.left)
        else:
            return root

    def find_max(self, root):
        if root.right:
            return self.find_max(root.right)
        else:
            return root

    def del_node(self, root, val):
        if root is None:
            return
        if val < root.val:
            root.left = self.del_node(root.left, val)
        elif val > root.val:
            root.right = self.del_node(root.right, val)
        # 当val == root.val时,分为三种情况:只有左子树或者只有右子树、有左右子树、即无左子树又无右子树
        else:
            if root.left and root.right:
                # 既有左子树又有右子树,则需找到右子树中最小值节点
                temp = self.find_min(root.right)
                root.val = temp.val
                # 再把右子树中最小值节点删除,让它符合二叉搜索树的定义
                root.right = self.del_node(root.right, temp.val)
            elif root.right is None and root.left is None:
                # 左右子树都为空
                root = None
            elif root.right is None:
                # 只有左子树
                root = root.left
            elif root.left is None:
                # 只有右子树
                root = root.right
        return root


if __name__ == ‘__main__‘:
    List = [17, 5, 35, 2, 11, 29, 38, 9, 16, 8]
    root = None
    op = OperationTree()
    for val in List:
        root = op.insert(root, val)
    print(‘中序打印二叉搜索树:‘, end=‘ ‘)
    op.printTree(root)

  

以上是关于python 二叉搜索树相关代码的主要内容,如果未能解决你的问题,请参考以下文章

二叉搜索树(BST)---python实现

数据结构树相关代码(数据结构笔试复测Leecode牛客)

二叉搜索树_BST

二叉搜索树_BST

如何在python中打印二叉搜索树?

LeetCode第九十八题—验证二叉搜索树—Python实现