计算比所有儿子都大的节点
Count nodes that are bigger than all their sons
我必须编写一个程序来计算比他们的儿子大的节点,而不包括叶节点,例如:
4 For this tree, the solution would be 4 because is greater than its son 3 but
\ 3 is not counted
3
这是我的方法:
//Result2 is a class that stores a T value (info of the node) and n (counter) to
// count the nodes that are bigger than it's sons, this is a method into the class of
//the node that is a generic class of binary nodes for trees (left, right and info
//parameters)
public Result2<T> countBigger()
{
Result2<T> result = new Result2<T>();
result.info = this.info;
if(this.isLeaf())
{
result.n = 0;
}
else
{
Result2<T> resultL = new Result2<T>();
Result2<T> resultR = new Result2<T>();
if(this.hasLeft())
{
resultL = this.left.countBigger();
}
else
{
resultL.info = this.info;
}
if(this.hasRight())
{
resultR = this.right.countBigger();
}
else
{
resultR.info = this.info;
}
if(result.info.compareTo(resultL.info) > 0 & result.info.compareTo(resultR.info) > 0)
{
result.n++;
}
result.n = result.n + resultL.n + resultR.n;
}
return result;
}
但是我看不到错误
假设 info 是一个 int,这可能适合你。我还没有测试代码,所以如果有问题请告诉我。
public Result2<T> countBigger() {
Result2 <T> result = new Result2<T>();
result.info = this.info;
if (this.isLeaf()) {
result.n = 0;
} else {
Result2<T> resultL;
Result2<T> resultR;
if (this.hasLeft()) {
resultL = this.left.countBigger();
}
if (this.hasRight()) {
resultR = this.right.countBigger();
}
//if resultL/resultR is not set, then there are no children.
int left = (resultL != null) ? resultL.info ? 0;
int right = (resultR != null) ? resultR.info ? 0;
// if parent.info is bigger than both child's info combined, add 1
// if you want to cout it seprately, then use (result.info > left && result.info > right)
if (result.info > (left + right)) {
result.n++;
}
//Add the counter from child results if they are not null
result.n += (resultL != null) ? resultL.n : 0;
result.n += (resultR != null) ? resultR.n : 0;
}
return result;
}
我必须编写一个程序来计算比他们的儿子大的节点,而不包括叶节点,例如:
4 For this tree, the solution would be 4 because is greater than its son 3 but
\ 3 is not counted
3
这是我的方法:
//Result2 is a class that stores a T value (info of the node) and n (counter) to
// count the nodes that are bigger than it's sons, this is a method into the class of
//the node that is a generic class of binary nodes for trees (left, right and info
//parameters)
public Result2<T> countBigger()
{
Result2<T> result = new Result2<T>();
result.info = this.info;
if(this.isLeaf())
{
result.n = 0;
}
else
{
Result2<T> resultL = new Result2<T>();
Result2<T> resultR = new Result2<T>();
if(this.hasLeft())
{
resultL = this.left.countBigger();
}
else
{
resultL.info = this.info;
}
if(this.hasRight())
{
resultR = this.right.countBigger();
}
else
{
resultR.info = this.info;
}
if(result.info.compareTo(resultL.info) > 0 & result.info.compareTo(resultR.info) > 0)
{
result.n++;
}
result.n = result.n + resultL.n + resultR.n;
}
return result;
}
但是我看不到错误
假设 info 是一个 int,这可能适合你。我还没有测试代码,所以如果有问题请告诉我。
public Result2<T> countBigger() {
Result2 <T> result = new Result2<T>();
result.info = this.info;
if (this.isLeaf()) {
result.n = 0;
} else {
Result2<T> resultL;
Result2<T> resultR;
if (this.hasLeft()) {
resultL = this.left.countBigger();
}
if (this.hasRight()) {
resultR = this.right.countBigger();
}
//if resultL/resultR is not set, then there are no children.
int left = (resultL != null) ? resultL.info ? 0;
int right = (resultR != null) ? resultR.info ? 0;
// if parent.info is bigger than both child's info combined, add 1
// if you want to cout it seprately, then use (result.info > left && result.info > right)
if (result.info > (left + right)) {
result.n++;
}
//Add the counter from child results if they are not null
result.n += (resultL != null) ? resultL.n : 0;
result.n += (resultR != null) ? resultR.n : 0;
}
return result;
}