链表中的数据区,使用结构体存储:
class Object{
public int value;
}
树类创建:创建一个树类Tree,在类中创建一个根节点。
链表类Node:
public class Node{
public Object data;
public Node leftNode;
public Node rightNode;
public Node(Object value){
this.data=value;
this.leftNode=null;
this.rightNode=null;
}
public(){}
public void setData(Object data){
this.data = data;
}
public Object getData(){
return data;
}
public void setLeftNode(Node node){
this.leftNode=node;
}
public Node getLeftNode(){
return leftNode;
}
public void setRightNode(Node node){
this.lRightNode=node;
}
public Node getRightNode(){
return RightNode;
}
}
树类Tree:
public class Tree{
public Node root;
public Node temp;
public Tree(){
root=null;
}
public boolead isEmpty(){
if(root==null){ return true;}
return false;
}
public void addTreeNode(Object data){
if(root!=null){
if(data.value<root.data.value){//左
if(roo.getLeftNode()!=null){
if(data.value==root.data.value){
System.out.println(“此值树中已有,请换值!”);
}else{
root=root.getLeftNode();
addTreeNode(data);
}
}else{roo.setLeftNode(new Node(data));}
}else if(data.value>root.data.value){//右
if(root.getRightNode()!=null){
if(data.value==rootgetRightNode().data.value){
System.out.println(“此值树中已有,请换值!”);
}else{
root=root.getRightNode();
addTreeNode(data);
}
}else{root.setRight(new Node(data));}
}else{//相等
System.out.println(“此值树中已有,请换值!”);
}
}else{
root=new Node(data);
}
}
}