二叉树的插入/添加方法
我需要帮助实现一个基于二叉树的文本形式的简单网站结构 - 每个新“页面”都有一个父节点和两个子节点。从主页开始,设计是将第一个“页面”添加到左侧节点,然后将主页作为父节点的后续页面添加到该节点的右侧节点,等等。所有页面都链接到作为父节点的主页。对于子类别也是如此 - 即添加具有父“商店”的页面应该向左搜索,直到找到商店页面,然后添加到左侧(如果它是第一个从该节点添加到右侧的具有相同父级的后续页面)。
这是到目前为止节点、站点和页面类的代码。我很确定我的问题在于 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() 是由导师实现的,所以需要休息以符合其中的 addpage 调用。
I need help implementing a simple website structure in text form based on a Binary Tree - each new "page" has a parent node and two children. Starting from the home the design is that the first "page" is added to the left hand node and then subsequent pages with home as the parent are added to the right node of that etc. all also linking to home as the parent. Same for sub categories - ie adding a page with parent "shops" should searh down the left until it finds shops page then add to the left if its the first to be added the right from that node for subsequent pages with the same parent.
Here's the code so far for node, site and page classes. I'm pretty sure my problem lies with non-recursiveness of the addpage methd as so far running the code produces a home page with two child nodes of shops & news but then all other nodes are null. Also reall news should be being added to the right node of shops rather than ome but I'm a bit stumped as to how to do that....
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);
}
}
and page class
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;
}
}
Please note - public Site() was implemented by tutor so rest needs to conform to the addpage calls therein.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你需要一个目录树而不是二叉搜索树,
如果你像这样构造你的节点,这会更清楚
you'll want a directory tree not a binary search tree
this will be more clear if you structure your node like so