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: | 304 Weeks Ago, 2 Days 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