Java 在泛型树中查找下一个较大的节点?
我必须在泛型树中找到并返回下一个较大的节点,几乎所有的测试用例都运行良好,并给出正确的输出——只有一个测试用例出现错误,它可能是任何东西。我已经调试了我的程序很多次,但都不知道那可能是什么错误?实际上,我所做的是比较递归为我获取的所有下一个更大的节点,并相互比较,最终找到正确的节点?我被卡住了,如果你能帮我一点忙,我将不胜感激 代码Java 在泛型树中查找下一个较大的节点?,java,algorithm,data-structures,tree,Java,Algorithm,Data Structures,Tree,我必须在泛型树中找到并返回下一个较大的节点,几乎所有的测试用例都运行良好,并给出正确的输出——只有一个测试用例出现错误,它可能是任何东西。我已经调试了我的程序很多次,但都不知道那可能是什么错误?实际上,我所做的是比较递归为我获取的所有下一个更大的节点,并相互比较,最终找到正确的节点?我被卡住了,如果你能帮我一点忙,我将不胜感激 代码 /* TreeNode structure class TreeNode<T> { T data; ArrayList<
/* TreeNode structure
class TreeNode<T> {
T data;
ArrayList<TreeNode<T>> children;
TreeNode(T data){
this.data = data;
children = new ArrayList<TreeNode<T>>();
}
}*/
public static TreeNode<Integer> findNextLargerNode(TreeNode<Integer> root, int n){
if(root==null)
return root;
if(root.children.size()==0)
{
if(root.data>n)
{
return root;
}
else
return null;
}
TreeNode<Integer> count[] = new TreeNode[root.children.size()];
for(int i=0;i<root.children.size();i++)
{
count[i] = findNextLargerNode(root.children.get(i),n);
}
int nextLarger=Integer.MAX_VALUE;
TreeNode<Integer> next = null;
for(int i=0;i<count.length;i++)
{
if(count[i]!=null)
{
if(count[i].data>n && count[i].data<nextLarger)
{
nextLarger = count[i].data;
next = count[i];
}
}
}
if(next!=null)
{
if(root.data>n && root.data<next.data)
return root;
else
return next;
}
else
return null;
}
/*树状结构
三烯类{
T数据;
ArrayList儿童;
树节点(T数据){
这个数据=数据;
children=newarraylist();
}
}*/
公共静态TreeNode findNextLargerNode(TreeNode根,int n){
if(root==null)
返回根;
if(root.children.size()==0)
{
如果(根数据>n)
{
返回根;
}
其他的
返回null;
}
TreeNode count[]=新的TreeNode[root.children.size()];
对于(inti=0;iTreeNode,通常是这样的
class TreeNode<T extends Comparable<T>> {
T data;
TreeNode<T> left, right;
TreeNode(T data){
this.data = data;
}
public TreeNode<T> findNextLargerNode(T t) {
if (data.compareTo(t) <= 0)
return right == null ? null : right.findNextLargerNode(t);
T found = left == null ? null : left.findNextLargerNode(t);
return found == null ? this : found;
}
}
类树节点{
T数据;
树节点左,右;
树节点(T数据){
这个数据=数据;
}
公共树节点findNextLargerNode(T){
如果(data.compareTo)(t)一个树节点通常是这样的
class TreeNode<T extends Comparable<T>> {
T data;
TreeNode<T> left, right;
TreeNode(T data){
this.data = data;
}
public TreeNode<T> findNextLargerNode(T t) {
if (data.compareTo(t) <= 0)
return right == null ? null : right.findNextLargerNode(t);
T found = left == null ? null : left.findNextLargerNode(t);
return found == null ? this : found;
}
}
类树节点{
T数据;
树节点左,右;
树节点(T数据){
这个数据=数据;
}
公共树节点findNextLargerNode(T){
如果(data.compareTo(t)我看到一个可能失败的极端测试:如果正确答案是一个具有as dataInteger.MAX_值的节点,那么代码将返回null
,而不是该节点
代码更改最少的解决方案是替换:
count[i].data<nextLarger
count[i].data我看到一个可能失败的极端测试:如果正确答案是一个具有as dataInteger.MAX_值的节点,那么代码将返回null
,而不是该节点
代码更改最少的解决方案是替换:
count[i].data<nextLarger
count[i].data最后,我在我的代码中找到了bug。它位于下面的部分
if(next!=null)
{
if(root.data>n && root.data<next.data)
return root;
else
return next;
}
else
return null;
最后,我在我的代码中发现了这个bug
if(next!=null)
{
if(root.data>n && root.data<next.data)
return root;
else
return next;
}
else
return null;
试一试
试一试
由于这是一棵树,我建议您使用树结构来查找下一个元素,而不是使用迭代。如果您想知道如何执行此操作,我建议您阅读一个类似于TreeMap源代码的示例,或者在线阅读其他内容。提示:不要使用size()、数组、迭代。Hint2:如果您要调用size(),不要多次调用它。通常树有一个左节点和一个右节点。按照使用方式,它看起来像一个复杂的数组结构,但不清楚它为什么如此复杂。@PeterLawrey它是一个泛型树。此树中的一个节点可以有两个以上的子节点。我已经在Edits中添加了treeStructure
,那么是否有这些子项
和数据
之间的y关系?为什么要有一棵树,你可以把每个元素都放在子项集合中?看来你把它概括为一个不是树的东西,而是一个复杂的数组列表。请定义“下一步”。你的代码搜索值>n。因为你的树没有排序(对吗?),似乎有可能在与起点相同的距离内有许多“较大节点”。查看您的代码,似乎最后一个孩子next
应该赢得比赛。但是如果(root.data>n&&root.datas由于这是一棵树,我建议您使用树结构来查找下一个元素,而不是使用迭代。如果您想知道如何执行此操作,我建议您阅读一个类似于TreeMap源代码的示例或在线其他内容。提示:不要使用size()、数组、迭代。提示2:如果您要调用size()),不要多次调用它。通常树有一个左节点和一个右节点。按照使用方式,它看起来像一个复杂的数组结构,但不清楚它为什么如此复杂。@PeterLawrey它是一个泛型树。此树中的一个节点可以有两个以上的子节点。我已经在Edits中添加了treeStructure
,那么是否有这些子项
和数据
之间的y关系?为什么要有一棵树,你可以把每个元素都放在子项集合中?看来你把它概括为一个不是树的东西,而是一个复杂的数组列表。请定义“下一步”。你的代码搜索值>n。因为你的树没有排序(对吗?),似乎有可能在与起点相同的距离内有许多“较大节点”。查看您的代码,似乎最后一个孩子next
应该赢得比赛。但是如果(root.data>n&&root.datasir我提到的树是一个GENERIC@PrinceVijayPratap这被称为泛型,在树中添加列表不再是树了。先生,我想我把您混淆在了泛型术语generic
和java术语generic
之间。我的问题是我有一个树,它可能不止两个或更多e children.所以为了存储子级的引用,我使用了arrayList。@PrinceVijayPratap树中可以有任意数量的节点,但每个节点下最多有两个节点,一个在左侧,保存所有小于数据值的节点,另一个在右侧,保存所有大于数据值的节点。您需要能够清楚地说出你为什么要使用一个列表,否则你会给你自己和其他人带来更多的困惑。@PrinceVijayPratap如果你用泛型这个词来表示它不是一棵真正的树,那没有说它是什么,只是说它不是什么GENERIC@PrinceVijayPratap称为泛型,在tre中添加列表先生,我想我把你搞糊涂了,把它分为一般术语generic
和java术语generic
package stack43210199;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.junit.Assert;
public class Test {
class TreeNode<T> {
T data;
List<TreeNode<T>> children;
TreeNode(T data) {
this.data = data;
children = new ArrayList<TreeNode<T>>();
}
public TreeNode<T> findNextNode(T n,Comparator<T> comp) {
if (comp.compare(data , n) < 0) {
return this;
}
if (children.size() == 0) {
return null;
}
for (int i = 0; i < children.size(); i++) {
TreeNode<T> node= children.get(i).findNextNode(n,comp);
if(node!=null)return node;
}
return null;
}
}
@org.junit.Test
public void testForYourCode() {
TreeNode<Integer> root = buildNode(0);
TreeNode<Integer> firstChild = buildNode(5);
TreeNode<Integer> secondChild = buildNode(4);
TreeNode<Integer> thirdChild = buildNode(5);
root.children.add(firstChild);
root.children.add(secondChild);
root.children.add(thirdChild);
//Arrg - not as expected
Assert.assertEquals(secondChild, findNextLargerNode(root, 0));
}
@org.junit.Test
public void testForModifiedCode() {
TreeNode<Integer> root = buildNode(2);
TreeNode<Integer> firstChild = buildNode(5);
TreeNode<Integer> secondChild = buildNode(4);
TreeNode<Integer> thirdChild = buildNode(5);
TreeNode<Integer> fourthChild = buildNode(1);
root.children.add(firstChild);
root.children.add(secondChild);
root.children.add(thirdChild);
thirdChild.children.add(fourthChild);
//find next greater
Assert.assertEquals(firstChild, root.findNextNode(2,(a,b)->{return b-a;}));
//find next lesser
Assert.assertEquals(fourthChild, root.findNextNode(2,(a,b)->{return a-b;}));
}
@org.junit.Test
public void testForModifiedCodeComplex() {
TreeNode<Integer> root = buildNode(2);
TreeNode<Integer> firstChild = buildNode(2);
TreeNode<Integer> secondChild = buildNode(4);
TreeNode<Integer> thirdChild = buildNode(5);
TreeNode<Integer> fourthChild = buildNode(1);
TreeNode<Integer> sixthChild = buildNode(8);
firstChild.children.add(fourthChild);
firstChild.children.add(sixthChild);
root.children.add(firstChild);
root.children.add(secondChild);
root.children.add(thirdChild);
//find next greater
Assert.assertEquals(sixthChild, root.findNextNode(2,(a,b)->{return b-a;}));
//find next lesser
Assert.assertEquals(fourthChild, root.findNextNode(2,(a,b)->{return a-b;}));
}
private TreeNode<Integer> buildNode(int i) {
return new TreeNode<Integer>(new Integer(i));
}
public static TreeNode<Integer> findNextLargerNode(TreeNode<Integer> root, int n) {
if (root == null)
return root;
if (root.children.size() == 0) {
if (root.data > n) {
return root;
}
else
return null;
}
TreeNode<Integer> count[] = new TreeNode[root.children.size()];
for (int i = 0; i < root.children.size(); i++) {
count[i] = findNextLargerNode(root.children.get(i), n);
}
int nextLarger = Integer.MAX_VALUE;
TreeNode<Integer> next = null;
for (int i = 0; i < count.length; i++) {
if (count[i] != null) {
if (count[i].data > n && count[i].data < nextLarger) {
nextLarger = count[i].data;
next = count[i];
}
}
}
if (next != null) {
if (root.data > n && root.data < next.data)
return root;
else
return next;
} else {
if (root.data > n)
return root;
else
return null;
}
}
}