## Introduction

A Binary Search tree is a fundamental data structure. It is used to implement other abstract data structures like sets, multisets, etc. It is more efficient than a standard binary tree. Operations like searching, insertion and deletion are performed faster and more efficiently in a binary search tree.

### What is the difference between a binary tree and a binary search tree?

Binary trees and binary search trees are non-linear data structures. Each node in the binary tree and binary search tree can have at most two children. Binary search trees are node-based. The elements are added and deleted based on the values present in each node compared with the parent node.

A binary search tree has a left subtree and a right subtree. Binary trees do not follow any order while insertion and deletion, which in binary search tree elements are inserted and deleted in a specific manner. This is why operations like insertion, deletion, and searching are expected much faster in binary search trees than in binary trees.

To know more check this out.

## What is the basic idea behind binary search trees?

In a binary search tree, every element present in the left subtree of each node must have a value lesser than the value of its parent. Every element present in the right subtree of each node must have a value greater than the value of its parent.

Example1: To store elements from 1 to 10 such that 7 is the root. All values less than 7 will be held in the nodes of the left subtree. All the values greater than 7 will be stored in the nodes of the right subtree.

Example2: Diagram demonstrating how elements are arranged in a binary search tree.

The above example is that of a binary search tree. In this tree, the root is 15. The element in the next level is 11 and 26. Since 11 is smaller than 15, the element is inserted into the node present on the left. Since 26 is greater than 15, it is inserted in the node present in the right to 15.

The element to be added next is 8. The number is first compared to the root that has the value 15. Since 8 is less than 15, 8 is moved to the left subtree of 15. In the left subtree, the root value is 11. 11 when compared with the value to be entered that is 8,8 is less than 11, so it is moved to its left subtree. Since no elements are present in the left subtree, a new node is created to the left of 11, and the node with the value 8 is inserted.

The element to be added next is 30. The number is first compared to the root that has the value 15. Since 30 is greater than 15, 30 is moved to the right subtree of 15. In the right subtree, the root value is 26. 26 when compared with the value to be entered, 30,30 is greater than 26, so it is moved to its right subtree. Since no elements are present in the right subtree, a new node is created to the right of 26, and the node with the value 30 is inserted.

The rest of the elements are added to it similarly.