Find a binary search tree, insert and delete

Find a binary search tree, insert and delete

Binary search tree (BST, Binary Search Tree)

Binary search tree: a binary tree, may be empty; if not empty, then the following properties:

  1. All key non-empty left subtree <root of its key

  2. All keys nonempty right subtree <its root keys

  3. Left and right sub-trees are binary search trees

A, bst Find

Including data search and find the minimum maximum values.

Position Find (ElementType X, BinTree BST): Find the element X from the BST in the return address which the node resides.

Ideas: Find the root from the beginning, if the tree is empty, return NULL

If the search tree is not empty, then the root keyword and X are compared, and different treatments:

(1) If X <root key, simply continue to search in the left subtree

(2) if X> root key, the search continues in the right subtree

(3) if x = root key, the search is completed, the node returns a pointer pointing to this

code

def find(self, root, val):
    if not root:
        return False
    if root.val == val:
        return True
    elif root.val < val:
        return find(root.left, val)
    elif root.val > val:
        return find(root.right, val)

Position FindMin (BST): find and return address of the node from where the smallest element of BST.

code

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

Position FindMax (BST): Finds and returns the address of the node where the largest element from BST in.

code

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

Two, bst insertion

Starting from the root, if the inserted value is smaller than the value of the root node, then insert it into the left sub-tree root node; if greater than the value of the root node, it is inserted into the right sub-tree root node. This operation can be implemented using recursion.

code

def insert(self, root, val):
    if not root:
        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

Third, delete the bst

three conditions:

  1. Delete leaf nodes: delete, and modify a pointer to its parent node

    Delete leaf nodes

  2. Nodes removed only one child node: The parent node pointer pointing to the node you want to remove child nodes

    Only delete a node child node

  3. Alternatively the largest element to be deleted node with a right subtree of the smallest element of another node or left subtree: To delete a node has left and right two subtrees

    Replace the current minimum node elements with the right subtree

    Right around the node _

    The maximum current tree element in place with the left child node

    _ Left around the junction

    code

def delNode(self, root, val):
    if not root:
        return
    if val < root.val:
        root.left = self.delNode(root.left, val)
    elif val > root.val:
        root.right = self.delNode(root.right, 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 not root.right and not root.left:
            # 左右子树都为空
            root = None
        elif not root.right:
            # 只有左子树
            root = root.left
        elif not root.left:
            # 只有右子树
            root = root.right
        return root

Guess you like

Origin www.cnblogs.com/wyz-2020/p/12354135.html