Results 1 to 3 of 3
  1. #1
    addykhanna05 is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Unable to traverse a tree in java

    Hi I want to traverse a tree in a vector form where
    A is parent of B,G .
    B is parent of C, F.
    C is a parent of D, E.
    G is a parent of H ,I
    I is a parent of J

    the output should be as below :



    A
    A>B
    A>B>C
    A>B>C>D
    A>B>C>E
    A>B>F
    A>G
    A>G>H
    A>G>I
    A>G>I>J

    currently i can only get as below

    A>>B
    A>>G
    B>>C
    B>>F
    G>>H
    G>>I
    C>>D
    C>>E
    I>>J


    PFA the files .


    (1)TestVector.java

    /**
    *
    */
    package com;

    import com.Cashpool;
    import java.util.Vector;

    /**
    * @author Nripesh_Patel
    *
    */
    public class TestVector {


    /**
    * @param args
    */
    public static void main(String[] args) {

    Vector<Cashpool> vector1 = new Vector<Cashpool>();

    Vector<Cashpool> vector = new Vector<Cashpool>();
    vector.add(new Cashpool("1","A","B")); //0
    vector.add(new Cashpool("1","A","G"));//1
    vector.add(new Cashpool("2","B","C"));//2
    vector.add(new Cashpool("2","B","F"));//3
    vector.add(new Cashpool("2","G","H"));//4
    vector.add(new Cashpool("2","G","I"));//5
    vector.add(new Cashpool("3","C","D"));//6
    vector.add(new Cashpool("3","C","E"));//7
    vector.add(new Cashpool("3","I","J"));//8


    int level = 1;
    for(int a=1;a < vector.size();a ++)
    {
    if(Integer.parseInt((vector.get(a).getLevel())) > (Integer.parseInt((vector.get(a-1).getLevel()))))
    {
    level = Integer.parseInt((vector.get(a).getLevel()));
    }
    }
    System.out.println(" Total Level ###" +level);

    int j =1;
    int total = 0;
    String accounts ="";
    String saccounts ="";

    String accountsx ="";
    String saccountsx ="";
    while ( j <= level){
    for(int a=0;a < vector.size();a ++)
    {

    accounts = vector.get(a).getAccount();
    saccounts = vector.get(a).getSubaccount();

    System.out.println(" " +accounts+">>" + subaccounts);
    if(vector.get(a).getLevel().equalsIgnoreCase(Strin g.valueOf(j)))
    {
    // System.out.println(" Total " +total);
    total++;
    }

    }
    if(total !=0){
    for(int a=1, count =0;a < vector.size();a ++){
    if(vector.get(a).getLevel().equalsIgnoreCase(Strin g.valueOf(j))){
    count++;
    System.out.println(" Level is:: "+j+ " Counts is :: "+count);
    }
    j= j+1;
    if(j < level){
    iterateVector(accounts,vector,j,level);
    }
    }

    }
    }
    }

    private static void iterateVector(String accounts, Vector<Cashpool> vector, int j, int level) {

    int total=0;
    for(int a=1;a < vector.size();a ++){
    accounts = vector.get(a).getAccount();
    if(vector.get(a).getLevel().equalsIgnoreCase(Strin g.valueOf(j))){
    total++;

    }
    // System.out.println(" Level isa:: "+j+ " Counts isa :: "+total);
    }
    if(total !=0){
    for(int a=1, count =0;a < vector.size();a ++){
    if(vector.get(a).getLevel().equalsIgnoreCase(Strin g.valueOf(j))){
    count++;
    }
    // System.out.println(" Level isa1:: "+j+ " Counts isa :: "+count);
    j = j+1;
    if(j <= level){
    iterateVector(accounts,vector,j+1,level);
    }
    }

    }


    }

    private static void printValue(Vector<Cashpool> vector1) {
    for(int b=0;b < vector1.size();b++){
    System.out.println(" Account is: "+vector1.get(b).getAccount()+ "::Sub Account is: "+vector1.get(b).getSubaccount()+ ":: Level is :"+vector1.get(b).getLevel());
    }


    }
    }





    (2)Cashpool.java

    package com;

    public class Cashpool {

    String level ="";
    String account = "";
    String subaccount = "";

    public Cashpool() {
    super();
    }
    public Cashpool(String level, String account, String subaccount) {
    super();
    this.level = level;
    this.account = account;
    this.subaccount = subaccount;
    }
    public String getAccount() {
    return account;
    }
    public void setAccount(String account) {
    this.account = account;
    }
    public String getLevel() {
    return level;
    }
    public void setLevel(String level) {
    this.level = level;
    }
    public String getSubaccount() {
    return subaccount;
    }
    public void setSubaccount(String subaccount) {
    this.subaccount = subaccount;
    }

    }




    Please help ..

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Unable to traverse a tree in java

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Unable to traverse a tree in java

    Traverse the tree in a depth first order and use a global stack: when you descend into a child node, push the child node on the stack and print the stack from bottom to top; when you're ready with a node, pop it from the stack.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 04-16-2012, 09:35 AM
  2. Help: traverse Huffman tree
    By Reploids in forum New To Java
    Replies: 0
    Last Post: 05-24-2011, 11:02 AM
  3. Replies: 2
    Last Post: 11-02-2010, 01:27 PM
  4. Traverse control on WizardPage
    By laurie in forum SWT / JFace
    Replies: 0
    Last Post: 06-29-2010, 09:16 PM
  5. Replies: 2
    Last Post: 11-27-2007, 02:45 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
  •