SlideShare a Scribd company logo
1 of 7
Download to read offline
To create a node for a binary search tree (BTNode, ) and to create a binary search tree of
comparable objects (Tree BTNode BTNode -data:E -left: BTNode -right: BTNode +BTNode
(newData:E, newLeft: BTNode, newRight: BTNode) +getData():E +getLeft():BTNode
+getRight():BTNode +getRightMostData():E +inOrderPrint():void +removeRightmost():
BTNode +setData(newData:E):void +setLeft(newleft: BTNode):void +setRight(newRight:
BTNode):void needed for remove needed for remove Tree for Lab 7 Tree -root: 8TNode -
numltems:int +Tree() +add(element:E):void +remove(element:E):boolean + remove
+size():int + printTree():void
Solution
//BTNode.java
public class BTNode
{
private E data;
private BTNode left;
private BTNode right;
public BTNode(E newData, BTNode newLeft, BTNode newRight)
{
data = newData;
left = newLeft;
right = newRight;
}//end constructor
/**
* Returns the data
* @return The data
*/
public E getData()
{
return data;
}//end getData
/**
* Returns the left link
* @return the left link
*/
public BTNode getLeft()
{
return left;
}//end getLeft
/**
* Returns the right link
* @return the right link
*/
public BTNode getRight()
{
return right;
}//end getRight
/**
* Prints the tree in order from left to right.
*/
public void inOrderPrint()
{
if (left != null){
left.inOrderPrint();
}//end left
System.out.println(data);
if(right != null){
right.inOrderPrint();
}//end right
}//end inOrderPrint
/**
* Sets the data in the node
* @param newData The new data to be passed
*/
public void setData(E newData)
{
data = newData;
}//end setData
/**
* Sets a new link to left
* @param newLeft the new link to go left
*/
public void setLeft(BTNode newLeft)
{
left = newLeft;
}//end setLeft
/**
* Sets a new link to the right
* @param newRight the new link to the right
*/
public void setRight(BTNode newRight)
{
right = newRight;
}//end setRight
public E getRightMostData()
{
if(right == null){
return data;
} else {
return right.getRightMostData();
}//end right == null if-else
}//end getRightMostData
public BTNode removeRightmost()
{
if (right == null){
return left;
} else {
right = right.removeRightmost();
return this;
}//end if right == null if=-else
}//end removeRightMost
}//end BTNode
=====================================================================
==
//Tree.java
public class Tree>
{
private BTNode root;
private int numItems;
/**
* No-arg constructor
*/
public Tree()
{
root = null;
numItems = 0;
}//end constructor
public void add(E element)
{
if (root == null) {
root = new BTNode(element, null, null);
}else{
BTNode cursor = root;
boolean done = false;
while(!done){
if (element.compareTo(cursor.getData()) <= 0){//cursor.getData().compareTo(element) > 0){
if (cursor.getLeft() == null) {
cursor.setLeft(new BTNode(element,null,null));
done = true;
} else {
cursor = cursor.getLeft();
} //end left is null if-else
}else{
if(cursor.getRight() == null){
cursor.setRight(new BTNode(element, null, null));
done = true;
}else{
cursor = cursor.getRight();
}//end cursor.getRight if-else
}//end compareto if-else
}//end while
}//end root ==null if-else
numItems++;
}//end add
/**
* Get the number of elements in the tree
* @return The size of the tree
*/
public int size()
{
return numItems;
}//end size
/**
* Print the tree in order
*/
public void printTree()
{
if (root == null){
System.out.println("This tree is empty");
}else{
root.inOrderPrint();
}//end if-else
}//end inOrderPrint
public boolean remove(E target)
{
BTNode cursor = root;
BTNode parentOfCursor = null;
boolean found = false;
while (cursor != null && !found){
if (target.equals(cursor.getData())){
found = true;
}else{
if(target.compareTo(cursor.getData()) <= 0){
parentOfCursor = cursor;
cursor = cursor.getLeft();
}else{
parentOfCursor = cursor;
cursor = cursor.getRight();
}//end target.compareTo if-else
}//end target.equals if-else
}//end cursor && found while
if (cursor != null){
if (cursor.getLeft() == null && cursor == root){
root = root.getRight();
} else if (cursor.getLeft() == null && cursor!= root){
if (cursor == parentOfCursor.getLeft()){
parentOfCursor.setLeft(cursor.getRight());
} else {
parentOfCursor.setRight(cursor.getRight());
}//end cursor ==parentOfCursor.setLEft if-else
} else{
cursor.setData(cursor.getLeft().getRightMostData());
cursor.setLeft(cursor.getLeft().removeRightmost());
} //end cursor == root if-else
}//end found if
return found;
}
}//end Tree

More Related Content

Similar to To create a node for a binary search tree (BTNode, ) and to create a .pdf

computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docxtodd991
 
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdfCreate a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdfshyamsunder1211
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdframbagra74
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfsivakumar19831
 
Write a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdfWrite a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdfinfo824691
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
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
 
JavaCreate a java application using the code example in the follo.pdf
JavaCreate a java application using the code example in the follo.pdfJavaCreate a java application using the code example in the follo.pdf
JavaCreate a java application using the code example in the follo.pdfambersushil
 
Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfcontact32
 
In c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdfIn c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdfezycolours78
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfADITIEYEWEAR
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docxJakeT2gGrayp
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfpublic class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfannaipowerelectronic
 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.pdfsyedabdul78662
 
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.pdffeelingcomputors
 
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.pdfpetercoiffeur18
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxbudbarber38650
 

Similar to To create a node for a binary search tree (BTNode, ) and to create a .pdf (20)

computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
10 Solution#include-iostream-h- #include-conio-h- #include-process-h-.docx
 
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdfCreate a class BinarySearchTree- A class that implements the ADT binar.pdf
Create a class BinarySearchTree- A class that implements the ADT binar.pdf
 
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
5. Design and implement a method contains 2 for BinarySearchTree, fu.pdf
 
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdfObjective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
Objective Binary Search Tree traversal (2 points)Use traversal.pp.pdf
 
Write a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdfWrite a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
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
 
JavaCreate a java application using the code example in the follo.pdf
JavaCreate a java application using the code example in the follo.pdfJavaCreate a java application using the code example in the follo.pdf
JavaCreate a java application using the code example in the follo.pdf
 
Tugas struktur data terakhir_pohonBiner
Tugas struktur data terakhir_pohonBinerTugas struktur data terakhir_pohonBiner
Tugas struktur data terakhir_pohonBiner
 
Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
In c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdfIn c++ format please, for each function in the code, using the comme.pdf
In c++ format please, for each function in the code, using the comme.pdf
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
Please finish everything marked TODO BinaryNode-java public class Bi.docx
Please finish everything marked TODO   BinaryNode-java public class Bi.docxPlease finish everything marked TODO   BinaryNode-java public class Bi.docx
Please finish everything marked TODO BinaryNode-java public class Bi.docx
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfpublic class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdf
 
package lab7 public class SetOperations public static.pdf
package lab7     public class SetOperations  public static.pdfpackage lab7     public class SetOperations  public static.pdf
package lab7 public class SetOperations public static.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
 
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
 
For this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docxFor this project, write a program that stores integers in a binary.docx
For this project, write a program that stores integers in a binary.docx
 

More from bhim1213

In 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdfIn 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdfbhim1213
 
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdfIf A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdfbhim1213
 
How do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdfHow do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdfbhim1213
 
How many times will the following code print Welcome to Java in.pdf
How many times will the following code print Welcome to Java  in.pdfHow many times will the following code print Welcome to Java  in.pdf
How many times will the following code print Welcome to Java in.pdfbhim1213
 
From your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdfFrom your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdfbhim1213
 
Forms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdfForms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdfbhim1213
 
During DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdfDuring DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdfbhim1213
 
Disease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdfDisease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdfbhim1213
 
Darwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdfDarwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdfbhim1213
 
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdfDefine toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdfbhim1213
 
A system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdfA system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdfbhim1213
 
All content is made permanent for future use. All content will be lo.pdf
All content is made permanent for future use.  All content will be lo.pdfAll content is made permanent for future use.  All content will be lo.pdf
All content is made permanent for future use. All content will be lo.pdfbhim1213
 
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdfa. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdfbhim1213
 
Biochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdfBiochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdfbhim1213
 
A process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdfA process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdfbhim1213
 
Which of the following statements is (are) trueI Cash flow is a.pdf
Which of the following statements is (are) trueI  Cash flow is a.pdfWhich of the following statements is (are) trueI  Cash flow is a.pdf
Which of the following statements is (are) trueI Cash flow is a.pdfbhim1213
 
What is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdfWhat is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdfbhim1213
 
What is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdfWhat is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdfbhim1213
 
What is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdfWhat is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdfbhim1213
 
What do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdfWhat do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdfbhim1213
 

More from bhim1213 (20)

In 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdfIn 1998, Congress passed legislation concerning shifting the burden .pdf
In 1998, Congress passed legislation concerning shifting the burden .pdf
 
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdfIf A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
If A denotes some event, what does A denote If P(A)equals=0.997, wh.pdf
 
How do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdfHow do neutrophils find and get to an infectionSolutionNeutro.pdf
How do neutrophils find and get to an infectionSolutionNeutro.pdf
 
How many times will the following code print Welcome to Java in.pdf
How many times will the following code print Welcome to Java  in.pdfHow many times will the following code print Welcome to Java  in.pdf
How many times will the following code print Welcome to Java in.pdf
 
From your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdfFrom your own words not copy past from internet please Talked about.pdf
From your own words not copy past from internet please Talked about.pdf
 
Forms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdfForms an important part of the subunits of the ribosome. The _ are sp.pdf
Forms an important part of the subunits of the ribosome. The _ are sp.pdf
 
During DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdfDuring DNA replication, a series of steps are required to ensure tha.pdf
During DNA replication, a series of steps are required to ensure tha.pdf
 
Disease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdfDisease X occurs when someone has the xx genotype. However, only 20 .pdf
Disease X occurs when someone has the xx genotype. However, only 20 .pdf
 
Darwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdfDarwins Theory had five main tenets (or components), what are they.pdf
Darwins Theory had five main tenets (or components), what are they.pdf
 
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdfDefine toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
Define toxic shock syndrome (TSS) impetigo MRSASolutionAnswer.pdf
 
A system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdfA system was set up in parallel with two components, A and B. The pr.pdf
A system was set up in parallel with two components, A and B. The pr.pdf
 
All content is made permanent for future use. All content will be lo.pdf
All content is made permanent for future use.  All content will be lo.pdfAll content is made permanent for future use.  All content will be lo.pdf
All content is made permanent for future use. All content will be lo.pdf
 
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdfa. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
a. A waitress is serving 5 people at a table. She has the 5 dishes t.pdf
 
Biochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdfBiochemistry homework Focus concept ripening fruit and the activity.pdf
Biochemistry homework Focus concept ripening fruit and the activity.pdf
 
A process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdfA process that has its representative PCB in a device queue is in th.pdf
A process that has its representative PCB in a device queue is in th.pdf
 
Which of the following statements is (are) trueI Cash flow is a.pdf
Which of the following statements is (are) trueI  Cash flow is a.pdfWhich of the following statements is (are) trueI  Cash flow is a.pdf
Which of the following statements is (are) trueI Cash flow is a.pdf
 
What is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdfWhat is the relationship of the epithelium of the epidermis to the co.pdf
What is the relationship of the epithelium of the epidermis to the co.pdf
 
What is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdfWhat is a linked listWhat is a linked lists general syntaxCan .pdf
What is a linked listWhat is a linked lists general syntaxCan .pdf
 
What is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdfWhat is the output of running class GenericMethodDemo of the followi.pdf
What is the output of running class GenericMethodDemo of the followi.pdf
 
What do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdfWhat do you think are the roles of professional societies in contemp.pdf
What do you think are the roles of professional societies in contemp.pdf
 

Recently uploaded

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 

Recently uploaded (20)

ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 

To create a node for a binary search tree (BTNode, ) and to create a .pdf

  • 1. To create a node for a binary search tree (BTNode, ) and to create a binary search tree of comparable objects (Tree BTNode BTNode -data:E -left: BTNode -right: BTNode +BTNode (newData:E, newLeft: BTNode, newRight: BTNode) +getData():E +getLeft():BTNode +getRight():BTNode +getRightMostData():E +inOrderPrint():void +removeRightmost(): BTNode +setData(newData:E):void +setLeft(newleft: BTNode):void +setRight(newRight: BTNode):void needed for remove needed for remove Tree for Lab 7 Tree -root: 8TNode - numltems:int +Tree() +add(element:E):void +remove(element:E):boolean + remove +size():int + printTree():void Solution //BTNode.java public class BTNode { private E data; private BTNode left; private BTNode right; public BTNode(E newData, BTNode newLeft, BTNode newRight) { data = newData; left = newLeft; right = newRight; }//end constructor /** * Returns the data * @return The data */ public E getData() { return data; }//end getData /**
  • 2. * Returns the left link * @return the left link */ public BTNode getLeft() { return left; }//end getLeft /** * Returns the right link * @return the right link */ public BTNode getRight() { return right; }//end getRight /** * Prints the tree in order from left to right. */ public void inOrderPrint() { if (left != null){ left.inOrderPrint(); }//end left System.out.println(data); if(right != null){ right.inOrderPrint(); }//end right }//end inOrderPrint /** * Sets the data in the node
  • 3. * @param newData The new data to be passed */ public void setData(E newData) { data = newData; }//end setData /** * Sets a new link to left * @param newLeft the new link to go left */ public void setLeft(BTNode newLeft) { left = newLeft; }//end setLeft /** * Sets a new link to the right * @param newRight the new link to the right */ public void setRight(BTNode newRight) { right = newRight; }//end setRight public E getRightMostData() { if(right == null){ return data; } else { return right.getRightMostData(); }//end right == null if-else }//end getRightMostData public BTNode removeRightmost() {
  • 4. if (right == null){ return left; } else { right = right.removeRightmost(); return this; }//end if right == null if=-else }//end removeRightMost }//end BTNode ===================================================================== == //Tree.java public class Tree> { private BTNode root; private int numItems; /** * No-arg constructor */ public Tree() { root = null; numItems = 0; }//end constructor public void add(E element) { if (root == null) { root = new BTNode(element, null, null); }else{ BTNode cursor = root; boolean done = false; while(!done){ if (element.compareTo(cursor.getData()) <= 0){//cursor.getData().compareTo(element) > 0){
  • 5. if (cursor.getLeft() == null) { cursor.setLeft(new BTNode(element,null,null)); done = true; } else { cursor = cursor.getLeft(); } //end left is null if-else }else{ if(cursor.getRight() == null){ cursor.setRight(new BTNode(element, null, null)); done = true; }else{ cursor = cursor.getRight(); }//end cursor.getRight if-else }//end compareto if-else }//end while }//end root ==null if-else numItems++; }//end add /** * Get the number of elements in the tree * @return The size of the tree */ public int size() { return numItems; }//end size /** * Print the tree in order */ public void printTree() { if (root == null){ System.out.println("This tree is empty"); }else{
  • 6. root.inOrderPrint(); }//end if-else }//end inOrderPrint public boolean remove(E target) { BTNode cursor = root; BTNode parentOfCursor = null; boolean found = false; while (cursor != null && !found){ if (target.equals(cursor.getData())){ found = true; }else{ if(target.compareTo(cursor.getData()) <= 0){ parentOfCursor = cursor; cursor = cursor.getLeft(); }else{ parentOfCursor = cursor; cursor = cursor.getRight(); }//end target.compareTo if-else }//end target.equals if-else }//end cursor && found while if (cursor != null){ if (cursor.getLeft() == null && cursor == root){ root = root.getRight(); } else if (cursor.getLeft() == null && cursor!= root){ if (cursor == parentOfCursor.getLeft()){ parentOfCursor.setLeft(cursor.getRight()); } else { parentOfCursor.setRight(cursor.getRight()); }//end cursor ==parentOfCursor.setLEft if-else } else{ cursor.setData(cursor.getLeft().getRightMostData()); cursor.setLeft(cursor.getLeft().removeRightmost());
  • 7. } //end cursor == root if-else }//end found if return found; } }//end Tree