I have an arraylist which is linked in a tree structure (a family tree to be precise). Each node is an object containing a generation field. I would like to find the leaf with the highest generation within the tree. I tried this:

```
private int findHighestLeafGeneration(FamilyMember node, int gen){
if(node.getChildren().isEmpty()){
return gen;
}
for(int i = 0; i < node.getChildren().size(); i++){
gen = findHighestLeafGeneration(node.getChild(i), node.getChild(i).getGeneration());
}
return gen;
}
```

I call the method like this:

```
findHighestLeafGeneration(node, node.getGeneration());
```

What this is supposed to do is take a member of the family tree (a node) and print all the descendants for that member. My PrintDescendants method requires the number of generations to print.

The above is a little buggy however. It gets the leaf generation then returns and overwrites the leafs generation with the parent nodes generation. Any clues why?

Do this:

```
private int findHighestLeafGeneration(FamilyMember node) {
int gen = node.getGeneration();
for(int i = 0; i < node.getChildren().size(); i++){
int highestChild = findHighestLeafGeneration(node.getChild(i));
if(highestChild > gen) {
gen = highestChild;
}
}
return gen;
}
```

Similar Questions

I'm trying to get the row with the highest/lowest number, after performing a GROUP BY: Here is my test data mysql> SELECT * FROM test; +----+-------+------+ | id | value | name | +----+-------+----

I happened to the same problem as this Finding the highest n values of each group in MySQL I have some data like this: + --------- + ----------- + | lane | series | + --------- + ----------- + | 1 |

Given the following array: complete_matrix = numpy.array([ [0, 1, 2, 4], [1, 0, 3, 5], [2, 3, 0, 6], [4, 5, 6, 0]]) I would like to identify the row with the highest average, excluding the diagonal z

I am trying to get the highest value from a table, but the value must have been set within the last 5 days. The query below seems to work but how do I write it in Hibernate Query Laguage, HQL? SELEC

I have two lists old and new, with the same number of elements. I'm trying to write an efficient function that takes n as a parameter, compares the elements of two lists at the same locations (by inde

I want to sort my array order by highest value of score. it's a json data score:[ { userId:5, playtime:1396369254, score:25 }, { userId:1, playtime:1396369056, score:12 }, {

I have the following tables linked below: http://i.stack.imgur.com/UlxDe.png I want to list all the times a menu item has been selected for the menu items that have the 2 highest values for price per

Fixed: See EDIT EDIT Hey guys, I'm having trouble with writing my own recursive each for an n-ary tree. @element is the value of the node, and @children is an array of all connected lower nodes. This

I wondered if there were any tree libraries available for (n)curses. I'm trying to write a component that shows a tree of folders & was curious if there was a prebuilt curses component that could

In XPath, how can I get the node with the highest value? e.g. <tr> <td>$12.00</td> <td>$24.00</td> <td>$13.00</td> </tr> would return $24.00. I'm usin

I want to use the sed or awk commands to print only lines from a list that are unique in terms of pattern 1 and have the highest value for pattern 2. The input is a list of filenames that are software

I am having trouble coding in Java the following method int findNodeN(Node node, int n) For example if the binary search tree is constructed as following: 20 10 30 1 14 25 35 Then node 1 would b

SELECT * FROM employee A WHERE 3=(select count(*) +1 from employee B where B.salary > A.salary) This gets the 3rd highest salary; can somebody explain the logic behind this query and how it works.

i have written this tree class for a familytree now i need a search method to find a node in my tree its a n-ary tree that each node can have 0 to n children the search method can search for a node or

In a MYSQL query, how can i select all of the rows where a value is highest for example: SELECT UNIX_TIMESTAMP(creation_date) AS `date`, release_version FROM content WHERE id = '1' ORDER BY `date` DES

I have a simple question (or so I thought). I have a dictionary, lets say it looks like this: dict = {'A':100, 'a':10, 'T':50, 't':5} I simply want to delete the key with the highest value. I tried t

I would like to create a new dictionary from dictionaries. all keys in all dictionaries must be present in resulting dictionary all keys must be present only once value for key is highest value from

I need a function that will go through below values and print out the highest value with its name. It is not hard to find the highest value which is 20 but I can't figure out a way to display the name

very basic question: How can I find the highest or lowest value in a random matrix. I know there is a possibility to say: a = find(A>0.5) but what I'm looking for would be more like this: A = rand

I am trying to write some code which identifies the greatest two values for each row and provides their column number and value. df = data.frame( car = c (2,1,1,1,0), bus = c (0,2,0,1,0), walk = c (0,

How do I retrieve the second highest value from a table?

I have 4 variables and each of those have an integer assigned to them. Could anybody please let me know how I can get the name of the variable which has the highest value? Thanks in advance.

I use sed to get the content of file from a desire point but I have a problem. I can not print $variable value into this sed command count=$(sed -n '/$variable/,$p' file.log | grep 'KO' -c) I try w

This is a homework question: Use a pre-process algorithm in O(n*log(n)) and then describe the algorithm to answer this question in O(1):whether node u is an ancestor of node v? actually I can't thi

I'm having difficulty to understand the logic behind the method to find the second highest number in array. The method used is to find the highest in the array but less than the previous highest (whic

Problem: I need to sort the array (contents shown in bulleted list in the order they appear within the array) into order of the numbers on the left-hand side (highest to lowest). The numbers correspon

So here is what I am trying to do - To write a program with an array of 50 values getting the highest number in the array and printing it out. I have hit a brick wall though. I am pretty sure I've got

I know there are n leafs in the tree, How many possible trees? The tree can be arbitrary branched (at least 2-branch).

I'm looking for a way to prove the run time of the pre-tree traversal algorithm for a n-ary tree. Each node can have any number of children. I seem to be only able to find a proof for a binary tree. I

Is there any way to find the highest value of each colum (in a html table) and to add a class to it using js or jquery? Thank you. note: the table is build with <thead> and <tbody> The tab

a[0] = {:id => '1234', :value => '37'} a[1] = {:id => '4321', :value => '50'} a[2] = {:id => '1122', :value => '50'} From here I want to be able to check to see if a hash exists wit

Say we doing the following: for i in dir(foo): print type(i) This setup will give back a list of strings, but I'm more interested in finding out the type of the items inside of foo. What would be a g

i want to know the n'th highest value from a column of a table for eg: 2nd highest salary from tbl_salary or 4th highest salary from tbl_salary i seen somewhere below query select salary from tbl_sala

function find_highest_prime_factor($n) { for ($i = 2; $i <= $n; $i++) { if (bcmod($n, $i) == 0) //its a factor { return max($i, find_highest_prime_factor(bcdiv($n,$i))); } } if ($i == $n) { return

I came across a piece of code for finding the smallest power of 2 greater than a 32-bit integer n... n+=(n==0); n--; n|=n>>1; n|=n>>2; n|=n>>4; n|=n>>8; n|=n>>16; n++; N

Which would be a neat implemenation of a N-ary tree in C language? Particulary, I want to implement an n-ary tree, not self-ballancing, with an unbound number of children in each node, in which each n

I'm trying to graphically display a graph of N lines and I'm trying to find a way to dynamically assign distinct colors based on how many lines I have. The values in RGB range from 0 to 1. I can't use

Consider a binary tree with n nodes. How many different possible binary trees structures are there? I tried something like: n number of different structure: 1 1 2 4 3 16 so is that 4(n-1) for n >1

Finding the level of the particular xml using a node value in .Net Experts ! This is my sample Xml.This is a tree structure and T->T.1,T.2->T.2.2,TA->TA.1 - this is the tree strucutre. I know

I have student table which has column of score. I want to get the fourth highest score,Where the table data is sorted in ascending order.

Having trouble comparing 4 float values in objective c, I would like to return the highest or equal values of these 4 integers. As you can see two are the same here. This is the NSLOG output 2011-08-1

Is there a way to get the highest column value for example the highest column that contains values. using getWorksheetIterator. I want to get the column and use that to clean cells upto the highest co

So let's say I'm building a Tree using javax.swing.tree.DefaultMutableTreeNode and I add N children to a particular node. I want the children to be in a particular order (based on Comparable/a custom

In my query I'm returning number of days a week something occurs. Everything is working fine, with the exception that I'm getting multiple rows returned where I only need the highest. So for example i

I am searching an algorithm for the following problem: given a directed-weighted graph G, find a tree T in G such that average weight of T is maximal.

What is the easiest way to get a key with the highest value from a hash in Perl?

This question already has an answer here: What is the simplest SQL Query to find the second largest value? 29 answers say I have a table of Employees where I maintain the name of the employee i

By searching the web I can find 2(kruskal and prims) algorithm for finding minimum spanning tree. But this algorithm *let T be initially the set of all edges *while there is some cycle C in T remove

Assume I have a binary tree: data Bst a = Empty | Node (Bst a) a (Bst a) I have to write a function that searches for a value and returns the number of its children. If there is no node with this val

I'm trying to code a procedure which goes through a record of numbers and finds which one is highest, the code currently is below. The issue I have is that it just seems to list the last score on the