Advertisement
Sphyxx

BST Java

Jul 14th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. public class BinaryNode {
  2.     int value;
  3.     //setting up binarynode values
  4.     private BinaryNode left;
  5.     private BinaryNode right;
  6.    
  7.     public BinaryNode(int value) {
  8.         this.value=value;
  9.         left = null;
  10.         right = null;
  11.     }
  12.    
  13.    
  14.    
  15.     public void setLeft(BinaryNode left) {
  16.         this.left = left;
  17.     }
  18.     public void setRight(BinaryNode right) {
  19.         this.right = right;
  20.     }
  21.     public void setValue(int value) {
  22.         this.value = value;
  23.     }
  24.  
  25.  
  26.    
  27.     public BinaryNode getLeft() {return left;}
  28.     public BinaryNode getRight() {return right;}
  29.     public int getValue() {return value;}
  30.  
  31.    
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39. public class BinaryTree {
  40.    
  41.     BinaryNode root;
  42.     public BinaryNode getRoot(){return root;}
  43.    
  44.     public void insertNode(int newInt, BinaryNode currentNode) {
  45.        
  46.          if (root == null) {
  47.                 root = new BinaryNode(newInt);
  48.                 //System.out.println(currentNode);
  49.                 //System.out.println(newInt);
  50.                 //System.out.println(newInt.getValue());
  51.                 //System.out.println("Node Null, made root");
  52.              }else{
  53.  
  54.  
  55.  
  56.                  //if less than the node, go left
  57.                  if (newInt < currentNode.getValue()) {
  58.                      //System.out.println("less than");
  59.                      if (currentNode.getLeft() == null) {
  60.                          //System.out.println("Node Null, set left");
  61.                          currentNode.setLeft(new BinaryNode(newInt));
  62.                      }
  63.                      //the recursive call on insert
  64.                      else {
  65.                          //System.out.println("recursive left");
  66.                          insertNode(newInt, currentNode.getLeft());
  67.                      }
  68.                  }
  69.  
  70.                  else {
  71.                      //if greater than node, and
  72.                      //System.out.println("greater than");
  73.                      if (currentNode.getRight() == null) {
  74.                          //System.out.println("Node Null, set right");
  75.                          currentNode.setRight(new BinaryNode(newInt));
  76.                 }
  77.  
  78.                      else {
  79.                          //System.out.println("recursive right");
  80.                          insertNode(newInt, currentNode.getRight());
  81.                      }
  82.  
  83.              }
  84.  
  85.     }
  86.        
  87.     }//insertNode body
  88.    
  89.     public void outputList() {
  90.        
  91.         //create output string
  92.         String listoutput =("");
  93.         traverse(root);
  94.         System.out.println(listoutput);
  95.        
  96.     }
  97.    
  98.      public String traverse(BinaryNode node) {
  99.          
  100.          //first a recursive call on the left subtree, if it exists
  101.          if (node.getLeft() != null) {
  102.                  traverse(node.getRight());
  103.          }
  104.          //add current node to outString
  105.          append(node.getValue());
  106.        
  107.          //followed by a recursive call on the right subtree, if it exists
  108.          if(node.getRight() != null) {
  109.                  traverse(node.getRight());
  110.          }
  111.      }
  112.  
  113.      public void append(String newString){
  114.  
  115.     if (listoutput.equals("")){
  116.         listoutput += newString;
  117.     } else {
  118.         listoutput += ", " + newString;
  119.         }
  120.     }
  121.  
  122.    
  123.     public static void main(String[] args) {
  124.          //input from command line
  125.          BinaryTree input = new BinaryTree();
  126.          for (String arg : args) {
  127.              input.insertNode(Integer.parseInt(arg), input.getRoot());
  128.  
  129.          }
  130.          input.outputList();
  131.     }
  132.    
  133. }//class body
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement