# Binary Search Tree (BTS) Introduction!

A binary search tree (BST) is a type of data structure used for storing and organizing items in a way that allows for fast searching, insertion, and deletion of elements. In a BST, each node has at most two children, which are referred to as the left and right children. The left child of a node contains a value that is less than or equal to the value of the parent node, while the right child contains a value that is greater than the parent node. This property of the BST allows for fast searching because it eliminates the need to search through every element in the tree to find a specific item.

## How Does Binary Tree Work?

A binary search tree (BST) is a data structure that organizes items in a way that allows for fast searching, insertion, and deletion of elements. Each node in a BST contains a value, as well as a reference to its left and right children.

When inserting a new value into a BST, the algorithm first compares the new value to the value of the root node. If the new value is less than the value of the root node, it is inserted as the left child of the root node. If the new value is greater than the value of the root node, it is inserted as the right child. This process is then repeated for each subsequent node, comparing the new value to the value of the current node and deciding whether to insert it as the left or right child.

When searching for a value in a BST, the algorithm begins at the root node and compares the value to be searched with the value of the current node. If the value is less than the value of the current node, the algorithm moves to the left child. If the value is greater than the value of the current node, the algorithm moves to the right child. This process is repeated until the value is found or the algorithm reaches a leaf node without the value.

When deleting a value from a BST, the algorithm first locates the node containing the value. If the node has no children, it can be simply removed from the tree. If the node has one child, it can be replaced with its child. If the node has two children, it can be replaced with the smallest value in its right subtree or largest value in its left subtree.

The advantages of BST include its O(log n) time complexity for search, insert and delete operations. The disadvantage is that, if the tree is not balanced, it can degenerate into a linked list, resulting in worst case complexity of O(n)

It is also worth mentioning that there are multiple types of BST, like AVL tree, red-black tree, splay tree, etc. which are variations that maintain balance and have same or better time complexity than a regular BST.

## Types of Binary Tree

There are several types of binary search trees, each with its own unique characteristics and performance characteristics:

**Simple Binary Search Tree (BST):**This is the most basic type of binary search tree, where each node has at most two children. It is easy to implement, but the tree can become unbalanced if elements are inserted in a non-random order.**AVL Tree:**AVL trees are self-balancing binary search trees, which means they maintain a balance property at all times. This allows for fast insertion and deletion operations, as well as a guaranteed O(log n) time complexity for search operations.**Red-Black Tree:**This type of binary search tree is also self-balancing and maintains a balance property using the colors of the nodes (red and black). They have similar performance characteristics to AVL trees, but the algorithms for insertion and deletion are slightly different.**Splay Tree:**Splay trees are a type of self-adjusting binary search tree, where recently accessed elements are moved closer to the root of the tree. This improves the performance of frequently accessed elements but can make less frequently accessed elements harder to find.**B-Tree and B+Tree:**B-trees and B+trees are balanced search trees which are used for storing large data on disk or flash memory. They are mostly used in databases and file systems.

All these types of BSTs are used based on the specific requirement of the application and have trade-offs between time and space complexity.

## Example of Binary Search Tree

```
class Node {
int key;
Node left, right;
public Node(int item) {
key = item;
left = right = null;
}
}
class BinarySearchTree {
Node root;
BinarySearchTree() {
root = null;
}
void insert(int key) {
root = insertRec(root, key);
}
Node insertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
return root;
}
void inorder() {
inorderRec(root);
}
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}
public static void main(String[] args) {
BinarySearchTree tree = new BinarySearchTree();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.inorder();
}
}
```

## Applications of Binary Search Tree

Binary Search Trees (BSTs) are a type of data structure that can be used in a variety of applications, including:

**Searching and sorting:**BSTs can be used to quickly search for a specific element in a large dataset. The average time complexity for searching in a BST is O(log n), which is much faster than searching through an unsorted list (O(n)). BSTs can also be used to sort elements in a dataset by in-order traversal.**Database indexing:**BSTs can be used to index large databases, which allows for faster search and retrieval of data.**Compiler design:**BSTs can be used to store and organize symbols used in programming languages, such as variables and functions.**File systems:**Some file systems, such as NTFS and ext3, use BSTs to organize and store files on a disk.**Graph algorithms:**Many graph algorithms such as Dijkstra's algorithm, Prim's algorithm and Kruskal's algorithm use the concept of a priority queue, which can be efficiently implemented using a binary heap or a binary search tree.**Game development:**Game development, such as AI pathfinding, collision detection, and other game mechanics can use BSTs to optimize searching and sorting operations.

## Time and Space Complexity of Binary Search Tree

The time complexity of a binary search tree (BST) is the amount of time it takes for the algorithm to complete its task, and the space complexity is the amount of memory used by the algorithm.

The time complexity of operations on a BST can be characterized by the height of the tree, which is the number of edges from the root to the deepest leaf. In the best case, the height of the tree is logarithmic in the number of nodes, and in the worst case, the height of the tree is linear in the number of nodes.

- The time complexity for the following operations on a BST are:
**Search:**O(log n) (best case) to O(n) (worst case)**Insertion:**O(log n) (best case) to O(n) (worst case)**Deletion:**O(log n) (best case) to O(n) (worst case)**In-order traversal:**O(n)**Pre-order traversal:**O(n)**Post-order traversal:**O(n)

The space complexity of a BST is O(n), where n is the number of nodes in the tree. This is because the space required to store a BST is proportional to the number of elements in the tree.

It's important to notice that the above complexities are based on average case, if the tree is skewed or unbalanced the time complexity could change.

## Advantages and Disadvantages of Binary Search Tree

Advantages of Binary Search Trees (BSTs):

**Fast searching:**The average time complexity for searching in a BST is O(log n), which is much faster than searching through an unsorted list (O(n)).**Fast sorting:**BSTs can be used to sort elements in a dataset by in-order traversal, which has a time complexity of O(n).**Flexibility:**BSTs are flexible in terms of the operations that can be performed on them, such as insertion, deletion, and searching.**Dynamic memory allocation:**BSTs can adjust to the number of elements in the tree, making them a good choice for applications that require dynamic memory allocation.

Disadvantages of BSTs:

**Unbalanced trees:**If the tree becomes unbalanced, the time complexity for operations can degrade to O(n) which is not optimal.**Extra memory:**BSTs require extra memory to store the left and right pointers for each node.**Not suitable for caching:**BSTs are not a good choice for caching because the elements are stored based on their value, and not based on the frequency of access.**Not suitable for parallel computing:**The tree structure of BSTs makes it difficult to perform parallel operations on the data.

Overall, BSTs are a powerful data structure with many advantages, but they also have some disadvantages that should be considered when choosing a data structure for a particular application.