Implement special cases here:
(define (delete t v)
(let ((path (find t v)))
(cond
(( empty? t ) )
((empty? (bst-root t)) )
((not ( = v (node-value (car path))) )
(( > (node-count (car path)) 1)
decrement count)
((equal? (car path) (bst-root t)) ;node were trying to delete
(cond ; nest conditiona;
( (let-AND right (car path) empty) ; case 0.1
root <- null
((let-root empty, right empty) ; case 1
root <- left car path ; case 1.1
((left-empty, right not empty) ; case 1.2
root <- right car path
(else (delete-node path))
(else (delete-node path))
)
)
)
)
)
)
)
)
)
Special cases
Top-Level Function: Delete Value from BST: (delete t v)
The first parameter t is a reference to a bst, and the second parameter v is a value to find and
delete. Like the insert function, delete will directly resolve a few special cases. For the general
cases, it will call (find t v) to obtain a list of nodes that represent a path from the root to the node to
be deleted, and will then call a helper function (delete-node path) to handle the rest of the work. In
the case of delete the helper function will further rely on secondary helper functions to resolve
cases and subcases.
Deletion logically breaks down into cases based on how many child nodes the node to delete has.
Each of those cases has subcases. The large number of cases implies the need for a
correspondingly larger set of test cases: a test case to trigger each deletion case.
Delete Cases
Before we look at the detailed code for delete in Racket, lets look at the general approach. As for
some of the other functions, it will be convenient to create a top-level function (delete t v) and
several helper functions that manage specific detailed cases. There will be some initial special
cases that should be managed separately, before launching any lower level helper function.
Special cases for (delete t v):
Tree t is empty: end the function because there is nothing to do.
At this point, call (find t v) to create the path from root to node to delete if it is present. Assume the
list returned is bound to a symbol named path. The node of interest will be the first node in the list:
(car path)
By examining node (car path), you can determine if the value to delete is present or not.
Value v is not in the tree: end the function because there is nothing to do.
Value v is in the tree:
o Is the count field > 1? Dont delete the node, simply decrement its count field.
o Is the count field = 1? In this case, we must move forward to actually delete the node.
Is the node to delete the root of the tree?
Does the node have 0 or 1 child nodes? This corresponds to delete cases
0.1, 1.1, and 1.2. Delete the node and update the root of the tree.
o If we reach this pointvalue v is in the tree, count field is 1, node is not the root if it has 0 or 1 child
nodes then the general delete case applies, call (delete-node path)
The remaining delete cases will be covered by the (delete-node path) helper function. Some
general considerations for node n (node to delete), node p (parent of n), and node c (child of n).
Is node n the left or right child of node p?
If node n only has one child, node c, is node c the left child or right child of node n?
If node n has two children, we will use an additional procedure to find the in-order predecessor
of n, node iop. We will locate node iop, physically delete it, and copy its value and count fields
into the original node, node n. This results in a logical but not physical delete of node n. Below is a
detailed enumeration of all delete cases based on child count. Subcases 0.1, 1.1, and 1.2 will be
handled as special cases by the top-level delete function because these cases cause the root of
the tree to change.
Since we are maintaining the result of (find t v) in path we can make the following assumptions for
all remaining cases:
Node to delete: (car path)
Parent of node to delete: (car (cdr path)) = (cadr path)
Child nodes of node to delete, if they exist:
o (node-left(carpath))
o (node-right(carpath))
Path from n to in-order predecessor node: ioppath (only needed if n has two child nodes)
In-order predecessor node: (car ioppath) (only needed if node n has two child nodes)
Dont confuse path (list of nodes from root of tree to n) with ioppath (list of nodes from n to iop)
Case 0: Child Count = 0
Subcase 0.1: parent is null implying node is root (already covered in top-level function) o node
being deleted is root of the tree and has 0 child nodes. Set root to null
Subcase 0.2: node is left child of parent o set left child of parent to null
Subcase 0.3: node is right child of parent
o Set right child of parent to null, root doesnt change
Case 1: Child Count = 1
Subcase 1.1: parent is null and child is left child of node (already covered in top-level) o node is
root ,set root to left child
Subcase 1.2: parent is null and child is right child of node (already covered in top-level) o Node is
root, set root to right child
Subcase 1.3: node is left child of parent and child is left child of node o set left child of parent to
left child of node
Subcase 1.4: node is left child of parent and child is right child of node o set left child of parent to
right child of node
Subcase 1.5: node is right child of parent and child is left child of node o set right child of parent to
left child of node
Subcase 1.6: node is right child of parent and child is right child of node o set right child of parent
to right child of node
The cases and subcases are similar, but still differ in the specifics. Take care to identify the correct
case and respond with the correct implementation for that case. In the above cases, note that the
root is the only node with no parent (if parent of n is null, then n is the root). Also note the deletion
cases that cause a change in the root of the tree (0.1, 1.1, and 1.2) are coded directly in the top-
level function.
Case 2: Child Count = 2
There is no easy way to delete a node with two child nodes. Instead we substitute the original
problem with a problem that is easier to solve:
Locate the in-order predecessor of the node to be deleted.
Move the value and count fields from the in-order predecessor node to the original node,
thereby erasing the previous value and count fields of the original node. This content
substitution achieves a logical removal of the original node.
Physically remove the in-order predecessor node
o Case 2.1: predecessor node is left of parent
o Case 2.2: predecessor node is right of paren

More Related Content

PDF
computer notes - Deleting a node
PPT
Binary search trees (1)
PPTX
Binary Search Tree (BST)
PPT
Binary trees
PPT
Red-Black-Trees-4.ppthkghkfhhfgkgcbjgvnufbu
PPT
BINARY TREE REPRESENTATION.ppt
PDF
Chapter 7 - Binary Search Tree in the context of DSA.pdf
PPT
A Binary Search Tree (BST) is a binary tree where each node stores a key or v...
computer notes - Deleting a node
Binary search trees (1)
Binary Search Tree (BST)
Binary trees
Red-Black-Trees-4.ppthkghkfhhfgkgcbjgvnufbu
BINARY TREE REPRESENTATION.ppt
Chapter 7 - Binary Search Tree in the context of DSA.pdf
A Binary Search Tree (BST) is a binary tree where each node stores a key or v...

Similar to Implement special cases here define delete t v let.pdf (20)

PPTX
Tree data structure.pptx
PPTX
Binary Search Tree.pptx
DOC
2 4 Tree
PDF
binary_trees3
PPTX
Red Black Trees delete BST application .ppt.pptx
PPT
PDF
Trees in Data Structure
PPT
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
PPT
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
DOCX
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
PPTX
Binary search tree deletion
PPT
Binary Search Tree
PDF
LEC 6-DS ALGO(updated).pdf
PPT
PPT
Data Structure: TREES
PDF
C programming. Answer question only in C code Ninth Deletion with B.pdf
PDF
Binary tree
PPT
computer notes - Data Structures - 22
PPTX
Lecture 7 bst
PPTX
Tree data structure.pptx
Binary Search Tree.pptx
2 4 Tree
binary_trees3
Red Black Trees delete BST application .ppt.pptx
Trees in Data Structure
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
(a) There are three ways to traverse a binary tree pre-order, in-or.docx
Binary search tree deletion
Binary Search Tree
LEC 6-DS ALGO(updated).pdf
Data Structure: TREES
C programming. Answer question only in C code Ninth Deletion with B.pdf
Binary tree
computer notes - Data Structures - 22
Lecture 7 bst
Ad

More from ADITIEYEWEAR (20)

PDF
In a study on the retirement savings plans of young Canadian.pdf
PDF
In a spreadsheet decompose the change in the debt ratio fro.pdf
PDF
In a school there are 5 sections of 10th standard with same.pdf
PDF
In a recent year grade 10 Washington State public school st.pdf
PDF
In a recent poll the Gallup Organization found that 45 of.pdf
PDF
In a random sample of UTC students 50 indicated they are b.pdf
PDF
In a previous yeac the weights of the members of the San Fra.pdf
PDF
In a questionnaire for 100 pernens the gender distribution .pdf
PDF
In a normal distribution what is the probability that a ran.pdf
PDF
In 2019 scientists conducted a research study about spicy f.pdf
PDF
In a holiday gathering two families family 1 and family 2 .pdf
PDF
in 2019 facebook Announced is Plan 70 pon out a nea stablec.pdf
PDF
In a city of half a million there are initially 400 cases o.pdf
PDF
In a 10year graph with an explanation of Idaho Panhandle H.pdf
PDF
In 2D let xx1x2 Write down the explicit cubic feature.pdf
PDF
In 2018 Tirana Trucks had a retum on equity of 180 and a.pdf
PDF
In 2021 California passed a law to provide all public schoo.pdf
PDF
import yfinance as yf yfTickerstickersAAPL TSLA.pdf
PDF
In 2020 Natural Selection a nationwide computer dating se.pdf
PDF
In 2017 Americans spent a recordhigh 91 billion on Hallo.pdf
In a study on the retirement savings plans of young Canadian.pdf
In a spreadsheet decompose the change in the debt ratio fro.pdf
In a school there are 5 sections of 10th standard with same.pdf
In a recent year grade 10 Washington State public school st.pdf
In a recent poll the Gallup Organization found that 45 of.pdf
In a random sample of UTC students 50 indicated they are b.pdf
In a previous yeac the weights of the members of the San Fra.pdf
In a questionnaire for 100 pernens the gender distribution .pdf
In a normal distribution what is the probability that a ran.pdf
In 2019 scientists conducted a research study about spicy f.pdf
In a holiday gathering two families family 1 and family 2 .pdf
in 2019 facebook Announced is Plan 70 pon out a nea stablec.pdf
In a city of half a million there are initially 400 cases o.pdf
In a 10year graph with an explanation of Idaho Panhandle H.pdf
In 2D let xx1x2 Write down the explicit cubic feature.pdf
In 2018 Tirana Trucks had a retum on equity of 180 and a.pdf
In 2021 California passed a law to provide all public schoo.pdf
import yfinance as yf yfTickerstickersAAPL TSLA.pdf
In 2020 Natural Selection a nationwide computer dating se.pdf
In 2017 Americans spent a recordhigh 91 billion on Hallo.pdf
Ad

Recently uploaded (20)

PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
Trump Administration's workforce development strategy
PDF
IGGE1 Understanding the Self1234567891011
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
PDF
Uderstanding digital marketing and marketing stratergie for engaging the digi...
PPTX
Virtual and Augmented Reality in Current Scenario
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Cambridge-Practice-Tests-for-IELTS-12.docx
Share_Module_2_Power_conflict_and_negotiation.pptx
Trump Administration's workforce development strategy
IGGE1 Understanding the Self1234567891011
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
1.3 FINAL REVISED K-10 PE and Health CG 2023 Grades 4-10 (1).pdf
Uderstanding digital marketing and marketing stratergie for engaging the digi...
Virtual and Augmented Reality in Current Scenario
A powerpoint presentation on the Revised K-10 Science Shaping Paper
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Weekly quiz Compilation Jan -July 25.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
History, Philosophy and sociology of education (1).pptx
CISA (Certified Information Systems Auditor) Domain-Wise Summary.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes

Implement special cases here define delete t v let.pdf

  • 1. Implement special cases here: (define (delete t v) (let ((path (find t v))) (cond (( empty? t ) ) ((empty? (bst-root t)) ) ((not ( = v (node-value (car path))) ) (( > (node-count (car path)) 1) decrement count) ((equal? (car path) (bst-root t)) ;node were trying to delete (cond ; nest conditiona; ( (let-AND right (car path) empty) ; case 0.1 root <- null ((let-root empty, right empty) ; case 1 root <- left car path ; case 1.1 ((left-empty, right not empty) ; case 1.2 root <- right car path (else (delete-node path)) (else (delete-node path)) ) ) ) ) ) ) ) ) ) Special cases Top-Level Function: Delete Value from BST: (delete t v) The first parameter t is a reference to a bst, and the second parameter v is a value to find and delete. Like the insert function, delete will directly resolve a few special cases. For the general cases, it will call (find t v) to obtain a list of nodes that represent a path from the root to the node to be deleted, and will then call a helper function (delete-node path) to handle the rest of the work. In the case of delete the helper function will further rely on secondary helper functions to resolve cases and subcases. Deletion logically breaks down into cases based on how many child nodes the node to delete has. Each of those cases has subcases. The large number of cases implies the need for a correspondingly larger set of test cases: a test case to trigger each deletion case. Delete Cases Before we look at the detailed code for delete in Racket, lets look at the general approach. As for some of the other functions, it will be convenient to create a top-level function (delete t v) and several helper functions that manage specific detailed cases. There will be some initial special
  • 2. cases that should be managed separately, before launching any lower level helper function. Special cases for (delete t v): Tree t is empty: end the function because there is nothing to do. At this point, call (find t v) to create the path from root to node to delete if it is present. Assume the list returned is bound to a symbol named path. The node of interest will be the first node in the list: (car path) By examining node (car path), you can determine if the value to delete is present or not. Value v is not in the tree: end the function because there is nothing to do. Value v is in the tree: o Is the count field > 1? Dont delete the node, simply decrement its count field. o Is the count field = 1? In this case, we must move forward to actually delete the node. Is the node to delete the root of the tree? Does the node have 0 or 1 child nodes? This corresponds to delete cases 0.1, 1.1, and 1.2. Delete the node and update the root of the tree. o If we reach this pointvalue v is in the tree, count field is 1, node is not the root if it has 0 or 1 child nodes then the general delete case applies, call (delete-node path) The remaining delete cases will be covered by the (delete-node path) helper function. Some general considerations for node n (node to delete), node p (parent of n), and node c (child of n). Is node n the left or right child of node p? If node n only has one child, node c, is node c the left child or right child of node n? If node n has two children, we will use an additional procedure to find the in-order predecessor of n, node iop. We will locate node iop, physically delete it, and copy its value and count fields into the original node, node n. This results in a logical but not physical delete of node n. Below is a detailed enumeration of all delete cases based on child count. Subcases 0.1, 1.1, and 1.2 will be handled as special cases by the top-level delete function because these cases cause the root of the tree to change. Since we are maintaining the result of (find t v) in path we can make the following assumptions for all remaining cases: Node to delete: (car path) Parent of node to delete: (car (cdr path)) = (cadr path) Child nodes of node to delete, if they exist: o (node-left(carpath)) o (node-right(carpath)) Path from n to in-order predecessor node: ioppath (only needed if n has two child nodes) In-order predecessor node: (car ioppath) (only needed if node n has two child nodes) Dont confuse path (list of nodes from root of tree to n) with ioppath (list of nodes from n to iop) Case 0: Child Count = 0 Subcase 0.1: parent is null implying node is root (already covered in top-level function) o node being deleted is root of the tree and has 0 child nodes. Set root to null Subcase 0.2: node is left child of parent o set left child of parent to null Subcase 0.3: node is right child of parent o Set right child of parent to null, root doesnt change
  • 3. Case 1: Child Count = 1 Subcase 1.1: parent is null and child is left child of node (already covered in top-level) o node is root ,set root to left child Subcase 1.2: parent is null and child is right child of node (already covered in top-level) o Node is root, set root to right child Subcase 1.3: node is left child of parent and child is left child of node o set left child of parent to left child of node Subcase 1.4: node is left child of parent and child is right child of node o set left child of parent to right child of node Subcase 1.5: node is right child of parent and child is left child of node o set right child of parent to left child of node Subcase 1.6: node is right child of parent and child is right child of node o set right child of parent to right child of node The cases and subcases are similar, but still differ in the specifics. Take care to identify the correct case and respond with the correct implementation for that case. In the above cases, note that the root is the only node with no parent (if parent of n is null, then n is the root). Also note the deletion cases that cause a change in the root of the tree (0.1, 1.1, and 1.2) are coded directly in the top- level function. Case 2: Child Count = 2 There is no easy way to delete a node with two child nodes. Instead we substitute the original problem with a problem that is easier to solve: Locate the in-order predecessor of the node to be deleted. Move the value and count fields from the in-order predecessor node to the original node, thereby erasing the previous value and count fields of the original node. This content substitution achieves a logical removal of the original node. Physically remove the in-order predecessor node o Case 2.1: predecessor node is left of parent o Case 2.2: predecessor node is right of paren