为什么方法不能识别指向静态变量的变量?

Why method doesn't recognize that a variable pointing to a static variable?

我正在尝试实现一个红黑树,它的每个节点都由另一个 RBTree 组成。这样我就能得到好的歌曲。第一个树是乐队,每个节点(乐队)都有它的歌曲 RBTree。

所以我尝试将一首新歌插入到band tree中Band节点的inner song tree中,而inner tree不承认它的头是指向 nilNode 的静态变量。不知道为什么。

两种classRBTrees的插入方法是一样的。每次插入都会考虑静态变量 "nilNode" 并根据它工作。当我将节点插入外部树(带树)时,该方法会识别它。但是当我从一个带节点使用 getter 到内部树,并使用内部树的插入方法时,它无法识别它的方法中的 nilNode。

插入方法是这样的:
1.带树调用插入。
2.该方法首先在bandsTree中找到band的节点。
3.然后节点使用一个getter得到他的内在歌曲Tree
4. innerTree调用相同的插入方法(他的插入方法与bands Tree的插入方法相同)。

现在插入方法使用 search 方法来查看节点(乐队或歌曲)是否已经存在或是否需要创建。如果存在它 returns 节点,如果不存在它 returns static "nilNode" 连接到节点的每个 "loose ends"。搜索方法从树的 head 开始。
第一次插入 歌曲树(当前乐队节点)是 意味着 head 指向静态 "nilNode"。所以 search 方法 假设在第一次检查循环时停止 。但它 不识别 head 等于 nilNode 所以它继续并且 error 来了当它获取 nilNode 的左节点时,它是 null 并尝试使用它。

这些是我创建的 4 个 classes: BandNode、BandsRBTree、SongNode、SongsRBTree

1. BandNode:

import java.io.Serializable;

public class BandNode implements Serializable{
    private Band band;
    private BandNode left;
    private BandNode right;
    private BandNode parent;
    private SongsRBTree innerTreeOfSongNames = new SongsRBTree(); **// another RBTree**
    ...

    public BandNode(String bandName) **// I got 4 constructors and each looks like this one**
    {   
        Band band = new Band(bandName);
        this.band = band;
        left = null;
        right = null;
        parent = null;
        innerTreeOfSongNames = new SongsRBTree();
    }
    ...
}//end of class BandNode

2。 SongNode: // 没有像 BandNode

这样的内部树
public class SongNode implements Serializable{
    private Song song;
    private SongNode left;
    private SongNode right;
    private SongNode parent;
    ...

    public SongNode(String songName) // same here 4 constructors
    {
        Song _song = new Song(songName);
        this.song = _song;
        left=null;
        right=null;
        parent=null;
    }
    ...
}//end of SongNode class

3。 BandsRBTree

import java.io.Serializable;

public class BandsRBTree implements Serializable{
    private BandNode head; // head of the tree
    static BandNode nilNodeBand; // Nil node to be connected to every 2 (left and right) null ends of a node
    ...

    public BandsRBTree()
    {
        nilNodeBand = new BandNode("Nill)");
        head = nilNodeBand;
    }

    //******************************************************
    //          methods for inner tree:

    public BandNode insert(BandNode z , SongNode songNde)
    {
        BandNode b = search(z.getBand().getBandName()); // searches for the band node
        if(b.equals(nilNodeBand)) // meaning the band node doesn't exists
        {
            //nothing to show here since it doesn't go in this part. 
            because the band node already exsits and the condition is false
            ...
        }
        else // the band is already in the tree. 
            now update it's inner tree of songs
        {   
            //checking if the song node is good
            if(songNde != null && songNde.getSong() != null)
            {
                    if(songNde.getSong().getSongName().length()>0  ) // name of the song is good
                    {
                        b.getInnerTreeOfSongNames().insert(songNde); // using the inner tree of songs
                        return b; // return the band node
                    }
                    else 
                        //print error
            }

            return null; // something was null
        }

    }//insert


    //search in the band tree:
    public BandNode search(String bandNameToSearch)
    {
        BandNode temp = head;
        while( !temp.equals( nilNodeBand)) 
        {
            if( temp.getBand().getBandName().compareTo(bandNameToSearch) == 0  )
                return temp;
            else if(bandNameToSearch.compareTo(temp.getBand().getBandName()) < 0  )
                temp = temp.getLeft();
            else
                temp = temp.getRight();
        }
        return nilNodeBand;
    }

}// class BandsRBTree end

4。歌曲RBTree

import java.io.Serializable;

public class SongsRBTree implements Serializable{
    private SongNode head; // head of the tree
    static SongNode nilNodeSong; // Nil node to be connected as every null child
    ...
    //constructor
    public SongsRBTree()
    {
        nilNodeSong = new SongNode(new Song("Nill"));
        head = nilNodeSong;  // the head is nilNode at the start
    }

    public SongNode insert(SongNode z )
    {     
        // first search:
        SongNode b = search(z.getSong().getSongName()); 

        ...
        //the method get here because of the error in the search method

    }//insert


    public SongNode search(String songNameToSearch)
    {
        SongNode temp = head; // here the head is nilNode. see in the constructor

        while( !temp.equals( nilNodeSong) ) // it enters the loop. ALTOUGH IT SHOULDN'T
        {                                   //  because temp = head. and the head suppose to be nilNodeSong
                                            //  since the tree is empty at the beginning
                                            //  see constructor
            if( temp.getSong().getSongName().compareTo(songNameToSearch) == 0  )
                return temp;
            else if(songNameToSearch.compareTo(temp.getSong().getSongName()) < 0  )
                temp = temp.getLeft();
            else
                temp = temp.getRight();
        }
        return nilNodeSong;

    }
} // end of BandsRBTree class  

总之情况是这样的:
1. 我有一个带有 10 个波段节点的波段树
2.通过bandNode的构造函数,每个节点都有一个空的内部树。
3. 我第一次尝试用inner tree的insert方法插入一首歌.
4.搜索方法无法识别树头指向nilNode。
为什么不识别?(头是 nilNode 因为它是空的)

问题出在classBandsRBTree的搜索方法中while循环的条件。该条件假设阻止该方法进入循环体,因为在 第一次插入 时,构造函数的头部是 nilNode,但该条件无法识别这一点。为什么??

请帮忙。

重现问题的唯一方法是使用搜索方法创建 class 的多个实例。比较是在一个不是最新创建的实例中完成的。 也许做你想做的事情的方法是错误的。 为了满足您的需求,实施 .equals.

就足够了

亲切的问候

每次构造 SongsRBTree 时,您都会清除静态变量的先前内容并在其位置创建一个新的 SongNode。快速分步:您创建 SongsRBTree tree1。现在 nilNodeSong 是一个 SongNode,我们称之为 nill1tree1headnill1.

现在您创建 SongsRBTree tree2。现在 nilNodeSong 是一个 SongNode,我们称之为 nill2tree2headnill2tree1head 仍然是 nill1。当您插入 coursetree1 时,它无法识别 nill1 == nill2.

解决方案:静态初始化器 覆盖 SongNode 中的 equals 方法,不要理会特殊的 static SongNode nillNodeSong。我可能会选择覆盖 equals,但两者都可以解决问题。