【发布时间】:2011-08-30 14:50:29
【问题描述】:
我需要帮助实现一个基于二叉树的文本形式的简单网站结构 - 每个新“页面”都有一个父节点和两个子节点。从主页开始,设计是将第一个“页面”添加到左侧节点,然后将主页作为父节点的后续页面添加到该节点的右侧节点等。所有这些都链接到作为父节点的主页。子类别相同 - 即添加带有父“商店”的页面应该在左侧搜索,直到找到商店页面然后添加到左侧,如果它是第一个从该节点添加到右侧的具有相同父级的后续页面。
这是迄今为止节点、站点和页面类的代码。我很确定我的问题在于 addpage 方法的非递归性,因为到目前为止运行代码会生成一个主页,其中包含商店和新闻的两个子节点,但所有其他节点都为空。此外,应该将真正的新闻添加到商店的正确节点而不是 ome,但我对如何做到这一点有点困惑....
public class Site
{
public class PageNode
{
private Page page;
private PageNode firstchild;
private PageNode parent;
private PageNode nextsibling;
public PageNode()
{
this.firstchild = null;
this.parent = null;
this.nextsibling = null;
}
public PageNode(String PageName)
{
this.firstchild = null;
this.parent = null;
this.nextsibling = null;
}
public String toString()
{
return ""+page;
}
}
private PageNode currentPage;
private PageNode homePage;
public Site()
{
this.homePage=new PageNode();
this.homePage.page=new Page("Home");
this.currentPage=this.homePage;
PageNode shops=addPage("Shops",this.homePage);
addPage("News",this.homePage);
PageNode products=addPage("Products",this.homePage);
addPage("Paisley",shops);
addPage("Hamilton",shops);
PageNode kitchen=addPage("Kitchen",products);
addPage("Bedroom",products);
addPage("Kettles",kitchen);
addPage("Cookers",kitchen);
addPage("Toasters",kitchen);
}
public PageNode addPage(String PageName)
{
this.currentPage=new PageNode();
this.currentPage.page=new Page(PageName);
PageNode ParentNode=new PageNode();
ParentNode.page=currentPage.page;
if (this.homePage==null)
this.homePage=ParentNode;
else
ParentNode=this.addPage(PageName,ParentNode);
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;
ParentNode.firstchild = new PageNode();
ParentNode.firstchild.page = new Page(PageName);
}
else if(currentPage.nextsibling == null)
{
currentPage.nextsibling=ParentNode;
ParentNode.nextsibling = new PageNode();
ParentNode.nextsibling.page = new Page(PageName);
}
return ParentNode;
}
public void displayCurrentPage()
{
if (this.homePage!=null)
{
this.displayBranches(this.homePage);
}
else
System.out.println("tree is empty");
}
private void displayBranches(PageNode ParentNode)
{
if (ParentNode!=null)
{
System.out.println(ParentNode.page+" ");
System.out.print(" left: ");
if (ParentNode.firstchild!=null)
System.out.println(ParentNode.firstchild.page);
else
System.out.println("null");
System.out.print(" right: ");
if (ParentNode.nextsibling!=null)
System.out.println(ParentNode.nextsibling.page);
else
System.out.println("null");
displayBranches(ParentNode.firstchild);
displayBranches(ParentNode.nextsibling);
}
}
和页面类
public class Page implements Comparable
{
private String page;
public Page (String PageName)
{
page = PageName;
}
public String getPage()
{
return page;
}
public int compareTo(Object otherObject)
{
int result=((Page)otherObject).page.compareTo(this.page);
return result;
}
public String toString()
{
return ""+page;
}
}
请注意 - public Site() 是由导师实现的,因此 rest 需要符合其中的 addpage 调用。
【问题讨论】:
-
我很困惑如何修改我的 addpage 方法以正确添加 10 页。我已经尝试了很多排列,但这是我所拥有的最好的,没有崩溃或从无限递归中引发 stackioverflow :( 只是一个可能需要改变的指标将是一个帮助。谢谢。
-
我刚刚发现我基本上被要求将其实现为左子右兄弟树(不是导师认为这样说有用但是嘿)......
-
好吧-我的编现在正在将商店添加到家的左侧,将新闻添加到右侧,然后(出于某种原因)再次将商店添加到商店的左侧,将新闻添加到新闻的右侧-我已经编辑以上内容以显示新代码
标签: java binary-tree nodes binary-search insertion