# Difference between Binary Tree and Binary Search Tree

In this article, we are going to see the difference between Binary Tree and Binary Search Tree. We will also look at the basic operations and their time and space complexities.We will then see the real-life applications which use these two data structures.

## 1.What is a Binary Tree?

A Binary tree is a non-linear (memory is randomly allocated) Tree data structure and it is represented in a top-down way, where the top node is called as the Root. It is a collection of nodes and is a non-ordered data structure. Each node in a Binary Tree can have a maximum of two (0,1 or 2) children, called Left and Right child. The nodes having child nodes are called a Parent node and the nodes that don’t have any children are called Leaf node.Here are the 3 main properties:

1. Data – the data in the node, any data type.
2. Left Pointer – which points to Left Child.
3. Right Pointer – which points Right Child.

Visual Representation

#### 1.1. Operations on Binary Tree with Complexities:

1. Search: Since the Binary Tree is unordered, to search an element we need to traverse all the nodes of the tree. The time complexity for the search would be O(n).
2. Insert: Since the insert can also happen to the last node of the tree hence, we need to traverse all the nodes of the tree. So, the overall complexity will be O(n).
3. Delete: Since deleting a node from a Binary Tree first requires to searching the node among all the nodes of the Binary Tree hence, the overall complexity will be O(n).

## 2.What is a Binary Search Tree

A Binary Search Tree (BST) is a special type of Binary Tree however unlike Binary Tree, the nodes in a Binary Search Tree are arranged in a pre-defined order. The structure remains however it is ordered. The following conditions must hold for a BST:

1. The data of the left node should be less than the value of its parent node.
2. The data of the right node should be greater than the value of its parent node.
3. The left and right sub-tree are BST in themselves as well.
4. Duplicate keys or values are not allowed.

Visual Representation

#### 2.1. Operations on Binary Search Tree with Complexities:

The main concept behind the BST data structure is to optimize the search(lookup) operations. While searching for an element in the BST, we remove one half (left or right) at each step because of its properties. At every step, we will reduce the search space by `n/2` (for a tree with n nodes) and will continue until an element is found.

1. Search: As per the above description, to search an element in a Binary Search Tree generally takes `O(log n)`. In the case of a Skewed BST, the worst-case time complexity would be `O(n)`.
2. Insert: For a Skewed tree, the worst-case time complexity would be `O(n)` for inserting an element.
3. Delete: For a Skewed tree, the worst-case time complexity would be `O(n)` for deleting an element.

## 3. Difference between Binary Tree and Binary Search Tree

here is a high level difference between Binary Tree and Binary Search Tree.

#### 3.1. Similarities

Though there are lot of difference between Binary Tree and Binary Search Tree, however binary search tree is based on the binary tree data structure and there are many similarities between the two.

• Both are based on Tree data structure.
• Both can have a maximum of 2 child nodes.
• Both can hold different data types.
• Both have random memory allocated to them.

## 4. Applications of Binary Tree:

#### 4.1. Applications of Binary Search Tree:

• In relational databases, BSTs can be used for indexing and multi-level indexing.
• There are various searching algorithms based on BST.
• It is useful in maintaining a sorted stream of data.
• TreeMap and TreeSet – uses BST internally.
• Huffman Coding Algorithm is based on BST.
• BST is used in UNIX kernels to managing a set of virtual memory areas (VMAs).
• Binary heaps – used to implement priority queues.
• Decision trees – used in machine learning for classification.
• B+ trees – used in database indexes

## Summary

• In this article, we have learned what is a Binary Tree and what is a Binary search tree
• In this article, we have learned the operations of a Binary Tree and a Binary search tree
• In this article, we have learned the difference between Binary Tree and Binary search tree
• In this article, we have learned the similarities between a Binary Tree and a Binary search tree
• In this article, we have also learned the real-life applications of a Binary Tree and a Binary search tree