Results 1 to 4 of 4
  1. #1
    mishy is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Binary Tree depth help

    I have a list of people with a matching telephone extension number, the people are organised in a hierachy tree with some colleagues at the same level and then some junior colleagues. I have been trying to write code that can find the tree height of any given member but I am unable too. Here is my code so far but the left and right are not working because I have not declared them any where in my code.

    Java Code:
    //hierarchy rank section 7
      // **Depth** from node to root (bottom up)
      public int rank(Member p1){ 	  
    	//to do
        
        int currentDepth, depth;
      
            if (p1 != null) {
                currentDepth++;
                 
                // record total depth if current depth is greater than total depth 
                if (currentDepth > depth) {
                    depth = currentDepth;
                }
         
                // recursively traverse entire tree
                depth(p1.left);
                depth(p1.right);
                 
                // decrement as we traverse up the binary tree
                currentDepth--;
            }
            return depth;
        
    	}

  2. #2
    Cg33k is offline Member
    Join Date
    Mar 2014
    Location
    Leb
    Posts
    9
    Rep Power
    0

    Default Re: Binary Tree depth help

    To get the depth of a BST you need to just count the steps taken to find the target.
    Basically just add a counter to the search method of the tree, when done searching that counter is the depth of the searched for member...

  3. #3
    mishy is offline Member
    Join Date
    Feb 2014
    Posts
    8
    Rep Power
    0

    Default Re: Binary Tree depth help

    My problem is eclipse is highlighting an error on p1.left and p1.right because p1 has not been set as a node but just as a member. What I am trying to achieve with my method is that for any given member you should be able find their rank, with the rank being how many branches down from the head (I hope it makes sense). Do I need to find the node of the member then the p1.left and p1.right will work?

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Binary Tree depth help

    This may not be right:

    When I worked with trees for writing a compression program, the "bottom" of my tree was simply another treenode with "null" branches.


    AKA, your very bottom member should actually be another tree node, where you set the branches to be null. That way, you can keep going down the tree until you reach a "NULL." It's an easy way to know when to stop.

Similar Threads

  1. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 02:08 AM
  2. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  3. Binary Tree
    By MuslimCoder in forum New To Java
    Replies: 8
    Last Post: 11-19-2009, 05:57 PM
  4. Creating a tree of depth 5
    By roaan in forum New To Java
    Replies: 1
    Last Post: 07-13-2009, 09:26 AM

Posting Permissions

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