Binary Search Tree

Data Structures and Algorithms Note

Allie Hsu
Coder Life
3 min readJan 31, 2022

--

What is Binary Search Tree

  • nodes on the left subtree are all smaller than the node’s data
  • nodes on the right subtree are all bigger than the node’s data
  • the left and right subtree are also binary search tree

Search a node

Similar to binary search, if target data is smaller than current node data, go node.left, otherwise, go node.right.

Insert a node

A new node is always inserted at the leaf.

Delete a node

there are three kinds of delete:

  • delete a leaf node
  • delete a node that only has one child
  • delete a node that has two children, need to find the smallest node to replace the deleted position

Inorder traversal implementing on Binary Search Tree

The problems above seems that can be solved by inorder traversal for the tree. If the result of inorder traversal of the tree is sorted in ascending order, then it is a BST. To find the predecessor and successor, just check the result of inorder traversal of the tree, the predecessor is the previous one of the target key while the next one of the target key is the successor.

--

--

Allie Hsu
Coder Life

A tech enthusiast who is keen to develop new skills