Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconArtificial Intelligences USbreadcumb forward arrow iconWhat is Binary Search Tree? Everything you need to know

What is Binary Search Tree? Everything you need to know

Last updated:
17th Sep, 2022
Views
Read Time
10 Mins
share image icon
In this article
Chevron in toc
View All
What is Binary Search Tree? Everything you need to know

Different data structures help organize and store data in various forms in computer science. Data structures comprise a collection of data values, their relationships, and multiple functions that we can apply to data and work with using specific algorithms. There are various types of data structures, and the tree is one of them. Trees are non-linear data structures with a central node, structural nodes, and sub-nodes connected via edges. 

Our AI & ML Programs in US

This article explores the concept of binary tree in data structure and binary search trees in detail.

What is the binary tree in data structure?

A binary tree is a non-linear data structure comprising nodes, and each parent has a maximum of two children. Each node in the tree contains the data element, the pointer to the left child, and a pointer to the right child. The node at the top of the hierarchy is the root node, and the nodes that carry the subnodes are called the parent nodes. Each parent node has two child nodes: the right child and the left child. 

Get Machine Learning Certification from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.

Ads of upGrad blog

The diagram below represents a binary tree data structure. 

binary tree in data structure

Source

Terminologies Associated With Binary Trees

Now, let’s understand each terminology associated with binary tree data structures in more detail.

Terminology Description
1. NodeA node represents a termination point in a binary tree.
2. RootA root is a binary tree’s topmost node.
3. ParentAny node of the binary tree (besides the root) with at least one subnode of its own is a parent.
4. ChildAs you move away from the root, the node that arises straight from the parent node is the child.
5. Internal nodeThese are inner nodes with a minimum of one child node.
6. Leaf nodeThese are the external nodes with no child.
7. EdgeAn edge connects two nodes to indicate a relationship between them.
8. Height/depth of a treeThe tree height or the root height represents the largest number of edges from the root to the farthest leaf node.

What is a binary search tree?

A binary search tree is a particular type of binary tree with three basic properties:

  • The left subtree of a node has elements with values lower than the value of the node
  • The right subtree of a node has elements with values greater than the value of the node
  • Both the right and left subtrees should be binary search trees 

A binary search tree is named so because it increases the efficiency of search operation in a logarithmic tree. 

The illustration below explains what a binary search tree is (left) and what it is not (right).

binary search tree

Source

The binary tree on the right is not a binary search tree because the right subtree of the node “3” has a value (2) smaller than the node.

Binary Search Tree Example

Let’s understand the concept of binary search trees with an example:

Binary Search Tree Example

Source

In the above figure, the root node is 25, and all the nodes of the left subtree are smaller than the root node. On the contrary, all the nodes of the right subtree are greater than the root node’s value.

Likewise, we can see that the right child of the root node (36) is greater than its left child (20), satisfying the condition of a binary search tree. The nodes “5,” “12,” “28,” “38,” and “48” are leaf nodes with no child. Thus, we can say that the binary tree in the above illustration is a binary search tree.

Types of Binary Trees

Binary trees have different kinds, and each has unique characteristics. The list below gives an overview of the different types of binary trees:

  • Full binary tree: A full binary tree has either two or zero children. In other words, all the nodes in a full binary tree either have two children nodes, or the parent node itself is the external node or lead node. Thus, every node other than the external node has two children.

Full binary tree

Source

  • Perfect binary tree: In a perfect binary tree, all the internal nodes have a maximum of two children, and every leaf node is at the same depth or level within the tree. 

Perfect binary tree

Source

  • Complete binary tree: In a complete binary tree, nodes occupy all the tree levels except the tree’s lowest level. In addition, every node typically occupies the left side in the lowest level of the binary tree.

Complete binary tree

Source

  • Degenerate binary tree: If every internal node in a binary tree has only a single child, it is known as a degenerate binary tree or a pathological binary tree. 

Degenerate binary tree

Source

  • Balanced binary tree: A balanced binary tree or a height-balanced binary tree is one where the height of the left and the right subtrees of any node differ by not more than 1.

Balanced binary tree

Source

Basic Operations in Binary Search Tree

Now, let’s briefly walk you through some of the basic operations you can perform on a binary search tree:

1. Search

Searching in a binary search tree means finding a specific node or element in a data structure. Below are the steps to search a node in a binary search tree:

  • Compare the element to be searched with the root value of the tree.
  • Return the node’s location if the root’s value matches the target element.
  • If the root’s value does not match the target element, check if the latter is smaller than the root element. If yes, then move to the left subtree.
  • If the target element is larger than the root, move to the right subtree.
  • Repeat the above procedure until the match is found.
  • Return NULL if the target element is not found in the tree.

Consider the following binary search tree and suppose we have to search for the node “20.”

 search for the node “20”

Source

Steps illustrating how to find the node “20” in the binary search tree:

Find the node “20” in the binary search tree - Step 1 & Step 2

Find the node “20” in the binary search tree - Step 3

Source

2. Insert

Inserting a new element in a binary search tree always takes place at the leaf. We start searching from the root node to insert an element in a binary search tree. If the inserting node has a value less than the root, we search for an empty location in the left subtree. On the contrary, if our data element’s value is greater than the node, we search for an empty place in the right subtree and insert the element. We must maintain the binary search tree property during insertion that the right subtree is larger than the root and the left subtree is smaller than the root.

The following diagram illustrates the insertion process in a binary search tree:

Inserting a new element in a binary search tree

Source

3. Delete

We must keep in mind not to violate the fundamental property of a binary search tree while performing the deletion operation. Binary search tree nodes can be deleted using three possible situations:

  • The target node is the leaf node: We replace the leaf node with NULL and free the allocated space.

The target node is the leaf node

Source

  • The target node has only one child: In this case, we replace the target node with its child, followed by deleting the child node.

The target node has only one child

Source

  • The target node has two children: In this case, we first find the inorder successor of the node we want to delete. Then, we replace the target node with the inorder successor until the target node is at the tree’s leaf. Finally, replace the target node with NULL and free up the designated space.

The target node has two children

 Source

Popular AI and ML Blogs & Free Courses

Wrapping Up

A binary search tree is a popular searching technique with applications in indexing, multi-level indexing, and maintenance of a sorted stream of data. Binary search trees are also widely used to implement various searching algorithms.

Ads of upGrad blog

Search algorithms also form a fundamental component of artificial intelligence and machine learning problems. If you are interested to learn more, check out upGrad’s Master of Science in Machine Learning & AI in collaboration with Liverpool John Moores University. 

The 20-month online program is best suited for candidates who wish to learn in-demand skills such as deep learning, NLP, and reinforcement learning with hands-on experience in 12+ industry projects, multiple programming languages and tools, and a master dissertation.

Sign up today to get exclusive upGrad advantages, including 360-degree career assistance, peer-to-peer learning, and networking. 

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.
Get Free Consultation

Select Coursecaret down icon
Selectcaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Best Artificial Intelligence Course

Frequently Asked Questions (FAQs)

1What are binary trees and their types?

A binary tree is a non-linear data structure with at most two children for each parent node. The node at the top of the tree is the root node, and the nodes that carry other sub-nodes are called parent nodes. Binary trees can be of five types: full, complete, perfect, balanced, and degenerate.

2Can a binary tree have one child?

A degenerate binary tree is where every internal node has a single child.

3What is a perfect binary tree?

A perfect binary tree is one type of binary tree where every internal node has a maximum of two child nodes, and all the leaf nodes are at the same tree level.

Explore Free Courses

Suggested Blogs

Top 25 New & Trending Technologies in 2024 You Should Know About
63210
Introduction As someone deeply immersed in the ever-changing landscape of technology, I’ve witnessed firsthand the rapid evolution of trending
Read More

by Rohit Sharma

23 Jan 2024

Basic CNN Architecture: Explaining 5 Layers of Convolutional Neural Network [US]
6375
A CNN (Convolutional Neural Network) is a type of deep learning neural network that uses a combination of convolutional and subsampling layers to lear
Read More

by Pavan Vadapalli

15 Apr 2023

Top 10 Speech Recognition Softwares You Should Know About
5509
What is a Speech Recognition Software? Speech Recognition Software programs are computer programs that interpret human speech and convert it into tex
Read More

by Sriram

26 Feb 2023

Top 16 Artificial Intelligence Project Ideas & Topics for Beginners [2024]
6135
Artificial intelligence controls computers to resemble the decision-making and problem-solving competencies of a human brain. It works on tasks usuall
Read More

by Sriram

26 Feb 2023

15 Interesting Machine Learning Project Ideas For Beginners & Experienced [2024]
5614
Taking on machine learning projects as a beginner is an excellent way to gain hands-on experience and develop a better understanding of the fundamenta
Read More

by Sriram

26 Feb 2023

Explaining 5 Layers of Convolutional Neural Network
5210
A CNN (Convolutional Neural Network) is a type of deep learning neural network that uses a combination of convolutional and subsampling layers to lear
Read More

by Sriram

26 Feb 2023

20 Exciting IoT Project Ideas & Topics in 2024 [For Beginners & Experienced]
9792
IoT (Internet of Things) is a network that houses multiple smart devices connected to one Cloud source. This network can be regulated in several ways
Read More

by Sriram

25 Feb 2023

Why Is Time Complexity Important: Algorithms, Types & Comparison
7576
Time complexity is a measure of the amount of time needed to execute an algorithm. It is a function of the algorithm’s input size and the type o
Read More

by Sriram

25 Feb 2023

Curse of dimensionality in Machine Learning: How to Solve The Curse?
11284
Machine learning can effectively analyze data with several dimensions. However, it becomes complex to develop relevant models as the number of dimensi
Read More

by Sriram

25 Feb 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon