当前位置: 技术问答>java相关
大家帮帮我。谢谢。
来源: 互联网 发布时间:2015-05-06
本文导语: 我是一个java初学者。下面是一个binary tree的程序。在jdk1.3编译成功了,但addnode没法实现,请各位大哥帮我看看。谢谢!!! class Treenode{ /* data members */ private int item; private Treenode left; private Treenode right; /*...
我是一个java初学者。下面是一个binary tree的程序。在jdk1.3编译成功了,但addnode没法实现,请各位大哥帮我看看。谢谢!!!
class Treenode{
/* data members */
private int item;
private Treenode left;
private Treenode right;
/* constractor */
public Treenode(int item){this.item=item;left=right=null;}
/* set item,left,right methods */
public void setitem(int item){this.item=item;}
public void setleft(Treenode left){this.left=left;}
public void setright(Treenode right){this.right=right;}
/* get item,left,right methods */
public int getitem(){return item;}
public Treenode getleft(){return left;}
public Treenode getright(){return right;}
/* add node method */
public void addnode(int item){
Treenode newnode=new Treenode(item);
Treenode curr=null,parent=this;
while(parent!=null){
curr=parent;
if(item>parent.getitem())
parent=parent.getleft();
else
parent=parent.getright();
}
if(curr.getitem()>item)
curr.setleft(newnode);
else
curr.setright(newnode);
}
/* display every node within the binary tree */
void display(Treenode tree){
if(tree==null) return;
System.out.println(tree.getitem());
display(tree.getleft());
display(tree.getright());
}
}
public class Btree{
public static void main(String[] args){
/* initialization the class Treenode with a instance ins */
Treenode ins=new Treenode(5);
/* a pointer tree pointes to object ins */
Treenode tree=ins;
tree.addnode(1);
tree.addnode(2);
tree.addnode(3);
tree.addnode(4);
tree.addnode(6);
tree.addnode(7);
tree.addnode(8);
tree.display(tree);
}
}
class Treenode{
/* data members */
private int item;
private Treenode left;
private Treenode right;
/* constractor */
public Treenode(int item){this.item=item;left=right=null;}
/* set item,left,right methods */
public void setitem(int item){this.item=item;}
public void setleft(Treenode left){this.left=left;}
public void setright(Treenode right){this.right=right;}
/* get item,left,right methods */
public int getitem(){return item;}
public Treenode getleft(){return left;}
public Treenode getright(){return right;}
/* add node method */
public void addnode(int item){
Treenode newnode=new Treenode(item);
Treenode curr=null,parent=this;
while(parent!=null){
curr=parent;
if(item>parent.getitem())
parent=parent.getleft();
else
parent=parent.getright();
}
if(curr.getitem()>item)
curr.setleft(newnode);
else
curr.setright(newnode);
}
/* display every node within the binary tree */
void display(Treenode tree){
if(tree==null) return;
System.out.println(tree.getitem());
display(tree.getleft());
display(tree.getright());
}
}
public class Btree{
public static void main(String[] args){
/* initialization the class Treenode with a instance ins */
Treenode ins=new Treenode(5);
/* a pointer tree pointes to object ins */
Treenode tree=ins;
tree.addnode(1);
tree.addnode(2);
tree.addnode(3);
tree.addnode(4);
tree.addnode(6);
tree.addnode(7);
tree.addnode(8);
tree.display(tree);
}
}
|
while(parent!=null){
curr=parent;
if(item>parent.getitem())
^^^^改为小于符号
parent=parent.getleft();
else
parent=parent.getright();
}
别忘了给分!!!!
curr=parent;
if(item>parent.getitem())
^^^^改为小于符号
parent=parent.getleft();
else
parent=parent.getright();
}
别忘了给分!!!!