Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: BinaryTree

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BinaryTree

    package SystemandDesign.BinaryTree;
     
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.List;
    import java.io.ObjectInputStream;
     
    public class BinaryTree{
     
      private int data;
      private List list;
      private BinaryTree root;
      private BinaryTree leftSubtree;
      private BinaryTree rightSubtree;
      private ObjectInputStream input;
     
      public BinaryTree(int value){
     
        this.data = value;
        this.leftSubtree = null;
        this.rightSubtree = null;
        this.root = null;
     
      }
     
      public int getData(){
     
        return this.data;
     
      }
     
      public void insert(int value){
     
        if(root == null){//starts a new Binary tree
          root = new BinaryTree(value);
        }
     
        if(value < data){//if the data being inserted is less than the original data, it will be inserted to the left side.
     
          if(leftSubtree == null){
            leftSubtree = new BinaryTree(value);
          }
          else{
            leftSubtree.insert(value);
          }
     
        }
        else if(value > data){//right side of binary tree
     
          if(rightSubtree == null){
            rightSubtree = new BinaryTree(value);
          }
          else{
            rightSubtree.insert(value);
          }
     
        }
     
     
      }
     
      public void inOrder(BinaryTree leaf){
     
        if(leaf != null){
          inOrder(leaf.leftSubtree);
          System.out.print(leaf.data+"");
          inOrder(leaf.rightSubtree);
     
     
      }
     
    }
     
      public static void main(String[] args){
     
        BinaryTree.openFile();
        BinaryTree.readFile();
        BinaryTree.closeFile();
      }
     
      public void openFile(){
        try{
        input = new ObjectInputStream(new FileInputStream("data.ser"));
      }catch(IOException ioException){
        System.err.println("Error opening file.");
      }
      }
      public void readFile(){ 
      try{
         list = (List<Integer>)input.readObject();
      }catch(IOException e){
        System.err.println("Error class not found.");
      }catch(ClassNotFoundException i){
        System.err.println("Error");
      }
      }
      public void closeFile(){
        try{
          if(input != null)
            input.close();
        }catch(IOException ioException){
          System.err.println("Error closing file.");
          System.exit(1);
        }
      }
    }

    So I am having trouble with trying to get the main method to work. The program is supposed to construct a BinaryTree, and deserialize a file that is used for the Tree.


  2. #2
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: BinaryTree

    Define " I am having trouble with trying to get the main method to work". What error messages do you get, if any?

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BinaryTree

    Quote Originally Posted by camel-man View Post
    Define " I am having trouble with trying to get the main method to work". What error messages do you get, if any?
    Cannot make a static reference to the non-static method openFile() from type SystemandDesign.BinaryTree.BinaryTree(same with readFile() and closeFile();

  4. #4
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: BinaryTree

    You need to make your methods static. ex.)static public void readFile(){
    .....

  5. #5
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BinaryTree

    Quote Originally Posted by camel-man View Post
    You need to make your methods static. ex.)static public void readFile(){
    .....
    getting 5 errors now:

    Cannot make static reference to the non-static field input
    " " list

  6. #6
    Member
    Join Date
    Dec 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BinaryTree

    3 errors and 3 warnings found:
    --------------
    *** Errors ***
    --------------
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 76]
    Error: Cannot make a static reference to the non-static method openFile() from the type SystemandDesign.BinaryTree.BinaryTree
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 77]
    Error: Cannot make a static reference to the non-static method readFile() from the type SystemandDesign.BinaryTree.BinaryTree
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 78]
    Error: Cannot make a static reference to the non-static method closeFile() from the type SystemandDesign.BinaryTree.BinaryTree
    --------------
    ** Warnings **
    --------------
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 11]
    Warning: java.util.List is a raw type. References to generic type java.util.List<E> should be parameterized
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 11]
    Warning: The field SystemandDesign.BinaryTree.BinaryTree.list is never read locally
    File: C:\Users\Carlos Satres\Documents\SystemandDesign\BinaryTree\Binary Tree.java [line: 90]
    Warning: Type safety: Unchecked cast from java.lang.Object to java.util.List<java.lang.Integer>

    Current errors with the code as is x.x

Similar Threads

  1. Binarytree in java
    By fabaho in forum Algorithms & Recursion
    Replies: 6
    Last Post: October 25th, 2012, 08:32 AM

Tags for this Thread