SlideShare a Scribd company logo
1 of 27
Lecture No 04
Data Structures
MOHAMMAD NAZIR
Deleting a node in BST
 As is common with many data structures,
the hardest operation is deletion.
 Once we have found the node to be
deleted, we need to consider several
possibilities.
 If the node is a leaf, it can be deleted
immediately.
Deleting a node in BST
 If the node has one child, the node can be
deleted after its parent adjusts a pointer to
bypass the node and connect to inorder
successor.
6
2
4
3
1
8
Deleting a node in BST
 The inorder traversal order has to be
maintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8

Deleting a node in BST
 The inorder traversal order has to be
maintained after the delete.
6
2
4
3
1
8
6
2
4
3
1
8
6
2
31
8
 
Deleting a node in BST
 The complicated case is when the node to
be deleted has both left and right subtrees.
 The strategy is to replace the data of this
node with the smallest data of the right
subtree and recursively delete that node.
Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4
Inorder
successor
Deleting a node in BST
Delete(2): locate inorder successor
6
2
5
3
1
8
4
Inorder
successor
 Inorder successor will be the left-most
node in the right subtree of 2.
 The inorder successor will not have a left
child because if it did, that child would be
the left-most node.
Deleting a node in BST
Delete(2): copy data from inorder successor
6
2
5
3
1
8
4
 6
3
5
3
1
8
4
Deleting a node in BST
Delete(2): remove the inorder successor
6
2
5
3
1
8
4
 6
3
5
3
1
8
4
 6
3
5
3
1
8
4
Deleting a node in BST
Delete(2)
 6
3
5
4
1
8
 6
3
5
3
1
8
4
C++ code for delete
 ‘delete’ is C++ keyword. We will call our
deleteNode routine remove.
 Here is the C++ code for remove.

C++ code for delete
TreeNode<int>* remove(TreeNode<int>* tree, int info)
{
TreeNode<int>* t;
int cmp = info - *(tree->getInfo());
if( cmp < 0 ){
t = remove(tree->getLeft(), info);
tree->setLeft( t );
}
else if( cmp > 0 ){
t = remove(tree->getRight(), info);
tree->setRight( t );
}

C++ code for delete
TreeNode<int>* remove(TreeNode<int>* tree, int info)
{
TreeNode<int>* t;
int cmp = info - *(tree->getInfo());
if( cmp < 0 ){
t = remove(tree->getLeft(), info);
tree->setLeft( t );
}
else if( cmp > 0 ){
t = remove(tree->getRight(), info);
tree->setRight( t );
}

C++ code for delete
TreeNode<int>* remove(TreeNode<int>* tree, int info)
{
TreeNode<int>* t;
int cmp = info - *(tree->getInfo());
if( cmp < 0 ){
t = remove(tree->getLeft(), info);
tree->setLeft( t );
}
else if( cmp > 0 ){
t = remove(tree->getRight(), info);
tree->setRight( t );
}

C++ code for delete
TreeNode<int>* remove(TreeNode<int>* tree, int info)
{
TreeNode<int>* t;
int cmp = info - *(tree->getInfo());
if( cmp < 0 ){
t = remove(tree->getLeft(), info);
tree->setLeft( t );
}
else if( cmp > 0 ){
t = remove(tree->getRight(), info);
tree->setRight( t );
}

C++ code for delete
//two children, replace with inorder successor
else if(tree->getLeft() != NULL
&& tree->getRight() != NULL ){
TreeNode<int>* minNode;
minNode = findMin(tree->getRight());
tree->setInfo( minNode->getInfo() );
t = remove(tree->getRight(),
*(minNode->getInfo()));
tree->setRight( t );
}

C++ code for delete
//two children, replace with inorder successor
else if(tree->getLeft() != NULL
&& tree->getRight() != NULL ){
TreeNode<int>* minNode;
minNode = findMin(tree->getRight());
tree->setInfo( minNode->getInfo() );
t = remove(tree->getRight(),
*(minNode->getInfo()));
tree->setRight( t );
}

C++ code for delete
//two children, replace with inorder successor
else if(tree->getLeft() != NULL
&& tree->getRight() != NULL ){
TreeNode<int>* minNode;
minNode = findMin(tree->getRight());
tree->setInfo( minNode->getInfo() );
t = remove(tree->getRight(),
*(minNode->getInfo()));
tree->setRight( t );
}

C++ code for delete
//two children, replace with inorder successor
else if(tree->getLeft() != NULL
&& tree->getRight() != NULL ){
TreeNode<int>* minNode;
minNode = findMin(tree->getRight());
tree->setInfo( minNode->getInfo() );
t = remove(tree->getRight(),
*(minNode->getInfo()));
tree->setRight( t );
}

C++ code for delete
//two children, replace with inorder successor
else if(tree->getLeft() != NULL
&& tree->getRight() != NULL ){
TreeNode<int>* minNode;
minNode = findMin(tree->getRight());
tree->setInfo( minNode->getInfo() );
t = remove(tree->getRight(),
*(minNode->getInfo()));
tree->setRight( t );
}

C++ code for delete
else { // case 1
TreeNode<int>* nodeToDelete = tree;
if( tree->getLeft() == NULL ) //will handle 0 children
tree = tree->getRight();
else if( tree->getRight() == NULL )
tree = tree->getLeft();
else tree = NULL;
delete nodeToDelete;
}
return tree;
}

C++ code for delete
else { // case 1
TreeNode<int>* nodeToDelete = tree;
if( tree->getLeft() == NULL ) //will handle 0 children
tree = tree->getRight();
else if( tree->getRight() == NULL )
tree = tree->getLeft();
else tree = NULL;
delete nodeToDelete;
}
return tree;
}

C++ code for delete
else { // case 1
TreeNode<int>* nodeToDelete = tree;
if( tree->getLeft() == NULL ) //will handle 0 children
tree = tree->getRight();
else if( tree->getRight() == NULL )
tree = tree->getLeft();
else tree = NULL;
delete nodeToDelete;
}
return tree;
}

C++ code for delete
TreeNode<int>* findMin(TreeNode<int>* tree)
{
if( tree == NULL )
return NULL;
if( tree->getLeft() == NULL )
return tree; // this is it.
return findMin( tree->getLeft() );
}

C++ code for delete
TreeNode<int>* findMin(TreeNode<int>* tree)
{
if( tree == NULL )
return NULL;
if( tree->getLeft() == NULL )
return tree; // this is it.
return findMin( tree->getLeft() );
}

C++ code for delete
TreeNode<int>* findMin(TreeNode<int>* tree)
{
if( tree == NULL )
return NULL;
if( tree->getLeft() == NULL )
return tree; // this is it.
return findMin( tree->getLeft() );
}

More Related Content

What's hot

Mysqlfunctions
MysqlfunctionsMysqlfunctions
MysqlfunctionsN13M
 
Pytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math ImpairedPytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math ImpairedTyrel Denison
 
Bresenham's line drawing algorithm
Bresenham's line drawing algorithmBresenham's line drawing algorithm
Bresenham's line drawing algorithmMani Kanth
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 Design and Develop SQL DDL statements which demonstrate the use of SQL objec... Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...bhavesh lande
 
Ah unit 1 differentiation
Ah unit 1 differentiationAh unit 1 differentiation
Ah unit 1 differentiationsjamaths
 
SciQL, Bridging the Gap between Science and Relational DBMS
SciQL, Bridging the Gap between Science and Relational DBMSSciQL, Bridging the Gap between Science and Relational DBMS
SciQL, Bridging the Gap between Science and Relational DBMSPlanetData Network of Excellence
 
Comparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimizationComparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimizationPranamesh Chakraborty
 
Convolutional Neural Network
Convolutional Neural NetworkConvolutional Neural Network
Convolutional Neural NetworkJun Young Park
 
Application of recursive perturbation approach for multimodal optimization
Application of recursive perturbation approach for multimodal optimizationApplication of recursive perturbation approach for multimodal optimization
Application of recursive perturbation approach for multimodal optimizationPranamesh Chakraborty
 
Matlab ploting
Matlab plotingMatlab ploting
Matlab plotingAmeen San
 
Mysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERTMysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERTHarish Gyanani
 
Introduction to Homomorphic Encryption
Introduction to Homomorphic EncryptionIntroduction to Homomorphic Encryption
Introduction to Homomorphic EncryptionChristoph Matthies
 
Pointer Events in Canvas
Pointer Events in CanvasPointer Events in Canvas
Pointer Events in Canvasdeanhudson
 

What's hot (14)

Mysqlfunctions
MysqlfunctionsMysqlfunctions
Mysqlfunctions
 
Pytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math ImpairedPytorch and Machine Learning for the Math Impaired
Pytorch and Machine Learning for the Math Impaired
 
Bresenham's line drawing algorithm
Bresenham's line drawing algorithmBresenham's line drawing algorithm
Bresenham's line drawing algorithm
 
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 Design and Develop SQL DDL statements which demonstrate the use of SQL objec... Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
Design and Develop SQL DDL statements which demonstrate the use of SQL objec...
 
Ah unit 1 differentiation
Ah unit 1 differentiationAh unit 1 differentiation
Ah unit 1 differentiation
 
SciQL, Bridging the Gap between Science and Relational DBMS
SciQL, Bridging the Gap between Science and Relational DBMSSciQL, Bridging the Gap between Science and Relational DBMS
SciQL, Bridging the Gap between Science and Relational DBMS
 
Comparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimizationComparative study of algorithms of nonlinear optimization
Comparative study of algorithms of nonlinear optimization
 
Convolutional Neural Network
Convolutional Neural NetworkConvolutional Neural Network
Convolutional Neural Network
 
Application of recursive perturbation approach for multimodal optimization
Application of recursive perturbation approach for multimodal optimizationApplication of recursive perturbation approach for multimodal optimization
Application of recursive perturbation approach for multimodal optimization
 
Chapter2
Chapter2Chapter2
Chapter2
 
Matlab ploting
Matlab plotingMatlab ploting
Matlab ploting
 
Mysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERTMysql Functions with examples CBSE INDIA 11th class NCERT
Mysql Functions with examples CBSE INDIA 11th class NCERT
 
Introduction to Homomorphic Encryption
Introduction to Homomorphic EncryptionIntroduction to Homomorphic Encryption
Introduction to Homomorphic Encryption
 
Pointer Events in Canvas
Pointer Events in CanvasPointer Events in Canvas
Pointer Events in Canvas
 

Similar to Data structures lecture 04

computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Deleting a node
computer notes - Deleting a nodecomputer notes - Deleting a node
computer notes - Deleting a nodeecomputernotes
 
Data StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdfData StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdfarkleatheray
 
Solutions manual for calculus an applied approach brief international metric ...
Solutions manual for calculus an applied approach brief international metric ...Solutions manual for calculus an applied approach brief international metric ...
Solutions manual for calculus an applied approach brief international metric ...Larson612
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
"You shall not pass : anti-debug methodics"
"You shall not pass : anti-debug methodics""You shall not pass : anti-debug methodics"
"You shall not pass : anti-debug methodics"ITCP Community
 
Binary search tree
Binary search treeBinary search tree
Binary search treeRacksaviR
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 englishyassminkhaldi1
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 
Introduction to python cheat sheet for all
Introduction to python cheat sheet for allIntroduction to python cheat sheet for all
Introduction to python cheat sheet for allshwetakushwaha45
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdferremmfab
 
To create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdfTo create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdfbhim1213
 
SQL techniques for faster applications
SQL techniques for faster applicationsSQL techniques for faster applications
SQL techniques for faster applicationsConnor McDonald
 
High performance GPU computing with Ruby RubyConf 2017
High performance GPU computing with Ruby  RubyConf 2017High performance GPU computing with Ruby  RubyConf 2017
High performance GPU computing with Ruby RubyConf 2017Prasun Anand
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxnoreendchesterton753
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyKimikazu Kato
 

Similar to Data structures lecture 04 (20)

computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
LEC 6-DS ALGO(updated).pdf
LEC 6-DS  ALGO(updated).pdfLEC 6-DS  ALGO(updated).pdf
LEC 6-DS ALGO(updated).pdf
 
computer notes - Deleting a node
computer notes - Deleting a nodecomputer notes - Deleting a node
computer notes - Deleting a node
 
Data StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdfData StructuresPlease I need help completing this c++ program..pdf
Data StructuresPlease I need help completing this c++ program..pdf
 
Solutions manual for calculus an applied approach brief international metric ...
Solutions manual for calculus an applied approach brief international metric ...Solutions manual for calculus an applied approach brief international metric ...
Solutions manual for calculus an applied approach brief international metric ...
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
Python.pdf
Python.pdfPython.pdf
Python.pdf
 
"You shall not pass : anti-debug methodics"
"You shall not pass : anti-debug methodics""You shall not pass : anti-debug methodics"
"You shall not pass : anti-debug methodics"
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Mementopython3 english
Mementopython3 englishMementopython3 english
Mementopython3 english
 
KScope19 - SQL Features
KScope19 - SQL FeaturesKScope19 - SQL Features
KScope19 - SQL Features
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Introduction to python cheat sheet for all
Introduction to python cheat sheet for allIntroduction to python cheat sheet for all
Introduction to python cheat sheet for all
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
 
To create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdfTo create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdf
 
SQL techniques for faster applications
SQL techniques for faster applicationsSQL techniques for faster applications
SQL techniques for faster applications
 
High performance GPU computing with Ruby RubyConf 2017
High performance GPU computing with Ruby  RubyConf 2017High performance GPU computing with Ruby  RubyConf 2017
High performance GPU computing with Ruby RubyConf 2017
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docx
 
Effective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPyEffective Numerical Computation in NumPy and SciPy
Effective Numerical Computation in NumPy and SciPy
 

More from Nazir Ahmed

Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1Nazir Ahmed
 
Control unit design(1)
Control unit design(1)Control unit design(1)
Control unit design(1)Nazir Ahmed
 
Computer architecture mcq (2)
Computer architecture mcq (2)Computer architecture mcq (2)
Computer architecture mcq (2)Nazir Ahmed
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heapNazir Ahmed
 
Clamper clipper(10)
Clamper clipper(10)Clamper clipper(10)
Clamper clipper(10)Nazir Ahmed
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sqlNazir Ahmed
 
Chapter # 12 er modeling
Chapter # 12 er modelingChapter # 12 er modeling
Chapter # 12 er modelingNazir Ahmed
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-schedulingNazir Ahmed
 

More from Nazir Ahmed (20)

Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1
 
Data structure
Data structureData structure
Data structure
 
Cover letter
Cover letterCover letter
Cover letter
 
Control unit design(1)
Control unit design(1)Control unit design(1)
Control unit design(1)
 
Computer architecture mcq (2)
Computer architecture mcq (2)Computer architecture mcq (2)
Computer architecture mcq (2)
 
Complete binary tree and heap
Complete binary tree and heapComplete binary tree and heap
Complete binary tree and heap
 
Clamper clipper(10)
Clamper clipper(10)Clamper clipper(10)
Clamper clipper(10)
 
Cisc mc68000
Cisc mc68000Cisc mc68000
Cisc mc68000
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter # 12 er modeling
Chapter # 12 er modelingChapter # 12 er modeling
Chapter # 12 er modeling
 
Chapeter 2
Chapeter 2Chapeter 2
Chapeter 2
 
Ch7 1 v1
Ch7 1 v1Ch7 1 v1
Ch7 1 v1
 
Ch07 deadlocks
Ch07 deadlocksCh07 deadlocks
Ch07 deadlocks
 
Ch05 cpu-scheduling
Ch05 cpu-schedulingCh05 cpu-scheduling
Ch05 cpu-scheduling
 
Ch04 threads
Ch04 threadsCh04 threads
Ch04 threads
 
Ch03 processes
Ch03 processesCh03 processes
Ch03 processes
 
Ch1 v1
Ch1 v1Ch1 v1
Ch1 v1
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

Data structures lecture 04

  • 1. Lecture No 04 Data Structures MOHAMMAD NAZIR
  • 2. Deleting a node in BST  As is common with many data structures, the hardest operation is deletion.  Once we have found the node to be deleted, we need to consider several possibilities.  If the node is a leaf, it can be deleted immediately.
  • 3. Deleting a node in BST  If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8
  • 4. Deleting a node in BST  The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 
  • 5. Deleting a node in BST  The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 6 2 31 8  
  • 6. Deleting a node in BST  The complicated case is when the node to be deleted has both left and right subtrees.  The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node.
  • 7. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor
  • 8. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor  Inorder successor will be the left-most node in the right subtree of 2.  The inorder successor will not have a left child because if it did, that child would be the left-most node.
  • 9. Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4
  • 10. Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4
  • 11. Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4
  • 12. C++ code for delete  ‘delete’ is C++ keyword. We will call our deleteNode routine remove.  Here is the C++ code for remove.
  • 13.  C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }
  • 14.  C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }
  • 15.  C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }
  • 16.  C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }
  • 17.  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }
  • 18.  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }
  • 19.  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }
  • 20.  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }
  • 21.  C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }
  • 22.  C++ code for delete else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) //will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }
  • 23.  C++ code for delete else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) //will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }
  • 24.  C++ code for delete else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) //will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }
  • 25.  C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }
  • 26.  C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }
  • 27.  C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }