StackOverflowError with a binary tree

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();; 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(); Page(PageName); if ( { System.out.println("attempt to insert a duplicate"); } else if (<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; }
Consider fixing the code formatting; also if/else without {} can lead to subtle hard-to-spot bugs, esp. when nested like that. (I stop looking at a question if the code is hard to read.) That asside, the easiest way to "see" what causes a stack overflow is to see what each invocation is passed and how it fits into the call-stack. Use a debugger (or those primitive println's).

以上就是StackOverflowError with a binary tree的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准