BIRLA INSTITUTE OF TECHNOLOGY
MESRA,
JAIPUR CAMPUS
TOPIC:- BINARY SEARCH TREE
NAME:- TARUN TIWARI
ROLL NO.:- MCA/25007/18
CLASS:- MCA 2ND SEM.
BINARY SEARCH TREE
• In Binary Search Tree, all the left subtree elements
should be less than root data and all the right
subtree elements should be greater than root data.
This is called binary search tree property.
• This property should be satisfied at every node in
the tree.
Examples
Binary
search trees
Not a binary
search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Data
NODE
LEFT CHILD RIGHT CHILD
Struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
STRUCTURE OF TREE NODE
SEARCHING BINARY SEARCH TREE
Example: Suppose T is the tree being searched:
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we should search in the left
subtree.
• If we are searching for a key > 15, then we should search in the
right subtree.
BINARY SEARCH TREE ALGORITHM
SearchElement (TREE,VAL)
Step 1: If Tree-> DATA=VAL OR TREE=NULL
RETURN TREE
ELSE
IF VAL < TREE -> DATA
RETURN SearchElement(TREE->LEFT,VAL)
ELSE
RETURN SearchElement(TREE->RIGHT,VAL)
[END OF IF]
[END OF IF]
Step 2 : EXIT
INSERTION BINARY SEARCH TREE
• IMPLEMENTING INSERTION RECURSIVELY
BINARY SEARCH TREE INSERTION
ALGORITHM
INSERT (TREE,VAL)
Step 1: IF TREE= NULL
Allocate memory for TREE
SET TREE -> DATA=VAL
SET TREE ->LEFT = TREE->RIGHT =NULL
ELSE
IF VAL < TREE->DATA
INSERT (TREE->LEFT , VAL)
ELSE
INSERT (TREE->RIGHT , VAL)
[END OF IF]
[END OF IF]
Step 2: EXIT
DELETION BINARY SEARCH TREE
Case 1: the node is a leaf Delete it immediately
Case 2: the node has one child
Adjust a pointer from the parent to bypass that
node
Case 3: the node has 2 children
Replace the key of that node with the minimum
element at the right subtree
COMPLEXITY IN BST
Operation Average Worst Case Best
Case
Search O(log n) O(n) O(1)
Insertion O(log n) O(n) O(1)
Deletion O(log n) O(n) O(1)
APPLICATIONS OF BST
• Used in many search applications where data is constantly
entering/leaving, such as the map and set objects in many
languages' libraries.
• Storing a set of names, and being able to lookup based on a prefix
of the name. (Used in internet routers.)
• Storing a path in a graph, and being able to reverse any subsection
of the path in O(log n) time. (Useful in travelling salesman
problems).
• Finding square root of given number
• allows you to do range searches efficiently.
THANK YOU

Binary Search Tree in Data Structure

  • 1.
    BIRLA INSTITUTE OFTECHNOLOGY MESRA, JAIPUR CAMPUS TOPIC:- BINARY SEARCH TREE NAME:- TARUN TIWARI ROLL NO.:- MCA/25007/18 CLASS:- MCA 2ND SEM.
  • 2.
    BINARY SEARCH TREE •In Binary Search Tree, all the left subtree elements should be less than root data and all the right subtree elements should be greater than root data. This is called binary search tree property. • This property should be satisfied at every node in the tree.
  • 3.
    Examples Binary search trees Not abinary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45
  • 4.
    Data NODE LEFT CHILD RIGHTCHILD Struct node { int data; struct node *leftChild; struct node *rightChild; }; STRUCTURE OF TREE NODE
  • 5.
    SEARCHING BINARY SEARCHTREE Example: Suppose T is the tree being searched: • If we are searching for 15, then we are done. • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree.
  • 7.
    BINARY SEARCH TREEALGORITHM SearchElement (TREE,VAL) Step 1: If Tree-> DATA=VAL OR TREE=NULL RETURN TREE ELSE IF VAL < TREE -> DATA RETURN SearchElement(TREE->LEFT,VAL) ELSE RETURN SearchElement(TREE->RIGHT,VAL) [END OF IF] [END OF IF] Step 2 : EXIT
  • 8.
  • 9.
  • 10.
    BINARY SEARCH TREEINSERTION ALGORITHM INSERT (TREE,VAL) Step 1: IF TREE= NULL Allocate memory for TREE SET TREE -> DATA=VAL SET TREE ->LEFT = TREE->RIGHT =NULL ELSE IF VAL < TREE->DATA INSERT (TREE->LEFT , VAL) ELSE INSERT (TREE->RIGHT , VAL) [END OF IF] [END OF IF] Step 2: EXIT
  • 11.
    DELETION BINARY SEARCHTREE Case 1: the node is a leaf Delete it immediately Case 2: the node has one child Adjust a pointer from the parent to bypass that node
  • 12.
    Case 3: thenode has 2 children Replace the key of that node with the minimum element at the right subtree
  • 13.
    COMPLEXITY IN BST OperationAverage Worst Case Best Case Search O(log n) O(n) O(1) Insertion O(log n) O(n) O(1) Deletion O(log n) O(n) O(1)
  • 14.
    APPLICATIONS OF BST •Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries. • Storing a set of names, and being able to lookup based on a prefix of the name. (Used in internet routers.) • Storing a path in a graph, and being able to reverse any subsection of the path in O(log n) time. (Useful in travelling salesman problems). • Finding square root of given number • allows you to do range searches efficiently.
  • 15.