package heap;
class SearchTreeInt{
private int data;
private SearchTreeInt lc;
private SearchTreeInt rc;
public boolean equals(Object obj){
if (obj instanceof SearchTreeInt){
SearchTreeInt elem;
elem= (SearchTreeInt) obj;
if (elem.lc==null && elem.rc==null)
return this.data == elem.data && this.lc==null && this.rc==null;
if (elem.lc==null && elem.rc != null)
return this.data == elem.data && this.lc==null && this.rc.equals(elem.rc);
if (elem.lc!=null && elem.rc == null)
return this.data == elem.data && this.rc==null && this.lc.equals(elem.lc);
if (elem.lc!=null && elem.rc != null)
return this.data == elem.data && this.lc.equals(elem.lc) && this.rc.equals(elem.rc);
}
return false;
}
public SearchTreeInt(){
data = 0;
lc = null;
rc = null;
}
public SearchTreeInt copy(){
if (lc==null) // empty tree
return new SearchTreeInt();
else {
SearchTreeInt out = new SearchTreeInt();
out.data = data;
out.lc = lc.copy();
out.rc = rc.copy();
return out;
}
}
public SearchTreeInt(int root){
data = root;
lc = new SearchTreeInt();
rc = new SearchTreeInt();
}
public boolean emptyTree(){
return (lc == null);
}
public boolean insert(int o) {
boolean out = false;
if (emptyTree()){
data = o;
lc = new SearchTreeInt();
rc = new SearchTreeInt();
return true;
}
else{
//int n = o.compareTo(data);
int n = (o - data);
if (n == 0) {}
else if (n < 0)
out = lc.insert(o);
else
out = rc.insert(o);
return out;
}//else
}//insert
public String toString(){
String out;
if (emptyTree()) out = "empty";
else
out = "tree(" + data + ",lc(" + lc + "),rc(" + rc + "))";
return out;
}
/*
public String toString(){
String out = "[";
ObjetoOrdenado[] inOrden = toArray();
for (int i = 0;i<inOrden.length;i++)
out += inOrden[i] + ",";
return out;
}
*/
/*
public ObjetoOrdenado[] toArray(){
ObjetoOrdenado[] out = new ObjetoOrdenado[numData];
toArrayRec(out,0);
return out;
}
private int toArrayRec(ObjetoOrdenado[] array,int i){
if (!emptyTree()){
int j = lc.toArrayRec(array,i);
array[j] = data;
j++;
j = rc.toArrayRec(array,j);
return j;
}
else return i;
}
*/
/*
public static void main(String args[]) throws Exception{
SearchTreeInt tree = new SearchTreeInt();
tree.insert(5);tree.insert(3);
SearchTreeInt tree2 = tree.copy();
tree.insert(7);
tree.insert(1);tree.insert(2);
System.out.println(tree);
System.out.println(tree2);
}
*/
}
|