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

Thread: help with inorder traversal

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with inorder traversal

    I need a method to do an inorder traversal for the following class
    public class TreeNode {
     
      /** the data stored at this node */
      private String data;
     
      /** the left child */
      private TreeNode left;
     
      /** the right child */
      private TreeNode right;
     
      /**
       * Constructor that takes the string
       * to store 
       * @param something the string to store
       */
      public TreeNode(String something) {
        data = something;
        left = null;
        right = null;
      }
     
      /** 
       * Method to return the data
       * @return the string data at this node 
       */
      public String getData() { return data; }
     
      /**
       * Method to set the data at this node
       * @param something the data to use
       */
      public void setData(String something){data = something;}
     
      /**
       * Method to get the left child
       * @return the left child (may be null)
       */
      public TreeNode getLeft() {return left;}
     
      /**
       * Method to get the right child
       * @return the right child (may be null)
       */
      public TreeNode getRight() {return right;}
     
      /**
       * Method to set the left child
       * @param newLeft the new left child 
       */
      public void setLeft(TreeNode newLeft){left = newLeft;}
     
      /**
       * Method to set the right child
       * @param newRight the new right child
       */
      public void setRight(TreeNode newRight)
      {right = newRight;}
     
      /**
       * Method to return a string of information
       * @return the information string
       */
     
      public String toString() {
        return
          "This: " + this.getData()+
          " Left: " + this.getLeft() +
          " Right: " + this.getRight();
      }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with inorder traversal

    What problems are you having? Do you have any specific questions about the code you are trying to write?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with inorder traversal

    Quote Originally Posted by Norm View Post
    What problems are you having? Do you have any specific questions about the code you are trying to write?
    No matter what I try I can never get it to reach down to the leftmost branch first.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: help with inorder traversal

    Can you post the program's output that shows what you are talking about? Add some comments about what is wrong and show what you want the output to be.

    How do you test the code? I don't see a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Generalizing actions for a binary tree traversal?
    By Rectangle in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2013, 08:40 AM
  2. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  3. Binary Tree Traversal help please
    By sim18 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2012, 08:01 AM
  4. How to Format output of a BSTree depending on the traversal being used?
    By SarahDoesIt in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 11th, 2012, 05:28 PM
  5. Replies: 6
    Last Post: October 23rd, 2009, 03:53 AM