二叉树问题---二叉搜索树查找、插入与删除的代码实现

来源:互联网 发布:男生长发 知乎 编辑:程序博客网 时间:2024/06/06 02:31
class TreeNode:    def __init__(self, x):        self.val = x        self.left = None        self.right = None

查找

def Find(root, x):    if not root:        print("The number {} is not found".format(x))        return None    if root.val == x:        print("True")        return root    elif x < root.val:        return Find(root.left, x)    elif x > root.val:        return Find(root.right, x)

插入

def BSTinsert(root, x):    if not root:        root = TreeNode(x)    elif root.val > x:        root.left = BSTinsert(root.left, x)    elif root.val < x:        root.right = BSTinsert(root.right, x)    return root

删除

def Delete(root, x):    if not root:        raise Exception("Element not found")    elif root.val > x:        root.left = Delete(root.left, x)    elif root.val < x:        root.right = Delete(root.right, x)    elif root.left and root.right:        TmpCell = FindMin(root.right)        root.val = TmpCell.val        root.right = Delete(root.right, root.val)    else:        if not root.left:            root = root.right        else:            root = root.left    return rootdef FindMin(root):    if not root:        return None    while root.left:        root = root.left    return root
阅读全文
2 0