SlideShare a Scribd company logo
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.pdfConint29
 
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.pdfxlynettalampleyxc
 
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
 
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
 
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 .pdfarchiesgallery
 
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.pdfsidkucheria
 
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.pdfarchgeetsenterprises
 
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.pdfeyewatchsystems
 
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.docxfarrahkur54
 
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.pdfinfo335653
 
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-------------.pdfshanki7
 
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.pdfChristopherkUzHunter
 
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
 
create a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdfcreate a binary search tree from an empty one by adding the key valu.pdf
create a binary search tree from an empty one by adding the key valu.pdferremmfab
 
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.docxVictorXUQGloverl
 
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.pdfillyasraja7
 
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.pdfarrowmobile
 
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
 
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.pdfaptind
 
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 walkthroughYoshi Watanabe
 

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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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 .pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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 .pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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,.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 
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.pdffeelingcomputors
 

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

Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointELaRue0
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesTechSoup
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryEugene Lysak
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resourcesdimpy50
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17Celine George
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptxmansk2
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 

Recently uploaded (20)

Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
Mattingly "AI & Prompt Design: Limitations and Solutions with LLMs"
 
The Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. HenryThe Last Leaf, a short story by O. Henry
The Last Leaf, a short story by O. Henry
 
Benefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational ResourcesBenefits and Challenges of Using Open Educational Resources
Benefits and Challenges of Using Open Educational Resources
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx2024_Student Session 2_ Set Plan Preparation.pptx
2024_Student Session 2_ Set Plan Preparation.pptx
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 

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 }