對於左側子樹 - 右側兄弟樹有以下插入方法 - 似乎在該方法的專用版本中再次調用addpage
的行上導致StackOverflowError
。任何人都可以幫助建議如何修復它?對不起,如果之前已經問過。帶二叉樹的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;
}
考慮修復代碼格式;如果/ else沒有'{}'也會導致細微的難以辨認的錯誤,特別是。當嵌套像那樣。 (如果代碼難以閱讀,我會停下來看一個問題)。順便說一下,「查看」導致堆棧溢出的最簡單方法是查看每次調用的傳遞情況以及它如何適應調用堆棧。使用調試器(或那些基本的'println')。 – 2011-05-22 21:27:52
這應該是什麼語言?考慮適當標記。 – 2011-05-22 21:37:25