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 3 of 3

Thread: Creating new instance

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Creating new instance

    Hello JPF. I am currently working on a program that builds an expression tree. I am provided with class BinaryTree and I am having trouble constructing a new node.

    1) This works:
    BinaryTree<String> newTree = new BinaryTree<String>(nextToken,null,null);

    2) This doesn't work:
    BinaryTree<String> newTree = new Node(nextToken);

    I'm trying to get number 2 to work. Can someone please help me out and explain why it doesn't work?

    This is the BinaryTree class:
    import java.io.*;
     
    /** Class for a binary tree that stores type E objects.
    *   @author Koffman and Wolfgang
    * */
     
    public class BinaryTree < E > implements Serializable 
    {
     
      /** Class to encapsulate a tree node. */
      protected static class Node < E >
          implements Serializable {
        // Data Fields
        /** The information stored in this node. */
        protected E data;
     
        /** Reference to the left child. */
        protected Node < E > left;
     
        /** Reference to the right child. */
        protected Node < E > right;
     
        // Constructors
        /** Construct a node with given data and no children.
            @param data The data to store in this node
         */
        public Node(E data) 
        {
          this.data = data;
          left = null;
          right = null;
        }
     
        // Methods
        /** Return a string representation of the node.
            @return A string representation of the data fields
         */
        public String toString() {
          return data.toString();
        }
      }
     
      // Data Field
      /** The root of the binary tree */
      protected Node < E > root;
     
      public BinaryTree() {
        root = null;
      }
     
      protected BinaryTree(Node < E > root) {
        this.root = root;
      }
     
      /** Constructs a new binary tree with data in its root,leftTree
          as its left subtree and rightTree as its right subtree.
       */
      public BinaryTree(E data, BinaryTree < E > leftTree,
                        BinaryTree < E > rightTree) {
        root = new Node < E > (data);
        if (leftTree != null) {
          root.left = leftTree.root;
        }
        else {
          root.left = null;
        }
        if (rightTree != null) {
          root.right = rightTree.root;
        }
        else {
          root.right = null;
        }
      }
     
      /** Return the left subtree.
          @return The left subtree or null if either the root or
          the left subtree is null
       */
      public BinaryTree < E > getLeftSubtree() {
        if (root != null && root.left != null) {
          return new BinaryTree < E > (root.left);
        }
        else {
          return null;
        }
      }
     
      /** Return the right sub-tree
            @return the right sub-tree or
            null if either the root or the
            right subtree is null.
        */
        public BinaryTree<E> getRightSubtree() {
            if (root != null && root.right != null) {
                return new BinaryTree<E>(root.right);
            } else {
                return null;
            }
        }
     
     
    /**** EXERCISE ****/
     
      /** Determine whether this tree is a leaf.
          @return true if the root has no children
       */
      public boolean isLeaf() {
        return (root.left == null && root.right == null);
      }
     
      public String toString() {
        StringBuilder sb = new StringBuilder();
        preOrderTraverse(root, 1, sb);
        return sb.toString();
      }
     
      /** Perform a preorder traversal.
          @param node The local root
          @param depth The depth
          @param sb The string buffer to save the output
       */
      private void preOrderTraverse(Node < E > node, int depth,
                                    StringBuilder sb) {
        for (int i = 1; i < depth; i++) {
          sb.append("  ");
        }
        if (node == null) {
          sb.append("null\n");
        }
        else {
          sb.append(node.toString());
          sb.append("\n");
          preOrderTraverse(node.left, depth + 1, sb);
          preOrderTraverse(node.right, depth + 1, sb);
        }
      }
     
      /** Method to read a binary tree.
          pre: The input consists of a preorder traversal
               of the binary tree. The line "null" indicates a null tree.
          @param bR The input file
          @return The binary tree
          @throws IOException If there is an input error
       */
      public static BinaryTree < String >
          readBinaryTree(BufferedReader bR) throws IOException {
        // Read a line and trim leading and trailing spaces.
        String data = bR.readLine().trim();
        if (data.equals("null")) {
          return null;
        }
        else {
          BinaryTree < String > leftTree = readBinaryTree(bR);
          BinaryTree < String > rightTree = readBinaryTree(bR);
          return new BinaryTree < String > (data, leftTree, rightTree);
        }
      }
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating new instance

    Are you trying to call the second constructor from an external class? Cause that constructor was declared protected. Other than that, there's not much difference as far as I can tell. If you want to make it easier to maintain, make that method like this:

    public BinaryTree(Node<E> root)
    {
         BinaryTree(root,null,null);
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    vluong (November 28th, 2009)

  4. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    24
    My Mood
    Fine
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Creating new instance

    I'll give that a try!

Similar Threads

  1. Problem in Creating Jar file
    By sikriyogesh in forum Member Introductions
    Replies: 0
    Last Post: November 20th, 2009, 02:12 AM
  2. Creating a class file
    By ipatch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2009, 07:19 PM
  3. Need help creating this program
    By ixjaybeexi in forum File I/O & Other I/O Streams
    Replies: 25
    Last Post: October 19th, 2009, 07:08 AM
  4. class constants instance constants..... etc
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 18th, 2009, 03:38 PM
  5. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM