二叉树的 StackOverflowError
对于左子树(右同级树)使用以下插入方法似乎会在该方法的私有版本中再次调用 addpage
的行处导致 StackOverflowError
。任何人都可以帮忙建议如何修复它吗?抱歉,如果之前有人问过这个问题。
public PageNode addPage(String PageName)
{
PageNode ParentNode=new PageNode();
ParentNode.page=currentPage.page;
if (this.homePage==null)
this.homePage=ParentNode.parent;
else
ParentNode=this.addPage(PageName,ParentNode.parent);
return ParentNode;
}
private PageNode addPage(String PageName, PageNode ParentNode)
{
ParentNode = new PageNode();
ParentNode.page=new Page(PageName);
if (this.currentPage.page.compareTo(ParentNode.page)==0)
{
System.out.println("attempt to insert a duplicate");
}
else
if (ParentNode.page.compareTo(currentPage.page)<0)
if(currentPage.firstchild == null)
currentPage.firstchild=ParentNode;
else
ParentNode = addPage(PageName, ParentNode.firstchild);
else if(currentPage.nextsibling == null)
currentPage.nextsibling=ParentNode;
else
ParentNode = addPage(PageName, ParentNode.nextsibling);
return ParentNode;
}
Have the following insertion method for a left child - right sibling tree - seems to be causing a StackOverflowError
at the line that called addpage
again within the private version of the method. Can anyone help advise how it can be fixed? Sorry if this has been asked before.
public PageNode addPage(String PageName)
{
PageNode ParentNode=new PageNode();
ParentNode.page=currentPage.page;
if (this.homePage==null)
this.homePage=ParentNode.parent;
else
ParentNode=this.addPage(PageName,ParentNode.parent);
return ParentNode;
}
private PageNode addPage(String PageName, PageNode ParentNode)
{
ParentNode = new PageNode();
ParentNode.page=new Page(PageName);
if (this.currentPage.page.compareTo(ParentNode.page)==0)
{
System.out.println("attempt to insert a duplicate");
}
else
if (ParentNode.page.compareTo(currentPage.page)<0)
if(currentPage.firstchild == null)
currentPage.firstchild=ParentNode;
else
ParentNode = addPage(PageName, ParentNode.firstchild);
else if(currentPage.nextsibling == null)
currentPage.nextsibling=ParentNode;
else
ParentNode = addPage(PageName, ParentNode.nextsibling);
return ParentNode;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重写方法以迭代而不是递归。
Java 没有实现尾递归删除。
rewrite the methods to iterate instead of recurse.
Java doesn't implement tail recursion removal.
您正在尝试实现二叉搜索树。您应该保证插入/删除/查找操作在搜索树中花费
O(logn)
时间。您的addPage
方法不会重新平衡树,因此在最坏的情况下,树的高度为O(n)
。如果您不熟悉该表示法,请参阅Big-O 表示法。了解红黑/AVL 树。您正在使用递归来添加新节点。由于树的高度可能达到
O(n)
,因此超出了堆栈大小的限制。我不知道 JVM 中每个线程的堆栈大小的默认上限。
You are trying to implement a binary search tree. You should guarantee that insert/delete/lookup operations take
O(logn)
time in your search tree. YouraddPage
method doesn't rebalance the tree so in the worst case, the height of your tree isO(n)
. See Big-O notation if you are unfamiliar with the notation. Learn about Red-Black/AVL trees.You are using recursion to add new nodes. As the height of the tree can get as big as
O(n)
, you exceed the limit on stack size.I don't know the default upper bound on stack size per thread in JVM.