SlideShare a Scribd company logo
Data Structure and
Algorithm (CS-102)
Ashok K Turuk
Binary Search Tree (BST)
Suppose T is a binary tree
Then T is called binary search tree if
each node N of T has the following
property
The value at N is greater than every value
in the left sub-tree of N and is less
than every value in the right sub-tree
of N
For any node y
in this subtree
key(y) < key(x)
x
For any node z
in this subtree
key(z) > key(x)
Binary Search Trees
A binary search tree Not a binary search tree
Binary search trees
Average depth of a node is O(log N);
maximum depth of a node is O(N)
Two binary search trees representing the
same set:
Searching and Inserting in BST
Algorithm to find the location of ITEM in
the BST T or insert ITEM as a new
node in its appropriate place in the tree
[a] Compare ITEM with the root node N
of the tree
(i) If ITEM < N, proceed to the left
child of N
(ii) If ITEM > N, proceed to the right
child of N
Searching and Inserting in BST
Algorithm to find the location of ITEM in
the BST T or insert ITEM as a new node
in its appropriate place in the tree
[b] Repeat Step (a) until one of the following
occurs
(i) We meet a node N such that ITEM = N.
In this case search is successful
(ii) We meet an empty sub-tree, which
indicates that search is unsuccessful and
we insert ITEM in place of empty subtree
Searching BST
• 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.
Insert 40, 60, 50, 33, 55, 11 into an empty
BST
40
1. ITEM = 40
40
60
2. ITEM = 60
40
60
50
3. ITEM = 50
40
60
50
33
4. ITEM = 33
Insert 40, 60, 50, 33, 55, 11 into an empty
BST
40
60
50
33
55
5. ITEM = 55
40
60
50
33
55
11
6. ITEM = 11
Locating an ITEM
A binary search tree T is in memory and
an ITEM of information is given. This
procedure finds the location LOC of
ITEM in T and also the location of the
parent PAR of ITEM.
Locating an ITEM
Three special cases:
[1] LOC == NULL and PAR == NULL, tree is
empty
[2] LOC  and PAR == NULL, ITEM is the
root of T
[3] LOC == NULL and PAR  NULL, ITEM
is not in T and can be added to T as
child of the node N with location PAR.
[1] [Tree empty ?]
If ROOT == NULL, then
Set LOC = NULL, PAR = NULL,
Exit
[2] [ITEM at root ?]
If ROOT->INFO == ITEM, then
Set LOC = ROOT, PAR = NULL, Exit
[3] [Initialize pointer PTR and SAVE]
If ITEM < ROOT->INFO then
Set PTR = ROOT->LEFT, SAVE = ROOT
Else
Set PTR = ROOT->RIGHT, SAVE =ROOT
FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
[4] Repeat Step 5 and 6 while PTR  NULL
[5] [ITEM Found ?]
If ITEM == PTR->INFO, then
Set LOC = PTR, PAR = SAVE, Exit
[6] If ITEM < PTR->INFO, then
Set SAVE = PTR, PTR = PTR->LEFT
Else
Set SAVE = PTR, PTR = PTR->RIGHT
[7] [Search Unsuccessful]
Set LOC = NULL, PAR = SAVE
[8] Exit
A binary search Tree T is in memory and
an ITEM of information is given.
Algorithm to find the location LOC of
ITEM in T or adds ITEM as a new node
in T at location LOC.
[1] Call FIND(INFO,
LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
[2] If LOC  NULL, then Exit
[3] [Copy the ITEM into the node NEW]
(a) Create a node NEW
(b) NEW->INFO = ITEM
( c) Set LOC = NEW, NEW->LEFT =
NULL, NEW->RIGHT = NULL
[4] [Add ITEM to tree]
If PAR = NULL
Set ROOT = NEW
Else
If ITEM < PAR->INFO, then
Set PAR->LEFT = NEW
Else
Set PAR->RIGHT = NEW
[5] Exit
Binary Search Tree
Insert Algorithm
• If value we want to insert < key of
current node, we have to go to the left
subtree
• Otherwise we have to go to the right
subtree
• If the current node is empty (not
existing) create a node with the value
we are inserting and place it here.
Binary Search Tree
For example, inserting ’15’ into the BST?
Deletion from BST
T is a binary tree. Delete an ITEM from
the tree T
Deleting a node N from tree depends
primarily on the number of children of
node N
Deletion
There are three cases in deletion
Case 1. N has no children. N is deleted from
the T by replacing the location of N in the
parent node of N by null pointer
40
60
50
33
55
11
ITEM = 11
Deletion
Case 2. N has exactly one children. N is deleted
from the T by simply replacing the location of
N in the parent node of N by the location of
the only child of N
40
60
50
33
55
11
ITEM = 50
Deletion
Case 2.
40
60
55
33
11
ITEM = 50
Deletion
Case 3. N has two children. Let S(N) denote the
in-order successor of N. Then N is deleted
from the T by first deleting S(N) from T and
then replacing node N in T by the node S(N)
40
60
50
33
55
11
ITEM = 40
Deletion
Case 3.
40
60
55
33
11
Delete 50
50
60
55
33
11
Replace 40 by 50
Types of Binary Trees
• Degenerate – only one child
• Balanced – mostly two children
• Complete – always two children
Degenerate
binary tree
Balanced binary
tree
Complete
binary tree
Binary Trees Properties
• Degenerate
– Height = O(n) for n
nodes
– Similar to linear list
• Balanced
– Height = O( log(n) )
for n nodes
– Useful for searches
Degenerate
binary tree
Balanced binary
tree
Binary Search Trees
• Key property
– Value at node
• Smaller values in left subtree
• Larger values in right subtree
– Example
• X > Y
• X < Z
Y
X
Z
Binary Search Trees
• Examples
Binary search
trees
Non-binary search
tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
Example Binary Searches
• Find ( 2 )
5
10
30
2 25 45
5
10
30
2
25
45
10 > 2, left
5 > 2, left
2 = 2, found
5 > 2, left
2 = 2, found
Example Binary Searches
• Find ( 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
Binary Search Properties
• Time of search
– Proportional to height of tree
– Balanced binary tree
• O( log(n) ) time
– Degenerate tree
• O( n ) time
• Like searching linked list / unsorted array
• Requires
– Ability to compare key values
Binary Search Tree Construction
• How to build & maintain binary trees?
– Insertion
– Deletion
• Maintain key property (invariant)
– Smaller values in left subtree
– Larger values in right subtree
Binary Search Tree – Insertion
• Algorithm
1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left
subtree for Y
4. If X > Y, insert new leaf X as new right
subtree for Y
• Observations
– O( log(n) ) operation for balanced tree
– Insertions may unbalance tree
Example Insertion
• Insert ( 20 )
5
10
30
2 25 45
10 < 20, right
30 > 20, left
25 > 20, left
Insert 20 on left
20
Binary Search Tree – Deletion
• Algorithm
1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
Observation
– O( log(n) ) operation for balanced tree
– Deletions may unbalance tree
Example Deletion (Leaf)
• Delete ( 25 )
5
10
30
2 25 45
10 < 25, right
30 > 25, left
25 = 25,
delete
5
10
30
2 45

More Related Content

What's hot

Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
karthikeyanC40
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Abhishek L.R
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
chauhankapil
 
Splay Tree
Splay TreeSplay Tree
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Meghaj Mallick
 
2-3 Tree
2-3 Tree2-3 Tree
Binary search trees
Binary search treesBinary search trees
Binary search trees
Dhananjaysinh Jhala
 
Tree
TreeTree
Tree
bhumish
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Shell sorting
Shell sortingShell sorting
Shell sorting
TUC
 
Trees
TreesTrees
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
Daniyal Khan
 

What's hot (20)

Binary tree
Binary  treeBinary  tree
Binary tree
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
2-3 Tree
2-3 Tree2-3 Tree
2-3 Tree
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Tree
TreeTree
Tree
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
Trees
TreesTrees
Trees
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 

Viewers also liked

Polish nootation
Polish nootationPolish nootation
Polish nootation
Srikanth Chennupati
 
C applications
C applicationsC applications
C applications
faizankhan260690
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
Akhil Ahuja
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
sethuraman R
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
almario1988
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
mua99
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
Syed Mustafa
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
Haqnawaz Ch
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
Then Murugeshwari
 

Viewers also liked (11)

Polish nootation
Polish nootationPolish nootation
Polish nootation
 
C applications
C applicationsC applications
C applications
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Infix to postfix conversion
Infix to postfix conversionInfix to postfix conversion
Infix to postfix conversion
 

Similar to 8.binry search tree

Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
BST.pdf
BST.pdfBST.pdf
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
ssuser034ce1
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
Eleti Ganapathi
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
kalyanineve
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
SSE_AndyLi
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Zafar Ayub
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
invertis university
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
ssuser034ce1
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
Knoldus Inc.
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
AdityaK92
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
Meetu Maltiar
 
Lec8
Lec8Lec8
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
RedHeart11
 
CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3
SIMONTHOMAS S
 
DS - BST
DS - BSTDS - BST
DS - BST
MythiliMurugan3
 
BST_algorithm.pptx
BST_algorithm.pptxBST_algorithm.pptx
BST_algorithm.pptx
MahdiHasan86
 
binary search tree
binary search treebinary search tree
binary search tree
Shankar Bishnoi
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
SwatiHans10
 

Similar to 8.binry search tree (20)

Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
BST.pdf
BST.pdfBST.pdf
BST.pdf
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
Lec8
Lec8Lec8
Lec8
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3
 
DS - BST
DS - BSTDS - BST
DS - BST
 
BST_algorithm.pptx
BST_algorithm.pptxBST_algorithm.pptx
BST_algorithm.pptx
 
binary search tree
binary search treebinary search tree
binary search tree
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 

More from Chandan Singh

Fundamental of Tissue engineering
Fundamental of Tissue engineeringFundamental of Tissue engineering
Fundamental of Tissue engineering
Chandan Singh
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instruments
Chandan Singh
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instruments
Chandan Singh
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instruments
Chandan Singh
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instruments
Chandan Singh
 
Electrical Measurement & Instruments
Electrical Measurement & InstrumentsElectrical Measurement & Instruments
Electrical Measurement & Instruments
Chandan Singh
 
Static characteristics of Instruments
Static characteristics of InstrumentsStatic characteristics of Instruments
Static characteristics of Instruments
Chandan Singh
 
Resistance measurement
Resistance measurementResistance measurement
Resistance measurement
Chandan Singh
 
Introduction to sensors
Introduction to sensorsIntroduction to sensors
Introduction to sensors
Chandan Singh
 
Energy meter
Energy meterEnergy meter
Energy meter
Chandan Singh
 
Classification (Analog instruments)
 Classification (Analog instruments) Classification (Analog instruments)
Classification (Analog instruments)
Chandan Singh
 
AC Bridges: Balance Condition
AC Bridges: Balance ConditionAC Bridges: Balance Condition
AC Bridges: Balance Condition
Chandan Singh
 
Cathode Ray Osciloscope
Cathode Ray OsciloscopeCathode Ray Osciloscope
Cathode Ray Osciloscope
Chandan Singh
 
Instrument transformer CT & PT
Instrument transformer CT & PTInstrument transformer CT & PT
Instrument transformer CT & PT
Chandan Singh
 
Megohmmeter
MegohmmeterMegohmmeter
Megohmmeter
Chandan Singh
 
Moving Iron
Moving IronMoving Iron
Moving Iron
Chandan Singh
 
Permanent Magnet Moving Coil
Permanent Magnet Moving Coil Permanent Magnet Moving Coil
Permanent Magnet Moving Coil
Chandan Singh
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search tree
Chandan Singh
 
9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
Chandan Singh
 
7.tree
7.tree7.tree

More from Chandan Singh (20)

Fundamental of Tissue engineering
Fundamental of Tissue engineeringFundamental of Tissue engineering
Fundamental of Tissue engineering
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instruments
 
Resistance Measurement instruments
Resistance Measurement instrumentsResistance Measurement instruments
Resistance Measurement instruments
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instruments
 
Moving iron (MI) instruments
Moving iron (MI) instrumentsMoving iron (MI) instruments
Moving iron (MI) instruments
 
Electrical Measurement & Instruments
Electrical Measurement & InstrumentsElectrical Measurement & Instruments
Electrical Measurement & Instruments
 
Static characteristics of Instruments
Static characteristics of InstrumentsStatic characteristics of Instruments
Static characteristics of Instruments
 
Resistance measurement
Resistance measurementResistance measurement
Resistance measurement
 
Introduction to sensors
Introduction to sensorsIntroduction to sensors
Introduction to sensors
 
Energy meter
Energy meterEnergy meter
Energy meter
 
Classification (Analog instruments)
 Classification (Analog instruments) Classification (Analog instruments)
Classification (Analog instruments)
 
AC Bridges: Balance Condition
AC Bridges: Balance ConditionAC Bridges: Balance Condition
AC Bridges: Balance Condition
 
Cathode Ray Osciloscope
Cathode Ray OsciloscopeCathode Ray Osciloscope
Cathode Ray Osciloscope
 
Instrument transformer CT & PT
Instrument transformer CT & PTInstrument transformer CT & PT
Instrument transformer CT & PT
 
Megohmmeter
MegohmmeterMegohmmeter
Megohmmeter
 
Moving Iron
Moving IronMoving Iron
Moving Iron
 
Permanent Magnet Moving Coil
Permanent Magnet Moving Coil Permanent Magnet Moving Coil
Permanent Magnet Moving Coil
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search tree
 
9.bst(contd.) avl tree
9.bst(contd.) avl tree9.bst(contd.) avl tree
9.bst(contd.) avl tree
 
7.tree
7.tree7.tree
7.tree
 

Recently uploaded

Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
PreethaV16
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
sachin chaurasia
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
ijseajournal
 
2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt
abdatawakjira
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
Kamal Acharya
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
Kamal Acharya
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
cannyengineerings
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 

Recently uploaded (20)

Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
OOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming languageOOPS_Lab_Manual - programs using C++ programming language
OOPS_Lab_Manual - programs using C++ programming language
 
Mechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineeringMechatronics material . Mechanical engineering
Mechatronics material . Mechanical engineering
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
 
2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt2. protection of river banks and bed erosion protection works.ppt
2. protection of river banks and bed erosion protection works.ppt
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 

8.binry search tree

  • 1. Data Structure and Algorithm (CS-102) Ashok K Turuk
  • 2. Binary Search Tree (BST) Suppose T is a binary tree Then T is called binary search tree if each node N of T has the following property The value at N is greater than every value in the left sub-tree of N and is less than every value in the right sub-tree of N
  • 3. For any node y in this subtree key(y) < key(x) x For any node z in this subtree key(z) > key(x)
  • 4. Binary Search Trees A binary search tree Not a binary search tree
  • 5. Binary search trees Average depth of a node is O(log N); maximum depth of a node is O(N) Two binary search trees representing the same set:
  • 6. Searching and Inserting in BST Algorithm to find the location of ITEM in the BST T or insert ITEM as a new node in its appropriate place in the tree [a] Compare ITEM with the root node N of the tree (i) If ITEM < N, proceed to the left child of N (ii) If ITEM > N, proceed to the right child of N
  • 7. Searching and Inserting in BST Algorithm to find the location of ITEM in the BST T or insert ITEM as a new node in its appropriate place in the tree [b] Repeat Step (a) until one of the following occurs (i) We meet a node N such that ITEM = N. In this case search is successful (ii) We meet an empty sub-tree, which indicates that search is unsuccessful and we insert ITEM in place of empty subtree
  • 8. Searching BST • 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.
  • 9.
  • 10. Insert 40, 60, 50, 33, 55, 11 into an empty BST 40 1. ITEM = 40 40 60 2. ITEM = 60 40 60 50 3. ITEM = 50 40 60 50 33 4. ITEM = 33
  • 11. Insert 40, 60, 50, 33, 55, 11 into an empty BST 40 60 50 33 55 5. ITEM = 55 40 60 50 33 55 11 6. ITEM = 11
  • 12. Locating an ITEM A binary search tree T is in memory and an ITEM of information is given. This procedure finds the location LOC of ITEM in T and also the location of the parent PAR of ITEM.
  • 13. Locating an ITEM Three special cases: [1] LOC == NULL and PAR == NULL, tree is empty [2] LOC  and PAR == NULL, ITEM is the root of T [3] LOC == NULL and PAR  NULL, ITEM is not in T and can be added to T as child of the node N with location PAR.
  • 14. [1] [Tree empty ?] If ROOT == NULL, then Set LOC = NULL, PAR = NULL, Exit [2] [ITEM at root ?] If ROOT->INFO == ITEM, then Set LOC = ROOT, PAR = NULL, Exit [3] [Initialize pointer PTR and SAVE] If ITEM < ROOT->INFO then Set PTR = ROOT->LEFT, SAVE = ROOT Else Set PTR = ROOT->RIGHT, SAVE =ROOT FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
  • 15. [4] Repeat Step 5 and 6 while PTR  NULL [5] [ITEM Found ?] If ITEM == PTR->INFO, then Set LOC = PTR, PAR = SAVE, Exit [6] If ITEM < PTR->INFO, then Set SAVE = PTR, PTR = PTR->LEFT Else Set SAVE = PTR, PTR = PTR->RIGHT [7] [Search Unsuccessful] Set LOC = NULL, PAR = SAVE [8] Exit
  • 16. A binary search Tree T is in memory and an ITEM of information is given. Algorithm to find the location LOC of ITEM in T or adds ITEM as a new node in T at location LOC.
  • 17. [1] Call FIND(INFO, LEFT,RIGHT,ROOT,ITEM,LOC,PAR) [2] If LOC  NULL, then Exit [3] [Copy the ITEM into the node NEW] (a) Create a node NEW (b) NEW->INFO = ITEM ( c) Set LOC = NEW, NEW->LEFT = NULL, NEW->RIGHT = NULL
  • 18. [4] [Add ITEM to tree] If PAR = NULL Set ROOT = NEW Else If ITEM < PAR->INFO, then Set PAR->LEFT = NEW Else Set PAR->RIGHT = NEW [5] Exit
  • 19. Binary Search Tree Insert Algorithm • If value we want to insert < key of current node, we have to go to the left subtree • Otherwise we have to go to the right subtree • If the current node is empty (not existing) create a node with the value we are inserting and place it here.
  • 20. Binary Search Tree For example, inserting ’15’ into the BST?
  • 21.
  • 22. Deletion from BST T is a binary tree. Delete an ITEM from the tree T Deleting a node N from tree depends primarily on the number of children of node N
  • 23. Deletion There are three cases in deletion Case 1. N has no children. N is deleted from the T by replacing the location of N in the parent node of N by null pointer 40 60 50 33 55 11 ITEM = 11
  • 24. Deletion Case 2. N has exactly one children. N is deleted from the T by simply replacing the location of N in the parent node of N by the location of the only child of N 40 60 50 33 55 11 ITEM = 50
  • 26. Deletion Case 3. N has two children. Let S(N) denote the in-order successor of N. Then N is deleted from the T by first deleting S(N) from T and then replacing node N in T by the node S(N) 40 60 50 33 55 11 ITEM = 40
  • 28. Types of Binary Trees • Degenerate – only one child • Balanced – mostly two children • Complete – always two children Degenerate binary tree Balanced binary tree Complete binary tree
  • 29. Binary Trees Properties • Degenerate – Height = O(n) for n nodes – Similar to linear list • Balanced – Height = O( log(n) ) for n nodes – Useful for searches Degenerate binary tree Balanced binary tree
  • 30. Binary Search Trees • Key property – Value at node • Smaller values in left subtree • Larger values in right subtree – Example • X > Y • X < Z Y X Z
  • 31. Binary Search Trees • Examples Binary search trees Non-binary search tree 5 10 30 2 25 45 5 10 45 2 25 30 5 10 30 2 25 45
  • 32. Example Binary Searches • Find ( 2 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found
  • 33. Example Binary Searches • Find ( 25 ) 5 10 30 2 25 45 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found
  • 34. Binary Search Properties • Time of search – Proportional to height of tree – Balanced binary tree • O( log(n) ) time – Degenerate tree • O( n ) time • Like searching linked list / unsorted array • Requires – Ability to compare key values
  • 35. Binary Search Tree Construction • How to build & maintain binary trees? – Insertion – Deletion • Maintain key property (invariant) – Smaller values in left subtree – Larger values in right subtree
  • 36. Binary Search Tree – Insertion • Algorithm 1. Perform search for value X 2. Search will end at node Y (if X not in tree) 3. If X < Y, insert new leaf X as new left subtree for Y 4. If X > Y, insert new leaf X as new right subtree for Y • Observations – O( log(n) ) operation for balanced tree – Insertions may unbalance tree
  • 37. Example Insertion • Insert ( 20 ) 5 10 30 2 25 45 10 < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 20
  • 38. Binary Search Tree – Deletion • Algorithm 1. Perform search for value X 2. If X is a leaf, delete X 3. Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation – O( log(n) ) operation for balanced tree – Deletions may unbalance tree
  • 39. Example Deletion (Leaf) • Delete ( 25 ) 5 10 30 2 25 45 10 < 25, right 30 > 25, left 25 = 25, delete 5 10 30 2 45