CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2010
    Posts
    4

    traversing a binary tree

    Hi

    I've written the following code for converting Postfix to Infix,it reads a string then reads it letter by letter ,a stack and a Tree is used,and I want to traverse the last object that has poped from the stack,in the following code I used this line:

    System.out.println((Character)(((TreeNode)stack.pop()).data));

    but it only prints the root of the tree,I tried to traverse it but I don't have any idea,I did sth like this( for traversing another node):

    (TreeNode)stack.pop() = ( (TreeNode)stack.pop() ).data;
    System.out.println((Character)(((TreeNode)stack.pop()).data));

    but it didn't work.
    would you plz help me with it.

    Here is the code:

    import java.util.Scanner;

    public class Test
    {
    public static void main( String args[])
    {

    Scanner input = new Scanner( System.in );

    System.out.printf("Enter your String(in Postfix):\n");
    String c = input.nextLine();//read a line of text

    Stack stack = new Stack();

    int i = 0 ;
    while( i < c.length() )
    {

    char a = c.charAt( i );

    if( Character.isLetterOrDigit (a) )
    {
    System.out.printf("\n%c" , a );
    stack.push( new TreeNode( a ) );
    }

    else if( a == '+' || a == '-' || a == '*' || a == '/' )
    {
    TreeNode t = new TreeNode( a );

    t.insert( 0 , stack.pop() );

    t.insert( 1 , stack.pop() );

    stack.push( t );

    }
    i++;
    }

    (TreeNode)stack.pop() = ( (TreeNode)stack.pop() ).data; //<-----Here is the problem

    System.out.println((Character)(((TreeNode)stack.pop()).data));

    }

    }



    Thax
    Bita

  2. #2
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: traversing a binary tree

    Welcome to codeguru. Some things we ask when you are posting questions:

    Always post your code (the actual code you're working on now) in code tags: [ code] [ /code] or the # tool on the post editor.

    Also, post specific questions. The comment "It doesn't work," really doesn't help anyone understand what the problem with your code is. We help our here part time and don't really have time to download and build your code to see what it's doing. If you are getting runtime exceptions, post the full exception output (again, in code tags), do the same with error messages form the compiler.

    Have you tried any debugging (either with the debugger or by adding System.out.println() statements) to see what is going on? What other steps have you tried?

    As a courtesy, you should also mention when you have posted the same question to other forums, and what response you've gotten. It helps both forums avoid wasting time by answering questions that are already answered elsewhere.

    As to the code you've posted here
    Code:
    (TreeNode)stack.pop() = ( (TreeNode)stack.pop() ).data;
    System.out.println((Character)(((TreeNode)stack.pop()).data));
    stack.pop() is a method or function. Assigning to a method doesn't make sense and doesn't work in Java.

    You should also be aware that each time you call stack.pop(), you remove one entry from your data structure. So in the code above, you'd be removing three entries from the stack (if it was working).

  3. #3
    Join Date
    Jul 2010
    Posts
    4

    Re: traversing a binary tree

    first of all thanks a lot to help me how to use the forum and second about the code , pop() is a method and stack is an instance of the Stack class that I have defined.in main class three characters poped from the stack and inserted into a tree that defined by class TreeNode.
    for example the input String 12* will pope in this order :'*' , '2' , '1'. and '*' will be the root of the tree , '2' will be the leftnode and '1' will be the right node,the all the tree will be pushed into the stack and in the next pop we will have 2*1 and my problem is with printing this result ,I don't know how to print an object that has poped from the stack.

    methods pop and push can accept object and class TreeNode only accept character , that's why I wrote the code below by changing the types.

    Code:
    System.out.println((Character)(((TreeNode)stack.pop()).data));
    but it seems it only prints the root , I don't know how to print all the tree , and my question is to help me print the tree.

    Thanx
    Bita

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: traversing a binary tree

    If you want to print the contents of the stack then you need a loop to pop all the elements off the stack and print them.
    Code:
    while( !stack.isEmpty() ) {
        System.out.println((Character)(((TreeNode)stack.pop()).data));
    }
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jul 2010
    Posts
    4

    Re: traversing a binary tree

    thank you for your answer but there is an object of a tree in stack's node and I want to traverse that tree , by just :

    Code:
     System.out.println((Character)(((TreeNode)stack.pop()).data));
    it just shows the root.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: traversing a binary tree

    How can one call to a single method that returns a Character member of an object "traverse the tree"?
    The method could do the traversing and set the data member of what it returns to the results of the traversal. That would be a weird pop() method.
    What do you expect to see printed?
    Norm

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: traversing a binary tree

    Sorry but I've got no idea what you are trying to do.

    If you are trying to print the contents of the tree then you need to iterate over the Tree, printing out each TreeNode in turn. You can't do it in one simple line of code.

    If you don't know how to traverse a tree (there are different traversal strategies eg breadth first, depth first so work out what you need to do first) then google it, there are even examples written in Java showing how to do it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    Jul 2010
    Posts
    4

    Smile Re: traversing a binary tree

    In the code that I've written:

    Code:
    TreeNode t = new TreeNode( a );
    
    t.insert( 0 , stack.pop() );
    
    t.insert( 1 , stack.pop() );
    
    stack.push( t );
    I want to traverse object "t" that will be gained by poping the stack.

    again thanks for helping

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: traversing a binary tree

    If you know your tree is only going to consist of a base node with a left and right leaf node then just call whatever methods your node class has to retrieve the base node's left and right nodes. Although if this is the case why are you using a tree in the first place?

    If there can be many nodes you have to decide the order in which you want to return the nodes.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured