need ur help plz


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: need ur help plz

  1. #1
    Join Date
    Apr 2006
    Posts
    5

    Thumbs down need ur help plz

    hi every body,,,i am new and this is first time i write,,,,
    i have question i dont know how to write it
    i should write a method called public nodeLevel(int k) which returns how many nodes in level k.
    i have already wtrite the class binary tree ...

    thank u

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What you will be performing is a breadth first search and using a queue to track the nodes visited and the node levels. [This is a classic kind of BFS solved task.] You will count the number of nodes which are at level "k" - either visit all the nodes at level k-1 and tally their children, or actually visit the nodes at level "k" and tally your count as you visit them.

    If you really wanted (needed) to, you could do this as a depth first search, to traverse down to each sucessor at level "k".
    Last edited by nspils; 04-29-2006 at 10:19 AM.

  3. #3
    Join Date
    Apr 2006
    Posts
    5
    i ve written this it is ok or not??


    public nodelevel(int k){
    if(root == null)
    return 0;
    else
    return getnodelevel(root, 1);
    }

    private int getnodelevel(TreeNode root, int k){
    if(root == null)
    return 0;
    else
    return level +getnodelevel(root.left, k) + getnodelevel(root.right,k);
    }

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    That's a nice recursive determining the depth of the tree. How are you going to capture the number of nodes at each level?

  5. #5
    Join Date
    Apr 2006
    Posts
    5
    ok can u help me with this plz

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What can you do to store (in your nodeLevel method) the values being returned by your getNodeLevel method? Once you have those values stored, what can you do to obtain a count of the number of nodes at level "k"?

  7. #7
    Join Date
    Apr 2006
    Posts
    5
    i don't know how to complete it??

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You would perform a breadth first search -

    go to root, put it into a queue, add a value that it is level 0
    whle the queue is not empty,
    pop the root node off the queue, visit each of its children, pushing them onto the queue, noting they are level 1
    pop each first child off the queue, increment your count of level 1 nodes, visit each of its chidren, pushing them onto the queue adding a note that they are level 2.
    when all level 1 nodes are off the queue,
    pop each grandchildren child off the queue, increment your count of level 2 nodes, visit each of their chidren, pushing them onto the queue adding a note that they are level 3.
    when all level 2 nodes are off the queue,
    etc.
    Last edited by nspils; 05-06-2006 at 09:01 PM.

  9. #9
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    How are you building your binary tree?

  10. #10
    Join Date
    Apr 2006
    Posts
    5
    what do you mean?
    i have already write the binary tree

Similar Threads

  1. Replies: 8
    Last Post: 02-09-2006, 11:03 PM
  2. plz help me n00b here
    By Apocalyp5e in forum Java
    Replies: 11
    Last Post: 12-22-2005, 09:03 PM
  3. plz plz plz help me(java)
    By kamon in forum Java
    Replies: 4
    Last Post: 08-10-2005, 11:30 AM
  4. java applet... i'm in a fix, plz help
    By nsbscool in forum Java
    Replies: 1
    Last Post: 05-02-2005, 09:01 AM
  5. Plz Help!!!!
    By Kyle in forum Java
    Replies: 0
    Last Post: 05-14-2003, 04:25 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center