SlideShare a Scribd company logo
1 of 9
Download to read offline
How to do the main method for this program?
BinaryNode.java
public class BinaryNode
implements Parent{
/** The item associated with this node. */
private E item;
/** The node at the root of the left subtree. */
private BinaryNode left;
/** The node at the root of the right subtree. */
private BinaryNode right;
/** Put item in a leaf node. */
public BinaryNode(E item) {
this.item = item;
// left and right are set to null by default
}
/** no-argument constructor sets everything to null */
public BinaryNode() {
item = null;
left = null;
right = null;
}
/** Put item in a node with the specified subtrees. */
public BinaryNode(E item, BinaryNode left,
BinaryNode right) {
this.item = item;
this.left = left;
this.right = right;
}
/** Put item in a node with the specified subtrees. */
public BinaryNode getChild(int direction) {
if (direction < 0) {
return left;
} else {
return right;
}
}
/** Return the item associated with this node. */
public E getItem() {
return item;
}
/** Return the root of the left subtree. */
public BinaryNode getLeft() {
return left;
}
/** Return the root of the right subtree. */
public BinaryNode getRight() {
return right;
}
/** Return true if this is a leaf. */
public boolean isLeaf() {
return (left == null) && (right == null);
}
/** Replace the item associated with this node. */
public void setItem(E item) {
this.item = item;
}
/** Replace the left subtree with the one rooted at left. */
public void setLeft(BinaryNode left) {
this.left = left;
}
/** Replace the right subtree with the one rooted at right. */
public void setRight(BinaryNode right) {
this.right = right;
}
public void setChild(int direction, BinaryNode child) {
if (direction < 0) {
left = child;
} else {
right = child;
}
}
/**
* Return the String representation of the tree rooted at this node
* traversed preorder.
**/
public String toStringPreorder() {
String result = "";
result += item;
if (left != null) {
result += left.toStringPreorder();
}
if (right != null) {
result += right.toStringPreorder();
}
return result;
}
/** Return a String representation of the tree rooted at this node
* traversed inorder.
**/
public String toStringInOrder() {
String result = "";
if (left != null) {
result += left.toStringInOrder();
}
result += item;
if (right != null) {
result += right.toStringInOrder();
}
return result;
}
/** Return a String representation of the tree rooted at this node,
* traversed postorder.
**/
public String toStringPostorder() {
String result = "";
if (left != null) {
result += left.toStringPostorder();
}
if (right != null) {
result += right.toStringPostorder();
}
result += item;
return result;
}
/**
* Return a String representation of the tree rooted at this node,
* traversed level order.
**/
public String toStringLevelOrder() {
String result = "";
Queue> q = new ArrayQueue>();
q.add (this);
while (!(q.isEmpty())) {
BinaryNode node = q.remove ();
result += node.item;
if (node.left != null) {
q.add(node.left);
}
if (node.right != null) {
q.add(node.right);
}
}
return result;
}
}
----------------------------------------------------------
BinarySearchTree.java
public class BinarySearchTree>
implements Parent,Set{
/** Root node. */
private BinaryNode root;
/** A BinarySearchTree is initially empty. */
public BinarySearchTree() {
root = null;
}
public void add(E target) {
Parent parent = this;
BinaryNode node = root;
int comparison = 0;
while (node != null) {
comparison = target.compareTo(node.getItem());
if (comparison < 0) { // Go left
parent = node;
node = node.getLeft();
} else if (comparison == 0) { // It's already here
return;
} else {
parent = node;
node = node.getRight();
}
}
parent.setChild(comparison, new BinaryNode(target));
}
public boolean contains(E target) {
BinaryNode node = root;
while (node != null) {
int comparison = target.compareTo(node.getItem());
if (comparison < 0) { // go left
node = node.getLeft();
} else if (comparison == 0) { // found it
return true;
} else {
node = node.getRight();
}
}
return false;
}
public BinaryNode getChild(int direction) {
return root;
}
// Remove method
public void remove(E target) {
Parent parent = this;
BinaryNode node = root;
int direction = 0;
while (node != null) {
int comparison = target.compareTo(node.getItem());
if (comparison < 0) { // Go left
parent = node;
node = node.getLeft();
} else if (comparison == 0) { // Found it
spliceOut(node, parent, direction);
return;
} else { // Go right
parent = node;
node = node.getRight();
}
direction = comparison;
}
}
/**
* Remove the leftmost descendant of nde and return the
* item contained in the removed node.
**/
protected E removeLeftmost(BinaryNode node, Parent parent) {
int direction = 1;
while (node.getLeft() != null) {
parent = node;
direction = -1;
node = node.getLeft();
}
E result = node.getItem();
spliceOut(node, parent, direction);
return result;
}
public void setChild(int direction, BinaryNode child) {
root = child;
}
public int size() {
return size(root);
}
/** Return the size of the subtree rooted at node. */
protected int size(BinaryNode node) {
if (node == null) {
return 0;
} else {
return 1 + size(node.getLeft()) + size(node.getRight());
}
}
/**
* Remove node, which is a child of parent. Direction is positive
* if node is the right child of parent. negative if it is the
* left child.
**/
protected void spliceOut(BinaryNode node,
Parent parent,
int direction) {
if (node.getLeft() == null) {
parent.setChild(direction, node.getRight());
} else if (node.getRight() == null) {
parent.setChild(direction, node.getLeft());
} else {
node.setItem(removeLeftmost(node.getRight(), node));
}
}
}
------------------------------------
public interface Parent {
/**
* Return the left child if direction < 0, or the right child
* otherwise.
**/
public BinaryNode getChild(int direction);
/**
* Replace the specified child of this parent with the new child.
* If direction < 0, replace the left child. Otherwise, replace
* the right child.
**/
public void setChild(int direction, BinaryNode child);
}
------------------------------------------
/** A set of Objects. */
public interface Set {
/** Add target to this Set. */
public void add(E target);
/** Return true if this Set contains target. */
public boolean contains(E target);
/** Remove target from this Set. */
public void remove(E target);
/** Return the number of elements in this Set. */
public int size();
}
Solution
With the given here we could actually perform various functions related to binary search tree. I
am providing few example function calls in the below main function.
public static void main(String args[])
{
BinarySearchTree bst = new BinarySearchTree(); //Initially an empty tree would be created.
System.out.println("Insert elements into the BST");
bst.add(10); //Insert 10 into the tree, 10 would become root
bst.add(8); //Insert 8 into BST, 8 would be inserted as left child of 10
bst.add(12); //Following rules of BST, 12 would be inserted as right child of 12
bst.add(11);
bst.add(15);
bst.add(6);
bst.add(9);
System.out.println("Size of the tree is ",bst.size());
System.out.println("In order traversal of the tree",bst.toStringInOrder());
System.out.println("Does the BST contains 11 ?",bst.contains(11));
bst.remove(6); //Remove 6 from the tree
System.out.println("Size of the tree is ",bst.size());
System.out.println("In order traversal of the tree",bst.toStringInOrder());
//Much more functions to explore
}

More Related Content

Similar to How to do the main method for this programBinaryNode.javapublic.pdf

Given BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfGiven BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdf
Conint29
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
xlynettalampleyxc
 
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
contact32
 
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
annaipowerelectronic
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
archiesgallery
 
In the class LinkedBinarySearchTree implement only the following metho.pdf
In the class LinkedBinarySearchTree implement only the following metho.pdfIn the class LinkedBinarySearchTree implement only the following metho.pdf
In the class LinkedBinarySearchTree implement only the following metho.pdf
sidkucheria
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
The following is to be written in JavaThe following is the BSTree.pdf
The following is to be written in JavaThe following is the BSTree.pdfThe following is to be written in JavaThe following is the BSTree.pdf
The following is to be written in JavaThe following is the BSTree.pdf
eyewatchsystems
 
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
 
usingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfusingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdf
info335653
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
shanki7
 
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- {   privat.pdfpublic class CircularDoublyLinkedList-E- implements List-E- {   privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
ChristopherkUzHunter
 
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
anton291
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
erremmfab
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdf
illyasraja7
 
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
 
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
budbarber38650
 
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
 

Similar to How to do the main method for this programBinaryNode.javapublic.pdf (20)

Given BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdfGiven BinaryNode.javapackage util;import java.util.;T.pdf
Given BinaryNode.javapackage util;import java.util.;T.pdf
 
For the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdfFor the code below complete the preOrder() method so that it perform.pdf
For the code below complete the preOrder() method so that it perform.pdf
 
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
 
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
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
 
In the class LinkedBinarySearchTree implement only the following metho.pdf
In the class LinkedBinarySearchTree implement only the following metho.pdfIn the class LinkedBinarySearchTree implement only the following metho.pdf
In the class LinkedBinarySearchTree implement only the following metho.pdf
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
The following is to be written in JavaThe following is the BSTree.pdf
The following is to be written in JavaThe following is the BSTree.pdfThe following is to be written in JavaThe following is the BSTree.pdf
The following is to be written in JavaThe following is the BSTree.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
 
usingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdfusingpackage util;import java.util.;This class implements.pdf
usingpackage util;import java.util.;This class implements.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- {   privat.pdfpublic class CircularDoublyLinkedList-E- implements List-E- {   privat.pdf
public class CircularDoublyLinkedList-E- implements List-E- { privat.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
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
Given the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.pdfGiven the following codepackage data1;import java.util.;p.pdf
Given the following codepackage data1;import java.util.;p.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
 
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
 
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
 
Binary search tree exact match - illustrated walkthrough
Binary search tree   exact match - illustrated walkthroughBinary search tree   exact match - illustrated walkthrough
Binary search tree exact match - illustrated walkthrough
 

More from feelingcomputors

Identify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdfIdentify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdf
feelingcomputors
 
Explain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdfExplain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdf
feelingcomputors
 
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdfExercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
feelingcomputors
 
describe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdfdescribe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdf
feelingcomputors
 
Android Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdfAndroid Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdf
feelingcomputors
 
Willy owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdfWilly owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdf
feelingcomputors
 
what historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdfwhat historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdf
feelingcomputors
 

More from feelingcomputors (20)

Identify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdfIdentify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdf
 
I have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdf
I have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdfI have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdf
I have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdf
 
How many lexemes does the following Java code containGive the num.pdf
How many lexemes does the following Java code containGive the num.pdfHow many lexemes does the following Java code containGive the num.pdf
How many lexemes does the following Java code containGive the num.pdf
 
How many 5-letter words can be formed from the letters S-W-I-N-G .pdf
How many 5-letter words can be formed from the letters S-W-I-N-G .pdfHow many 5-letter words can be formed from the letters S-W-I-N-G .pdf
How many 5-letter words can be formed from the letters S-W-I-N-G .pdf
 
How can companies and customers become interconnected Recently, the.pdf
How can companies and customers become interconnected Recently, the.pdfHow can companies and customers become interconnected Recently, the.pdf
How can companies and customers become interconnected Recently, the.pdf
 
H0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdf
H0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdfH0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdf
H0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdf
 
Explain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdfExplain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdf
 
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdfExercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
 
describe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdfdescribe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdf
 
Differentiating Events that Occur Before and After Ovulation In the O.pdf
Differentiating Events that Occur Before and After Ovulation In the O.pdfDifferentiating Events that Occur Before and After Ovulation In the O.pdf
Differentiating Events that Occur Before and After Ovulation In the O.pdf
 
Android Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdfAndroid Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdf
 
A man starts walking north at 3 fts from a point P. Five minutes la.pdf
A man starts walking north at 3 fts from a point P. Five minutes la.pdfA man starts walking north at 3 fts from a point P. Five minutes la.pdf
A man starts walking north at 3 fts from a point P. Five minutes la.pdf
 
11. Briefly describe the function of the following in protein synthe.pdf
11. Briefly describe the function of the following in protein synthe.pdf11. Briefly describe the function of the following in protein synthe.pdf
11. Briefly describe the function of the following in protein synthe.pdf
 
You save exist1 the first day of the month, exist2 the second day, ex.pdf
You save exist1 the first day of the month, exist2 the second day, ex.pdfYou save exist1 the first day of the month, exist2 the second day, ex.pdf
You save exist1 the first day of the month, exist2 the second day, ex.pdf
 
Willy owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdfWilly owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdf
 
Which of the following are used as cloning vectors Select all that a.pdf
Which of the following are used as cloning vectors Select all that a.pdfWhich of the following are used as cloning vectors Select all that a.pdf
Which of the following are used as cloning vectors Select all that a.pdf
 
When are bank deposits createdWhen businesses pay wages and salar.pdf
When are bank deposits createdWhen businesses pay wages and salar.pdfWhen are bank deposits createdWhen businesses pay wages and salar.pdf
When are bank deposits createdWhen businesses pay wages and salar.pdf
 
what historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdfwhat historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdf
 
What are some alternate perspectives that HRM professionals may purs.pdf
What are some alternate perspectives that HRM professionals may purs.pdfWhat are some alternate perspectives that HRM professionals may purs.pdf
What are some alternate perspectives that HRM professionals may purs.pdf
 
view the media release by Phillip Lowe, the Governor of the RBA and a.pdf
view the media release by Phillip Lowe, the Governor of the RBA and a.pdfview the media release by Phillip Lowe, the Governor of the RBA and a.pdf
view the media release by Phillip Lowe, the Governor of the RBA and a.pdf
 

Recently uploaded

MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell Tolls
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
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...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 

How to do the main method for this programBinaryNode.javapublic.pdf

  • 1. How to do the main method for this program? BinaryNode.java public class BinaryNode implements Parent{ /** The item associated with this node. */ private E item; /** The node at the root of the left subtree. */ private BinaryNode left; /** The node at the root of the right subtree. */ private BinaryNode right; /** Put item in a leaf node. */ public BinaryNode(E item) { this.item = item; // left and right are set to null by default } /** no-argument constructor sets everything to null */ public BinaryNode() { item = null; left = null; right = null; } /** Put item in a node with the specified subtrees. */ public BinaryNode(E item, BinaryNode left, BinaryNode right) { this.item = item; this.left = left; this.right = right; } /** Put item in a node with the specified subtrees. */ public BinaryNode getChild(int direction) { if (direction < 0) {
  • 2. return left; } else { return right; } } /** Return the item associated with this node. */ public E getItem() { return item; } /** Return the root of the left subtree. */ public BinaryNode getLeft() { return left; } /** Return the root of the right subtree. */ public BinaryNode getRight() { return right; } /** Return true if this is a leaf. */ public boolean isLeaf() { return (left == null) && (right == null); } /** Replace the item associated with this node. */ public void setItem(E item) { this.item = item; } /** Replace the left subtree with the one rooted at left. */ public void setLeft(BinaryNode left) { this.left = left; } /** Replace the right subtree with the one rooted at right. */ public void setRight(BinaryNode right) { this.right = right; } public void setChild(int direction, BinaryNode child) { if (direction < 0) {
  • 3. left = child; } else { right = child; } } /** * Return the String representation of the tree rooted at this node * traversed preorder. **/ public String toStringPreorder() { String result = ""; result += item; if (left != null) { result += left.toStringPreorder(); } if (right != null) { result += right.toStringPreorder(); } return result; } /** Return a String representation of the tree rooted at this node * traversed inorder. **/ public String toStringInOrder() { String result = ""; if (left != null) { result += left.toStringInOrder(); } result += item; if (right != null) { result += right.toStringInOrder(); } return result; }
  • 4. /** Return a String representation of the tree rooted at this node, * traversed postorder. **/ public String toStringPostorder() { String result = ""; if (left != null) { result += left.toStringPostorder(); } if (right != null) { result += right.toStringPostorder(); } result += item; return result; } /** * Return a String representation of the tree rooted at this node, * traversed level order. **/ public String toStringLevelOrder() { String result = ""; Queue> q = new ArrayQueue>(); q.add (this); while (!(q.isEmpty())) { BinaryNode node = q.remove (); result += node.item; if (node.left != null) { q.add(node.left); } if (node.right != null) { q.add(node.right); } } return result; }
  • 5. } ---------------------------------------------------------- BinarySearchTree.java public class BinarySearchTree> implements Parent,Set{ /** Root node. */ private BinaryNode root; /** A BinarySearchTree is initially empty. */ public BinarySearchTree() { root = null; } public void add(E target) { Parent parent = this; BinaryNode node = root; int comparison = 0; while (node != null) { comparison = target.compareTo(node.getItem()); if (comparison < 0) { // Go left parent = node; node = node.getLeft(); } else if (comparison == 0) { // It's already here return; } else { parent = node; node = node.getRight(); } } parent.setChild(comparison, new BinaryNode(target)); } public boolean contains(E target) { BinaryNode node = root; while (node != null) {
  • 6. int comparison = target.compareTo(node.getItem()); if (comparison < 0) { // go left node = node.getLeft(); } else if (comparison == 0) { // found it return true; } else { node = node.getRight(); } } return false; } public BinaryNode getChild(int direction) { return root; } // Remove method public void remove(E target) { Parent parent = this; BinaryNode node = root; int direction = 0; while (node != null) { int comparison = target.compareTo(node.getItem()); if (comparison < 0) { // Go left parent = node; node = node.getLeft(); } else if (comparison == 0) { // Found it spliceOut(node, parent, direction); return; } else { // Go right parent = node; node = node.getRight(); } direction = comparison; } }
  • 7. /** * Remove the leftmost descendant of nde and return the * item contained in the removed node. **/ protected E removeLeftmost(BinaryNode node, Parent parent) { int direction = 1; while (node.getLeft() != null) { parent = node; direction = -1; node = node.getLeft(); } E result = node.getItem(); spliceOut(node, parent, direction); return result; } public void setChild(int direction, BinaryNode child) { root = child; } public int size() { return size(root); } /** Return the size of the subtree rooted at node. */ protected int size(BinaryNode node) { if (node == null) { return 0; } else { return 1 + size(node.getLeft()) + size(node.getRight()); } } /** * Remove node, which is a child of parent. Direction is positive * if node is the right child of parent. negative if it is the * left child.
  • 8. **/ protected void spliceOut(BinaryNode node, Parent parent, int direction) { if (node.getLeft() == null) { parent.setChild(direction, node.getRight()); } else if (node.getRight() == null) { parent.setChild(direction, node.getLeft()); } else { node.setItem(removeLeftmost(node.getRight(), node)); } } } ------------------------------------ public interface Parent { /** * Return the left child if direction < 0, or the right child * otherwise. **/ public BinaryNode getChild(int direction); /** * Replace the specified child of this parent with the new child. * If direction < 0, replace the left child. Otherwise, replace * the right child. **/ public void setChild(int direction, BinaryNode child); } ------------------------------------------ /** A set of Objects. */ public interface Set { /** Add target to this Set. */ public void add(E target); /** Return true if this Set contains target. */ public boolean contains(E target);
  • 9. /** Remove target from this Set. */ public void remove(E target); /** Return the number of elements in this Set. */ public int size(); } Solution With the given here we could actually perform various functions related to binary search tree. I am providing few example function calls in the below main function. public static void main(String args[]) { BinarySearchTree bst = new BinarySearchTree(); //Initially an empty tree would be created. System.out.println("Insert elements into the BST"); bst.add(10); //Insert 10 into the tree, 10 would become root bst.add(8); //Insert 8 into BST, 8 would be inserted as left child of 10 bst.add(12); //Following rules of BST, 12 would be inserted as right child of 12 bst.add(11); bst.add(15); bst.add(6); bst.add(9); System.out.println("Size of the tree is ",bst.size()); System.out.println("In order traversal of the tree",bst.toStringInOrder()); System.out.println("Does the BST contains 11 ?",bst.contains(11)); bst.remove(6); //Remove 6 from the tree System.out.println("Size of the tree is ",bst.size()); System.out.println("In order traversal of the tree",bst.toStringInOrder()); //Much more functions to explore }