The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 337 Weeks Ago, 1 Day Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Displaying sorted Node list on a GUI. I have already created all the code and methods, i just need some help displaying the output of the Nodes when the perform sort button is pressed. currentlypackage Trees;
import java.util.*;
import java.lang.*;
//creates a generic binary search tree class
public class BinarySearchTree<A extends Comparable<A>> {
//the root of the node, which is the middle value
Node<A> root;
//this constructor will add a node
public void addNode(A userNumber){
//creates a new node
Node<A> newNode = new Node<A>(userNumber);
//if the root node has not be given a value, the new node will become the root
if(root == null){
root = newNode;
}//end if root == null
else{
//this node will become the parent of the new node
Node<A> parent;
//this node will temporarily become the root
Node<A> focusNode = root;
while(true){
parent = focusNode;
if(focusNode.number.compareTo(userNumber) < 0){
//makes the focus node the left child, because the left child has a value
//smaller than the parent
focusNode = focusNode.leftChild;
//if the focusNode is empty we want to place the new node to the left
if(focusNode == null){
parent.leftChild = newNode;
return;
}//end focusNode == null
}//end if focusNode.number.compareto(userNumber)
// puts the node on the right of the tree because it holds a larger value
//than the parent
else{
focusNode = focusNode.rightChild;
//if the right child has no children
if(focusNode == null){
//then place the new node on the right of it
parent.rightChild = newNode;
return;
}//end focusNode == null
}//end else
}//end while
}//end else
}//end addNode
//a recursive method to traverse the binaryTree in order
public void inOrderTraverse(Node<A> focusNode){
if(focusNode != null){
inOrderTraverse(focusNode.leftChild);
System.out.println(focusNode);
inOrderTraverse(focusNode.rightChild);
}//end if focus node != null
}//end inordertraverse
public void decreaseOrderTraverse(Node<A> focusNode){
if(focusNode != null){
decreaseOrderTraverse(focusNode.rightChild);
System.out.println(focusNode);
decreaseOrderTraverse(focusNode.leftChild);
}//end if
}//end decrease order traverse method
public class Node<A extends Comparable<A>>{
//this generic variable will become the user input either int or fraction
private A number;
//nodes that will become the left of right child of a parent node
Node<A> leftChild;
Node<A> rightChild;
//a node constructor that will take a generic input
Node(A number){
this.number = number;
}//end node constructor
public String toString(){
return "" +number;
}
}//end the Node class
public static void main(String[] args){
BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();
BinarySearchTree<String> sTree = new BinarySearchTree<String>();
System.out.println("enter a number to put into the node");
//tree.addNode((10));
//tree.addNode(3);
//tree.addNode(5);
//tree.addNode(20);
sTree.addNode("1/2");//=0.5
sTree.addNode("1/7");//=0.14
sTree.addNode("1/5");//=.20
//sTree.decreaseOrderTraverse(sTree.root);
sTree.inOrderTraverse(sTree.root);
//tree.inOrderTraverse(tree.root);
//tree.decreaseOrderTraverse(tree.root);
//num = input.nextInt();
//test = input.nextLine();
//tree.addNode(num);
//num30 = input.nextInt();
//tree.addNode(num30);
//tree.addNode(test);
//tree.printNode(tree.root);
}
}//end binary search tree
, only the first number of the input is displayed
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly