Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    well i figured it out. if anybody wants the explanation ill post it but it'd be hard to explain

    edit:
    or i can just post the code:

    Java Code:
    public Tree<E> unzip (String zippedTree)
      {
            int half = zippedTree.length() / 2;
            
            String pre = zippedTree.substring(0, half);
            String post = zippedTree.substring(half);
            
            
            return unzip (pre, post);
           
      }
      private Tree unzip(String pre, String post)
      {    
            if(pre.equals("") || post.equals(""))
                return new Tree ();
            else 
            {
                Node<E> root = new Node (pre.charAt(0));
                pre = pre.substring(1);
                post = post.substring(0, post.length()-1);
                
                List foo = new ArrayList<Tree> ();
                
                while(!pre.equals(""))
                {
                   String string2 = post.substring(0, post.indexOf(pre.charAt(0))+1);
                   String string1 = pre.substring(0, string2.length());
                   
                   pre = pre.substring( string1.length());
                   post = post.substring ( string1.length());
            
                   foo.add(unzip(string1, string2));
                }
                
                return new Tree(root, foo);
            }
      }
    Last edited by sAntA199; 12-04-2009 at 05:05 AM.

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sAntA199 View Post
    well i figured it out. if anybody wants the explanation ill post it but it'd be hard to explain
    Please do because the code itself doesn't make any sense to me (btw, it can't be done no matter how hard you try (see my small counter example above)).

    kind regards,

    Jos

  3. #23
    sAntA199 is offline Member
    Join Date
    Nov 2009
    Posts
    18
    Rep Power
    0

    Default

    ok so for example:
    preorder: tambcde
    postorder: abcdmet

    1. so you know t is the root because it is first in the preorder. you also know that everything to the left of t in the postorder are its children

    2. remove t from both strings

    3. you then know that a is the first child because its first in the preorder. it has no children because theres nothing to the left of it in the postorder.

    4. remove a from both strings

    5. you then know that m is the second child because its first in the preorder. it has b, c, and d as children because they're to the left of it in the postorder.

    6. remove m, b, c, and d from both strings

    7. you then know that e is the third child of t because its first in the preorder. it has no children because theres nothing to the left of it in the postorder.

    and there you go

  4. #24
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default

    You don't know whether or not a is a leftmost child of t; the leftmost child of t can be empty (or node a).

    kind regards,

    Jos

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2011, 05:30 PM
  2. Creating a Tree and then saving the Tree
    By jackmatt2 in forum New To Java
    Replies: 0
    Last Post: 08-22-2009, 12:51 PM
  3. Issue with TreeViewer and JPopupMenu and components in general
    By xcallmejudasx in forum Advanced Java
    Replies: 1
    Last Post: 11-13-2008, 11:43 PM
  4. General Discussion on Abstract
    By sanjeevtarar in forum Advanced Java
    Replies: 15
    Last Post: 05-06-2008, 06:16 AM
  5. How to set General options in NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 08-02-2007, 12:11 PM

Posting Permissions

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