0% found this document useful (0 votes)
6 views

DSA Unit 2 notes_part 2

A Binary Search Tree (BST) is a binary tree where each node's left subtree contains only nodes with keys lesser than the node's key, and the right subtree contains only nodes with keys greater. BSTs are used for sorting datasets, efficient searching, database indexing, and implementing symbol tables. Basic operations include creating, inserting, deleting, and searching nodes, with time complexities of O(log n) for best and average cases, and O(n) for worst cases.

Uploaded by

Gaurav Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

DSA Unit 2 notes_part 2

A Binary Search Tree (BST) is a binary tree where each node's left subtree contains only nodes with keys lesser than the node's key, and the right subtree contains only nodes with keys greater. BSTs are used for sorting datasets, efficient searching, database indexing, and implementing symbol tables. Basic operations include creating, inserting, deleting, and searching nodes, with time complexities of O(log n) for best and average cases, and O(n) for worst cases.

Uploaded by

Gaurav Roy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Binary Search Tree:

What is Binary Search Tree?

Binary Search Tree is a binary tree data structure which has the
following properties:
 The left subtree of a node contains only nodes with keys lesser than
the node’s key.
 The right subtree of a node contains only nodes with keys greater
than the node’s key.
 The left and right subtree each must also be a binary search tree.

Applications of Binary tree:


1. A BST can be used to sort a large dataset. By inserting the elements
of the dataset into a BST and then performing an in-order traversal,
the elements will be returned in sorted order.

2. In Binary tree the left subtree of a node contains values that are
smaller than the node’s value, and the right subtree contains values
that are larger. Due to this property, BSTs allow for efficient
searching by repeatedly dividing the search space in half, which
makes it an important data structure in computer science

3. Used in Database indexing.

4. BSTs can be used to implement symbol tables, which are used to


store data such as variable and function names in a programming
language.
Basic Operations on BST:

1.Create
2.Insert
3.Delete
4.Search

 creating a binary search tree

o Algorithm for creating BST:


1. Ask for number of nodes (n ) you want in your tree
2. Repeat following steps n times
i) Create new node P
ii) Enter value for P->data
iii) Initialize P->left=P->right=NULL
iv) If root is NULL then insert temp as root node
v) Else if P->data < rot->data then insert p in left subtree
of root
vi) Else if P->data > rot->data then insert p in right
subtree of root

Example of BST creation

Let's see the creation of binary search tree using an example.

Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50

o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert
it as the root of the left subtree, and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it
as the root of the right subtree.

Now, let's see the process of creating the Binary search tree using the
given data element. The process of creating the BST is shown below -

Step 1 - Insert 45.


Step 2 - Insert 15.

As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step 3 - Insert 79.

As 79 is greater than 45, so insert it as the root node of the right subtree.

Step 4 - Insert 90.

90 is greater than 45 and 79, so it will be inserted as the right subtree of


79.

Step 5 - Insert 10.

10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.


Step 6 - Insert 55.

55 is larger than 45 and smaller than 79, so it will be inserted as the left
subtree of 79.

Step 7 - Insert 12.

12 is smaller than 45 and 15 but greater than 10, so it will be inserted as


the right subtree of 10.

Step 8 - Insert 20.

20 is smaller than 45 but greater than 15, so it will be inserted as the right
subtree of 15.
Step 9 - Insert 50.

50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as
a left subtree of 55.

We can perform insert, delete and search operations on the binary search
tree.

 Insertion in Binary Search tree

A new key in BST is always inserted at the leaf. To insert an element in


BST, we have to start searching from the root node; if the node to be
inserted is less than the root node, then search for an empty location in the
left subtree. Else, search for the empty location in the right subtree and
insert the data. Insert in BST is similar to searching, as we always have to
maintain the rule that the left subtree is smaller than the root, and right
subtree is larger than the root.

Algorithm for inserting node temp in BST:


Insert(root , temp)

1. If root is NULL,
Initialize root with temp
2. if temp->data< root->data
Call insert function and insert temp in left subtree
insert(root->left ,temp)
3. if temp->data> root->data
Call insert function and insert temp in right subtree
insert(root->right ,temp)
4. return root

Now, let's see the process of inserting a node into BST using an example.
 Searching in Binary search tree

In Binary search tree, searching a node is easy because elements in BST


are stored in a specific order.

Algorithm for searching target node in a BST:

Search(root, target)

Repeat steps 2 to 5 while root!=NULL

1. compare the element to be searched with the root element of the tree.
2. If root->data is matched with the target element, then return the root
3. If it is not matched, then check whether the item is less than the root
element, if it is smaller than the root element, then move to the left
subtree.
4. If it is larger than the root element, then move to the right subtree.

endwhile
5. If the element is not found then return NULL.

Now, let's understand the searching in binary tree using an example. We


are taking the binary search tree formed above. Suppose we have to find
node 20 from the below tree.

Step1:

Step2:

Step3:
 Deletion in Binary Search tree

In a binary search tree, we must delete a node from the tree by keeping in
mind that the property of BST is not violated. To delete a node from BST,
there are three possible situations occur -

o The node to be deleted is the leaf node, or,


o The node to be deleted has only one child, and,
o The node to be deleted has two children

We will understand the situations listed above in detail.

 When the node to be deleted is the leaf node

It is the simplest case to delete a node in BST. Here, we have to replace


the leaf node with NULL and simply free the allocated space.

We can see the process to delete a leaf node from BST in the below image.
In below image, suppose we have to delete node 90, as the node to be
deleted is a leaf node, so it will be replaced with NULL, and the allocated
space will free.

 When the node to be deleted has only one child

In this case, we have to replace the target node with its child, and then
delete the child node. It means that after replacing the target node with its
child node, the child node will now contain the value to be deleted. So,
we simply have to replace the child node with NULL and free up the
allocated space.
We can see the process of deleting a node with one child from BST in the
below image. In the below image, suppose we have to delete the node 79,
as the node to be deleted has only one child, so it will be replaced with its
child 55.

So, the replaced node 79 will now be a leaf node that can be easily deleted.

When the node to be deleted has two children

This case of deleting a node in BST is a bit complex among other two
cases. In such a case, the steps to be followed are listed as follows -

o First, find the inorder successor of the node to be deleted.


o After that, replace that node with the inorder successor until the
target node is placed at the leaf of tree.
o And at last, replace the node with NULL and free up the allocated
space.

The inorder successor is required when the right child of the node is not
empty. We can obtain the inorder successor by finding the minimum
element in the right child of the node.

We can see the process of deleting a node with two children from BST in
the below image. In the below image, suppose we have to delete node 45
that is the root node, as the node to be deleted has two children, so it will
be replaced with its inorder successor. Now, node 45 will be at the leaf of
the tree so that it can be deleted easily.
Now let's understand how insertion is performed on a binary search tree.

Algorithm to delete node x from BST:

Delete( root, x)

1. Check if root is NULL- print node not found and return


NULL
2. Else if x is less than root->data then give recursive call to
delete function to delete x from left subtree
3. Else if x is greater than root->data then give recursive call
to delete function to delete x from right subtree
4. Else if x is equal to root->data then node to be deleted is
found so do following steps to delete node.

i) If root node has only right child then delete root


node and return root->right as new root
ii) If root node has only left child then delete root
node and return root->left as new root
iii) If root node has both child nodes then find
inorder successor (p) of root node and replace root
with inorder successor p
iv) Call delete function to delete p

5. Return root

o ***Note: Practice writing algorithm and code for all operations of


BST (create, insert, display, search ,delete, height, mirror image of
BST, Find minimum value from BST, count total number of nodes
present in BST etc) ------- (Refer code for practical 3)

The complexity of the Binary Search tree

Let's see the time complexity of the Binary search tree operations. We will
see the time complexity for insertion, deletion, and searching operations
in best case, average case, and worst case.

Time Complexity

Operations Best case Average Worst case


time case time time
complexity complexity complexity
Insertion O(log n) O(log n) O(n)
Deletion O(log n) O(log n) O(n)
Search O(log n) O(log n) O(n)

Where 'n' is the number of nodes in the given tree.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy