Java 为什么方法不';你不知道一个变量指向一个静态变量吗?
我试图实现一个红黑树,它的每个节点都由另一个RBTree组成。这样我就能唱出好听的歌。第一棵树由乐队组成,每个节点(乐队)都有自己的歌曲树 因此,我尝试将一首新歌插入到Band树中Band节点的内部歌曲树中,而内部树无法识别其头部指向nilNode的静态变量。不知道为什么。 插入的方法在RBTrees的两个类中都是相同的。每次插入都会考虑静态变量“nilNode”,并根据它进行操作。当我将节点插入到外部树(bands树)时,方法会识别它。但是,当我使用一个getter从一个band节点访问内部树,并使用内部树的insert方法时,它无法识别其方法中的nilNode 插入方法如下:Java 为什么方法不';你不知道一个变量指向一个静态变量吗?,java,static-variables,red-black-tree,Java,Static Variables,Red Black Tree,我试图实现一个红黑树,它的每个节点都由另一个RBTree组成。这样我就能唱出好听的歌。第一棵树由乐队组成,每个节点(乐队)都有自己的歌曲树 因此,我尝试将一首新歌插入到Band树中Band节点的内部歌曲树中,而内部树无法识别其头部指向nilNode的静态变量。不知道为什么。 插入的方法在RBTrees的两个类中都是相同的。每次插入都会考虑静态变量“nilNode”,并根据它进行操作。当我将节点插入到外部树(bands树)时,方法会识别它。但是,当我使用一个getter从一个band节点访问内部树
1。波段树调用插入。
2。该方法首先在bandsTree中查找频带的节点。
3。然后节点使用getter获取其内部歌曲树。
4.innerTree调用相同的插入方法(他的插入方法与bands树的插入方法相同) 现在,插入方法使用搜索方法查看节点(乐队或歌曲)是否已经存在或需要创建。如果存在,则返回节点;如果不存在,则返回连接到节点的每个“松散端”的静态“nilNode”。搜索方法从树的头开始。
在第一次插入时歌曲树(当前乐队节点的)为空意味着头部指向静态“nilNode”。因此,搜索方法假设在循环的第一次检查时停止。但是它无法识别头等于nilNode,因此它会继续,当它获取nilNode的左节点(为null)并尝试使用它时,会出现错误 以下是我创建的4个类: 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
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
import java.io.Serializable;
公共类BandsRBTree实现了可序列化{
私有BandNode头;//树的头
静态BandNode nilNodeBand;//每2个(左和右)空节点连接一个Nil节点
...
公共频带树()
{
nilNodeBand=新带节点(“Nill”);
头=零节点带;
}
//******************************************************
//内部树的方法:
公共BandNode插入(BandNode z、SongNode songNde)
{
BandNode b=搜索(z.getBand().getBandName());//搜索带节点
if(b.equals(nilNodeBand))//表示带节点不存在
{
//这里没有显示,因为它不在这一部分。
因为band节点已经存在,并且条件为false
...
}
else//band已在树中。
现在更新它的内部歌曲树
{
//正在检查歌曲节点是否正常
if(songNde!=null&&songNde.getSong()!=null)
{
if(songNde.getSong().getSongName().length()>0)//歌曲的名称是好的
{
b、 getInnerTreeOfSongNames().insert(songNde);//使用歌曲的内部树
return b;//返回band节点
}
其他的
//打印错误
}
return null;//某些内容为null
}
}//插入
//在波段树中搜索:
public BandNode搜索(字符串bandNameToSearch)
{
带节点温度=头;
而(!temp.equals(nilNodeBand))
{
if(temp.getBand().getBandName().compareTo(bandNameToSearch)==0)
返回温度;
else if(bandNameToSearch.compareTo(temp.getBand().getBandName())<0)
temp=temp.getLeft();
其他的
temp=temp.getRight();
}
返回nildodeband;
}
}//类BandsRBTree-end
4。歌曲树
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
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
import java.io.Serializable;
公共类SongsRBTree实现了可序列化{
私有SongNode头;//树的头
静态SongNode nilNodeSong;//作为每个空子节点连接的Nil节点
...
//建造师
公共歌曲树()
{
nilNodeSong=新歌曲节点(新歌(“Nill”);
head=nilNodeSong;//开头是nilNode
}
公共SongNode插入(SongNode z)
{
//首次搜索:
SongNode b=搜索(z.getSong().getSongName());
...
//由于搜索方法中的错误,该方法无法到达此处
}//插入
公共SongNode搜索(字符串songNameToSearch)
{
SongNode temp=head;//这里的head是nilNode。请参见构造函数中的
while(!temp.equals(nilNodeSong))//它进入循环。尽管它不应该进入循环
{//因为temp=head,而head假设为nilNodeSong
//因为树一开始是空的
//参见构造函数
if(temp.getSong().getSongName().compareTo(songNameToSearch)==0)
返回温度;
else if(songNameToSearch.compareTo(temp.getSong().getSongName())<0)
temp=temp.getLeft();
其他的
temp=temp.getRight();