Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

IT122

BINARY SEARCH T REE


Joie Ann M. Mac
Objectives:
• To learn about Binary Search Tree
• To know its different types
• To know its different operations
What is Binary Search Tree?
• A binary tree is a set of finite nodes that can be empty or
may contain several elements. A node is made up of three
entities. A value with two pointers on the left and right. The
root node is the parent component on each subtree.
• It can also be considered as the topmost node in a tree. The
nodes attached to the parent element are referred to as
children. Leaf nodes, on the other hand, are the base
elements in a binary tree.
What is Binary Search Tree?
Types of binary search trees
The various types of binary trees
include:
Complete binary tree: All levels of
the tree are filled and the root key
has a sub-tree that contains two or
no nodes.
Types of binary search trees
The various types of binary trees
include:
Balanced binary tree: The leaf nodes
are not far from the root which is
more of a relative metric. The nodes
can be more than a single level in a
tree. A balanced tree is quite efficient
when searching, inserting, and
deleting components.
Types of binary search trees
The various types of binary trees
include:
Full binary tree: It contains an equal
number of nodes in each subtree
except for the leaf nodes.
What is Binary Search Tree?
A Binary Search Tree is a special type of 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.
There must be no duplicate nodes.
Insertion in
BST:

• Consider the
following BST and
the value = 40 to
be added.
Insertion in
BST:

• Initially, 40 is less than


100. So move to the left
subtree.
• Now, 40 is greater than
20. So move to the right
subtree.
• Now we reach the leaf
node 30. As 40 is greater
than 30, create right child
of 30 and insert the value
40
Insertion in
BST:

• The final BST after insertion will


look like the above image
Search Operation

• The algorithm depends on the property of BST that if each left subtree has values
below root and each right subtree has values above the root.
Search Operation

• The algorithm depends on the property of BST that if each left subtree has values
below root and each right subtree has values above the root.
Deletion Operation

There are three cases for deleting a node from a binary search tree.
Case I
• In the first case, the node to be deleted is the leaf node. In such a case, simply delete the
node from the tree.
Deletion Operation

Case II
• In the second case, the node to be deleted lies has a single child node. In such a case
follow the steps below:
1. Replace that node with its child node.
2. Remove the child node from its original position.
Deletion Operation

6 is to be deleted
Deletion Operation

copy the value of its child to the node and delete the child
Deletion Operation

Final tree
Deletion Operation

Case III
In the third case, the node to be deleted has two children. In such
a case follow the steps below:
1.Get the inorder successor of that node.
2.Replace the node with the inorder successor.
3.Remove the inorder successor from its original position.
Deletion Operation

3 is to be deleted
Deletion Operation

Copy the value of the inorder successor (4) to the node


Deletion Operation

Delete the inorder successor

You might also like