0

バイナリツリーに基づくテキスト形式の単純なWebサイト構造を実装するための支援が必要です。新しい「ページ」にはそれぞれ親ノードと2つの子があります。ホームから始めて、最初の「ページ」が左側のノードに追加され、次にホームが親である後続のページがその右側のノードに追加されるなど、すべてがホームに親としてリンクされます。サブカテゴリについても同じです。つまり、親「ショップ」を含むページを追加すると、ショップページが見つかるまで左下に移動し、同じ親を持つ後続のページのノードから右に最初に追加される場合は左に追加する必要があります。

これまでのノード、サイト、ページクラスのコードは次のとおりです。私の問題は、addpage methdの非再帰性にあると確信しています。これまでのところ、コードを実行すると、ショップとニュースの2つの子ノードを持つホームページが生成されますが、他のすべてのノードはnullです。また、実際のニュースは、一部ではなく、ショップの適切なノードに追加する必要がありますが、それを行う方法については少し困惑しています。

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;
}
}

注意-publicSite()は家庭教師によって実装されたため、残りはその中のaddpage呼び出しに準拠する必要があります。

4

1 に答える 1

0

二分探索ツリーではなく、ディレクトリツリーが必要です。

ノードをそのように構成すると、これはより明確になります

public class PageNode
{
    private Page page;
    private PageNode child;
    private PageNode parent;
    private PageNode nextSibling;

    //...
}
于 2011-05-22T14:43:07.277 に答える