出于某种原因,我的avl树似乎不同意我的右转方法。
如果我按1,2,3的顺序加值,效果很好,但按3,2,1的顺序加值就不行了。我已经试了好几个小时了,但似乎无法解决这个问题。代码如下:
public class Node<T extends Comparable<T>> {
protected int depth;
protected T info;
protected Node<T> rightsub;
protected Node<T> leftsub;
int nr_nodes =0;
Comparable biggest;
public Node(T info, Node<T> leftsub, Node<T> rightsub, int depth) {
this.info = info;
this.rightsub = rightsub;
this.leftsub = leftsub;
this.depth = depth;
}
public Node(T info) {
this.info = info;
}
public void setInfo(T info) {
this.info = info;
}
public T getInfo(){
return this.info;
}
public Node<T> getRightsub() {
return this.rightsub;
}
public Node<T> getLeftsub() {
return this.leftsub;
}
public void setRightsub(Node<T> rightsub) {
this.rightsub = rightsub;
}
public void setLeftsub(Node<T> leftsub) {
this.leftsub = leftsub;
}
public int getDepth(Node T) {
return find_tree_height(T);
}
public void setDepth(int depth) {
this.depth = depth;
}
// public Tipp<T> lisaKirje(T kirje) {
// throw new RuntimeException();
// }
// public Tipp<T> eemaldaKirje(T kirje) {
// throw new RuntimeException();
// }
public int is_empty(Node T){
if(T.getInfo()!=null){
return 0;
}
else{
return 1;
}
}
public int find_tree_height(Node T){
if(T == null){
return -1;
}
int left = find_tree_height(T.getLeftsub());
int right = find_tree_height(T.getRightsub());
if(left > right){
return left + 1;
}
else{
return right + 1;
}
}
public Node<T> rightturn(){
Node x = this.getLeftsub();
Node T2 = x.getRightsub();
x.setRightsub(this);
this.setLeftsub(T2);
this.setDepth(max(find_tree_height(this.getLeftsub()), find_tree_height(this.getRightsub()))+1);
x.setDepth(max(find_tree_height(x.getLeftsub()), find_tree_height(x.getRightsub()))+1);
return x;
}
public Node<T> leftturn(){
Node y = this.getRightsub();
Node T2 = this.getLeftsub();
y.setLeftsub(this);
this.setRightsub(T2);
this.setDepth(max(find_tree_height(this.getLeftsub()), find_tree_height(this.getRightsub()))+1);
y.setDepth(max(find_tree_height(y.getLeftsub()), find_tree_height(y.getRightsub()))+1);
return y;
}
public int balance(Node T){
if(T==null || T.getInfo()==null){
return 0;
}
else{
return(getDepth(T.getLeftsub())-getDepth(T.getRightsub()));
}
}
public Node minValueTipp(Node node)
{
Node current = node;
while (current.getLeftsub() != null)
current = current.getLeftsub();
return current;
}
public Node<T> remove_node(T kirje){
if(this == null){
return this;
}
if(kirje.compareTo(this.getInfo())<0){
this.leftsub = remove_node(this.leftsub.getInfo());
}
if(kirje.compareTo(this.getInfo())>0){
this.rightsub = remove_node(this.rightsub.getInfo());;
}
else{
if((this.getLeftsub().getInfo()==null)||(this.getRightsub().getInfo()==null)){
T temp = null;
if(temp == this.getLeftsub()){
temp = this.rightsub.info;
}
else{
temp = this.leftsub.info;
}
if(temp==null){
temp=this.getInfo();
this.info = null;
}
else{
this.info = temp;
}
}
else{
Node temp = minValueTipp(this.getRightsub());
T võti = (T) temp.getInfo();
this.setRightsub(remove_node(this.rightsub.info));
}
}
if(this==null){
return this;
}
this.setDepth(max(find_tree_height(this.getLeftsub()), find_tree_height(this.getRightsub())));
int blnc = balance(this);
if(blnc>1 && balance(this.getLeftsub())>=0){
return rightturn();
}
if (blnc > 1 && balance(this.getLeftsub()) < 0)
{
this.setLeftsub(leftturn());
return rightturn();
}
if(blnc<(-1)&& balance(this.getRightsub())<=0){
return leftturn();
}
if(blnc<(-1)&& balance(this.getRightsub())>0){
this.setLeftsub(rightturn());
return leftturn();
}
return this;
}
public Node<T> addnode(T addvalue) {
if (addvalue.compareTo(this.getInfo()) == 0){
return this;}
if (addvalue.compareTo(this.info) < 0) {
if (this.getLeftsub() == null) {
this.leftsub = new Node<>(addvalue);
}
else{
this.leftsub = this.leftsub.addnode(addvalue);
}
}
else {
if (this.getRightsub() == null){
this.rightsub = new Node<>(addvalue);
}
else {
this.setRightsub(this.rightsub.addnode(addvalue));
}
}
int blnc = balance(this);
if(blnc < -1 ){
if(balance(this.getRightsub())==1){
setRightsub(rightturn(this.getRightsub()));
}
return leftturn(this);
}
if(blnc > 1){
if(balance(this.getLeftsub())==1){
setLeftsub(leftturn(this.getLeftsub()));
}
return rightturn(this);
}
else{
return this;
}
}
private Node<T> leftturn(Node<T> x) {
Node<T> y = x.getRightsub();
Node<T> T2 = x.getLeftsub();
y.setLeftsub(x);
x.setRightsub(T2);
x.setDepth(max(find_tree_height(x.getLeftsub()), find_tree_height(x.getRightsub()))+1);
y.setDepth(max(find_tree_height(y.getLeftsub()), find_tree_height(y.getRightsub()))+1);
return y;
}
public Node<T> rightturn(Node<T> y){
Node x = y.getLeftsub();
Node T2 = x.getRightsub();
x.setRightsub(y);
y.setLeftsub(T2);
y.setDepth(max(find_tree_height(y.getLeftsub()), find_tree_height(y.getRightsub()))+1);
x.setDepth(max(find_tree_height(x.getLeftsub()), find_tree_height(x.getRightsub()))+1);
return x;
}
目前,我的添加节点的解决方案使用了最后描述的转向方法(带参数)。
暂无答案!
目前还没有任何答案,快来回答吧!