Binary Search Tree is a node-based binary tree data structure which has the following properties:
although the number of permutations of
When inserting a new node into a binary search tree, we follow these steps:
this will work but for some cases this is bad because it is going to make our tree really deep e.g.
imagine if we want to add it from 1 to n one by one then we have a tree with height n-1 which slows down our search.
When deleting a node from a binary search tree, there are three cases to consider:
the proof was a pain in... so I did not include it here cause understanding it was already hard enough.😅
if you want to find the biggest number always go right.
if you want to find the smallest number always go left.
if you want to find the successor (first number after it) of a number you have to follow 2 steps:
If the node has a right child, the successor is the leftmost node in the right subtree. We move to the right child and then traverse as far left as possible until we reach a leaf node.
If the node does not have a right child, we need to find the ancestor of the node for which the given node is in the left subtree. We move up the tree until we find an ancestor whose left child is the current node. The parent of this node is the successor.
if you want to find the predecessor (first number after it) of a number you have to follow 2 steps:
If the node has a left child, the predecessor is the rightmost node in the left subtree. We move to the left child and then traverse as far right as possible until we reach a leaf node.
If the node does not have a left child, we need to find the ancestor of the node for which the given node is in the right subtree. We move up the tree until we find an ancestor whose right child is the current node. The parent of this node is the predecessor.