SlideShare a Scribd company logo
1 of 5
Download to read offline
In the class LinkedBinarySearchTree implement only the following methods:
removeMax
findMin
findMax
import java.util.NoSuchElementException;
public class LinkedBinarySearchTree<E extends Comparable<E>>
extends LinkedBinaryTree<E>
implements BinarySearchTree<E> {
/**
* Creates an empty binary search tree.
*/
public LinkedBinarySearchTree() {
super();
}
/**
* Creates a binary search with the specified element as its root.
*
* @param element the element that will be the root of the new binary
* search tree
*/
public LinkedBinarySearchTree(E element) {
super(element);
}
@Override
public void add(E element) {
if (isEmpty()) { // Add as root of new tree
root = new LinkedBinaryTreeNode(element);
modCount += 1;
} else if (element.compareTo(root.element) < 0) { // Add to left subtree
if (root.left == null) {
root.left = new LinkedBinaryTreeNode(element);
} else {
add(element, root.left);
}
modCount += 1;
} else if (0 < element.compareTo(root.element)){ // Add to right subtree
if (root.right == null) {
root.right = new LinkedBinaryTreeNode(element);
} else {
add(element, root.right);
}
modCount += 1;
} else { // Element found in tree. Do not add to tree.
}
}
private void add(E element, LinkedBinaryTreeNode node) {
if (element.compareTo(node.element) < 0) { // Add to left subtree
if (node.left == null) {
node.left = new LinkedBinaryTreeNode(element);
} else {
add(element, node.left);
}
} else if (0 < element.compareTo(node.element)) { // Add to right subtree
if (node.right == null) {
node.right = new LinkedBinaryTreeNode(element);
} else {
add(element, node.right);
}
} else { // Element found in tree. Do not add to tree.
}
}
@Override
public E remove(E targetElement) {
if (isEmpty()) {
throw new NoSuchElementException("LinkedBinarySearchTree");
}
E result = null;
LinkedBinaryTreeNode parent = null;
if (targetElement.equals(root.element)) { // Target element found
result = root.element;
LinkedBinaryTreeNode temp = replacement(root);
if (temp == null) {
root = null;
} else {
root.element = temp.element;
root.right = temp.right;
root.left = temp.left;
}
modCount -= 1;
} else { // Target element not found
parent = root;
if (targetElement.compareTo(root.element) < 0) {
result = removeElement(targetElement, root.left, parent);
} else {
result = removeElement(targetElement, root.right, parent);
}
}
return result;
}
private E removeElement(E targetElement,
LinkedBinaryTreeNode node, LinkedBinaryTreeNode parent) {
if (node == null) {
throw new NoSuchElementException("LinkedBinarySearchTree");
}
E result = null;
if (targetElement.equals(node.element)) { // Target element found.
result = node.element;
LinkedBinaryTreeNode temp = replacement(node);
if (parent.right == node) {
parent.right = temp;
} else {
parent.left = temp;
}
modCount -= 1;
} else { // Target element not found
parent = node;
if (targetElement.compareTo(node.element) < 0) { // Look in left subtree
result = removeElement(targetElement, node.left, parent);
} else { // Look in right subtree
result = removeElement(targetElement, node.right, parent);
}
}
return result;
}
private LinkedBinaryTreeNode replacement(LinkedBinaryTreeNode node) {
LinkedBinaryTreeNode result = null;
if ((node.left == null) && (node.right == null)) { // No children
result = null;
} else if ((node.left != null) && (node.right == null)) { // Left child only
result = node.left;
} else if ((node.left == null) && (node.right != null)) { // Right child only
result = node.right;
} else { // Two children
// Find leftmost descendant of right subtree
LinkedBinaryTreeNode parent = node;
LinkedBinaryTreeNode cursor = node.right;
while (cursor.left != null) {
parent = cursor;
cursor = cursor.left;
}
// Restructure the tree
cursor.left = node.left;
if (node.right != cursor) {
parent.left = cursor.right;
cursor.right = node.right;
}
result = cursor;
}
return result;
}
@Override
public E removeMin() {
if (isEmpty()) {
throw new IllegalStateException("LinkedBinarySearchTree");
}
E result = null;
if (root.left == null) { // Minimum element is the root
result = root.element;
root = root.right;
} else { // Minimum element is in the left subtree
// Find leftmost descendant of left subtree
LinkedBinaryTreeNode parent = root;
LinkedBinaryTreeNode cursor = root.left;
while (cursor.left != null) {
parent = cursor;
cursor = cursor.left;
}
result = cursor.element;
// Restructure the tree
parent.left = cursor.right;
}
modCount -= 1;
return result;
}
@Override
public E removeMax() {
// To be implemented.
}
@Override
public E findMin() {
// To be implemented.
}
@Override
public E findMax() {
// To be implemented.
}
}

More Related Content

Similar to In the class LinkedBinarySearchTree implement only the following metho.pdf

package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
aptind
 
There is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdfThere is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdf
Dhanrajsolanki2091
 
How to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdfHow to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
feelingcomputors
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
mumnesh
 
Adding elements public void add(String element) { For fi.pdf
 Adding elements public void add(String element) {  For fi.pdf Adding elements public void add(String element) {  For fi.pdf
Adding elements public void add(String element) { For fi.pdf
aparnacollection
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
xlynettalampleyxc
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdf
wasemanivytreenrco51
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
petercoiffeur18
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
sauravmanwanicp
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
federaleyecare
 
Complete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdfComplete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdf
abbecindia
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
farrahkur54
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
birajdar2
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
VictorzH8Bondx
 

Similar to In the class LinkedBinarySearchTree implement only the following metho.pdf (20)

package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
There is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdfThere is BinarySearchTree class. When removing a node from a BST, we.pdf
There is BinarySearchTree class. When removing a node from a BST, we.pdf
 
How to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdfHow to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
Adding elements public void add(String element) { For fi.pdf
 Adding elements public void add(String element) {  For fi.pdf Adding elements public void add(String element) {  For fi.pdf
Adding elements public void add(String element) { For fi.pdf
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
 
On the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdfOn the code which has a class in which I implementing a binary tree .pdf
On the code which has a class in which I implementing a binary tree .pdf
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
 
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
1)(JAVA) Extend the Binary Search Tree ADT to include a public metho.pdf
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
Create an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdfCreate an implementation of a binary tree using the recursive appr.pdf
Create an implementation of a binary tree using the recursive appr.pdf
 
Complete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdfComplete the class ArraySet1java which implements the SetA.pdf
Complete the class ArraySet1java which implements the SetA.pdf
 
Please find my implementationpublic KeyedItem remove() { DO.pdf
Please find my implementationpublic KeyedItem remove() { DO.pdfPlease find my implementationpublic KeyedItem remove() { DO.pdf
Please find my implementationpublic KeyedItem remove() { DO.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
Once you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docxOnce you have all the structures working as intended- it is time to co.docx
Once you have all the structures working as intended- it is time to co.docx
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 

More from sidkucheria

In the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdf
In the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdfIn the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdf
In the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdf
sidkucheria
 
In Table 19-1 are listed the average distance from the Sun- r- in Astr.pdf
In Table 19-1 are listed the average distance from the Sun- r- in Astr.pdfIn Table 19-1 are listed the average distance from the Sun- r- in Astr.pdf
In Table 19-1 are listed the average distance from the Sun- r- in Astr.pdf
sidkucheria
 
In spite of the contribution of plasmids to the spread of antibiotic r.pdf
In spite of the contribution of plasmids to the spread of antibiotic r.pdfIn spite of the contribution of plasmids to the spread of antibiotic r.pdf
In spite of the contribution of plasmids to the spread of antibiotic r.pdf
sidkucheria
 

More from sidkucheria (20)

In the below figure- the net flux of CO2 from the oceans to the atmosp.pdf
In the below figure- the net flux of CO2 from the oceans to the atmosp.pdfIn the below figure- the net flux of CO2 from the oceans to the atmosp.pdf
In the below figure- the net flux of CO2 from the oceans to the atmosp.pdf
 
In Southern blotting- DNA fragments must be nitrocellulose- before blo.pdf
In Southern blotting- DNA fragments must be nitrocellulose- before blo.pdfIn Southern blotting- DNA fragments must be nitrocellulose- before blo.pdf
In Southern blotting- DNA fragments must be nitrocellulose- before blo.pdf
 
In the biased coin problem- given a sequence of toss samples Head- Tai.pdf
In the biased coin problem- given a sequence of toss samples Head- Tai.pdfIn the biased coin problem- given a sequence of toss samples Head- Tai.pdf
In the biased coin problem- given a sequence of toss samples Head- Tai.pdf
 
In the article by Siedner and Kraemer- the article describes a delay i.pdf
In the article by Siedner and Kraemer- the article describes a delay i.pdfIn the article by Siedner and Kraemer- the article describes a delay i.pdf
In the article by Siedner and Kraemer- the article describes a delay i.pdf
 
In the accompanying figure- which section of the graph illustrates the.pdf
In the accompanying figure- which section of the graph illustrates the.pdfIn the accompanying figure- which section of the graph illustrates the.pdf
In the accompanying figure- which section of the graph illustrates the.pdf
 
In the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdf
In the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdfIn the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdf
In the 14th century- the bubonic plague killed more than 2-3 of Europe (1).pdf
 
In the 1970s- the United States was the largest producer of steel in t.pdf
In the 1970s- the United States was the largest producer of steel in t.pdfIn the 1970s- the United States was the largest producer of steel in t.pdf
In the 1970s- the United States was the largest producer of steel in t.pdf
 
In terms of epigenetic regulation of gene expression- acetylated histo.pdf
In terms of epigenetic regulation of gene expression- acetylated histo.pdfIn terms of epigenetic regulation of gene expression- acetylated histo.pdf
In terms of epigenetic regulation of gene expression- acetylated histo.pdf
 
In the 19th century- cavalries were still an important part of the Eur.pdf
In the 19th century- cavalries were still an important part of the Eur.pdfIn the 19th century- cavalries were still an important part of the Eur.pdf
In the 19th century- cavalries were still an important part of the Eur.pdf
 
In Table 19-1 are listed the average distance from the Sun- r- in Astr.pdf
In Table 19-1 are listed the average distance from the Sun- r- in Astr.pdfIn Table 19-1 are listed the average distance from the Sun- r- in Astr.pdf
In Table 19-1 are listed the average distance from the Sun- r- in Astr.pdf
 
In t- s --ana halnw the rell (formed element) indicated by letter A is.pdf
In t- s --ana halnw the rell (formed element) indicated by letter A is.pdfIn t- s --ana halnw the rell (formed element) indicated by letter A is.pdf
In t- s --ana halnw the rell (formed element) indicated by letter A is.pdf
 
In spite of the contribution of plasmids to the spread of antibiotic r.pdf
In spite of the contribution of plasmids to the spread of antibiotic r.pdfIn spite of the contribution of plasmids to the spread of antibiotic r.pdf
In spite of the contribution of plasmids to the spread of antibiotic r.pdf
 
In some goats- the presence of horns is produced by an autosomal gene.pdf
In some goats- the presence of horns is produced by an autosomal gene.pdfIn some goats- the presence of horns is produced by an autosomal gene.pdf
In some goats- the presence of horns is produced by an autosomal gene.pdf
 
In sexual reproduction- (select answer 1) produce genetically diverse.pdf
In sexual reproduction- (select answer 1) produce genetically diverse.pdfIn sexual reproduction- (select answer 1) produce genetically diverse.pdf
In sexual reproduction- (select answer 1) produce genetically diverse.pdf
 
In response to Covid- starting in early 2020- the U-S- government ____.pdf
In response to Covid- starting in early 2020- the U-S- government ____.pdfIn response to Covid- starting in early 2020- the U-S- government ____.pdf
In response to Covid- starting in early 2020- the U-S- government ____.pdf
 
In R code- for n N- derive the following limit value with detailed pr.pdf
In R code- for n  N- derive the following limit value with detailed pr.pdfIn R code- for n  N- derive the following limit value with detailed pr.pdf
In R code- for n N- derive the following limit value with detailed pr.pdf
 
IN PYTHON PLEASE! Create a script that- when run-checks every second i.pdf
IN PYTHON PLEASE! Create a script that- when run-checks every second i.pdfIN PYTHON PLEASE! Create a script that- when run-checks every second i.pdf
IN PYTHON PLEASE! Create a script that- when run-checks every second i.pdf
 
In prokaryotes some DNA molecules are in the form of Looped linear DNA.pdf
In prokaryotes some DNA molecules are in the form of Looped linear DNA.pdfIn prokaryotes some DNA molecules are in the form of Looped linear DNA.pdf
In prokaryotes some DNA molecules are in the form of Looped linear DNA.pdf
 
In project management- the known steps are anticipated- known steps ar.pdf
In project management- the known steps are anticipated- known steps ar.pdfIn project management- the known steps are anticipated- known steps ar.pdf
In project management- the known steps are anticipated- known steps ar.pdf
 
In processing a prefix expression- which part of the process was recur.pdf
In processing a prefix expression- which part of the process was recur.pdfIn processing a prefix expression- which part of the process was recur.pdf
In processing a prefix expression- which part of the process was recur.pdf
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 

In the class LinkedBinarySearchTree implement only the following metho.pdf

  • 1. In the class LinkedBinarySearchTree implement only the following methods: removeMax findMin findMax import java.util.NoSuchElementException; public class LinkedBinarySearchTree<E extends Comparable<E>> extends LinkedBinaryTree<E> implements BinarySearchTree<E> { /** * Creates an empty binary search tree. */ public LinkedBinarySearchTree() { super(); } /** * Creates a binary search with the specified element as its root. * * @param element the element that will be the root of the new binary * search tree */ public LinkedBinarySearchTree(E element) { super(element); } @Override public void add(E element) { if (isEmpty()) { // Add as root of new tree root = new LinkedBinaryTreeNode(element); modCount += 1; } else if (element.compareTo(root.element) < 0) { // Add to left subtree if (root.left == null) { root.left = new LinkedBinaryTreeNode(element); } else { add(element, root.left); } modCount += 1; } else if (0 < element.compareTo(root.element)){ // Add to right subtree if (root.right == null) { root.right = new LinkedBinaryTreeNode(element); } else { add(element, root.right); }
  • 2. modCount += 1; } else { // Element found in tree. Do not add to tree. } } private void add(E element, LinkedBinaryTreeNode node) { if (element.compareTo(node.element) < 0) { // Add to left subtree if (node.left == null) { node.left = new LinkedBinaryTreeNode(element); } else { add(element, node.left); } } else if (0 < element.compareTo(node.element)) { // Add to right subtree if (node.right == null) { node.right = new LinkedBinaryTreeNode(element); } else { add(element, node.right); } } else { // Element found in tree. Do not add to tree. } } @Override public E remove(E targetElement) { if (isEmpty()) { throw new NoSuchElementException("LinkedBinarySearchTree"); } E result = null; LinkedBinaryTreeNode parent = null; if (targetElement.equals(root.element)) { // Target element found result = root.element; LinkedBinaryTreeNode temp = replacement(root); if (temp == null) { root = null; } else { root.element = temp.element; root.right = temp.right; root.left = temp.left; } modCount -= 1; } else { // Target element not found parent = root; if (targetElement.compareTo(root.element) < 0) { result = removeElement(targetElement, root.left, parent); } else {
  • 3. result = removeElement(targetElement, root.right, parent); } } return result; } private E removeElement(E targetElement, LinkedBinaryTreeNode node, LinkedBinaryTreeNode parent) { if (node == null) { throw new NoSuchElementException("LinkedBinarySearchTree"); } E result = null; if (targetElement.equals(node.element)) { // Target element found. result = node.element; LinkedBinaryTreeNode temp = replacement(node); if (parent.right == node) { parent.right = temp; } else { parent.left = temp; } modCount -= 1; } else { // Target element not found parent = node; if (targetElement.compareTo(node.element) < 0) { // Look in left subtree result = removeElement(targetElement, node.left, parent); } else { // Look in right subtree result = removeElement(targetElement, node.right, parent); } } return result; } private LinkedBinaryTreeNode replacement(LinkedBinaryTreeNode node) { LinkedBinaryTreeNode result = null; if ((node.left == null) && (node.right == null)) { // No children result = null; } else if ((node.left != null) && (node.right == null)) { // Left child only result = node.left; } else if ((node.left == null) && (node.right != null)) { // Right child only result = node.right; } else { // Two children
  • 4. // Find leftmost descendant of right subtree LinkedBinaryTreeNode parent = node; LinkedBinaryTreeNode cursor = node.right; while (cursor.left != null) { parent = cursor; cursor = cursor.left; } // Restructure the tree cursor.left = node.left; if (node.right != cursor) { parent.left = cursor.right; cursor.right = node.right; } result = cursor; } return result; } @Override public E removeMin() { if (isEmpty()) { throw new IllegalStateException("LinkedBinarySearchTree"); } E result = null; if (root.left == null) { // Minimum element is the root result = root.element; root = root.right; } else { // Minimum element is in the left subtree // Find leftmost descendant of left subtree LinkedBinaryTreeNode parent = root; LinkedBinaryTreeNode cursor = root.left; while (cursor.left != null) { parent = cursor; cursor = cursor.left; } result = cursor.element; // Restructure the tree parent.left = cursor.right; }
  • 5. modCount -= 1; return result; } @Override public E removeMax() { // To be implemented. } @Override public E findMin() { // To be implemented. } @Override public E findMax() { // To be implemented. } }