public class Tree {
public static void main(String[] args) {
TreeArray myarray = new TreeArray();
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
int data;
try {
do {
System.out.print("请输入一个数字:");
data = Integer.parseInt(buf.readLine());
myarray.create(data);
} while (data != 0);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
myarray.display();
}
}
class TreeArray {
private int maxSize = 16;
private int[] Btree = new int[maxSize];
public TreeArray() {
for (int i = 0; i < maxSize; i++)
Btree[i] = 0;
}
public void create(int data) {
int leverl = 1;
while (Btree[leverl] != 0) {
if (data < Btree[leverl])
leverl = leverl * 2;
else
leverl = leverl * 2 + 1;
}
Btree[leverl] = data;
}
public void display() {
for (int i = 1; i < maxSize; i++)
System.out.println("Node: " + i + " " + Btree[i]);
}
} |