Algorithm Notes Binary Search Tree

  • A Binary Search Tree (BST) is a data structure used to store elements with comparable keys (usually numbers or strings)

1 Structural features

  • Node structure : Each node has a key and two child nodes (left child node and right child node).
  • Sort properties :
    • If the left subtree is not empty, then the values ​​of all nodes on the left subtree are less than the value of the root node
    • If the right subtree is not empty, then the values ​​of all nodes on the right subtree are greater than the value of the root node;

    • The left subtree and right subtree are also binary search trees respectively.

Such characteristics enable binary search trees to efficiently support a variety of search and dynamic collection operations.

An important feature of a binary search tree is that its in-order traversal results must be ordered.

2 Searching in Binary Search Tree

  • If the current node is empty, the search fails.

  • Otherwise, if the value of the current node is equal to the value to be found, it is returned directly.

  • Otherwise, if the value to be found is smaller than the current node, search in the left subtree of the current node. If the value you want to find is greater than the current node value, search in the right subtree of the current node.
     

3Insertion  into binary search tree

Newly inserted nodes are always leaf nodes

First find where he needs to insert, then insert

  • If the current node is empty, directly create a new node and return.

  • If the value to be inserted is smaller than the current node, search the left subtree of the current node for the insertion position.

  • If the value to be inserted is larger than the current node, search the right subtree of the current node for the insertion position.

4 Deletion of binary search tree

  • If you delete a leaf node, delete it directly.
  • If the deleted node has only one child node, replace it with this only child node.
  • If the deleted node has two child nodes, you need to consider how to store the child nodes after deleting the current node.
    • There are two ways to achieve deletion, one is to directly delete the node that needs to be deleted, and the other is to use the transposition method.
      • The disadvantage of direct deletion is that it will cause the tree to be seriously unbalanced.
      • AVL trees and red-black trees are both transposition methods.

4.1 Precursor nodes and trailing nodes

  • Predecessor node : Perform an in-order traversal on a binary tree. In the result of the traversal, the node before the current node is the predecessor node of the node;
  • Successor node : perform an in-order traversal on a binary tree. In the result of the traversal, the node after the current node is the successor node of the node;

  • Of course, finding the predecessor and successor nodes of a node does not necessarily require the trouble of printing the in-order traversal results of a binary tree.
    • The predecessor node of a
    • The successor node of a node is the smallest node of its right subtree, and this node is the node of its right subtree going all the way to the left.

4.2 Delete directly

The disadvantage of direct deletion is that it will seriously damage the balance of the tree.

4.2.1 Method 1: Let the right child node of the node to be deleted become the right child node of its predecessor node

4.2.2 Method 2: Let the left child node of the node to be deleted become the left child node of its successor node

 

5 Binary search tree performance

  • The cost of binary search tree operations (including insert, find, delete, etc.) is proportional to the number of nodes to be visited during the operation. If the binary search tree being operated is completely balanced, then the access cost will be logarithmic (O(logn)) level
  • In the worst case, the binary search tree will degenerate into a singly linked list. Time complexity is O(N)
  • Average performance is O(logn)

Guess you like

Origin blog.csdn.net/qq_40206371/article/details/132663202