刚学JAVA 写了个binary search tree, 编译没错, 可测试没有结果[求救]

zhmid 2003-11-09 12:03:03
public class node {
public int key;
public node left, right;
public node(int k, node l, node r) {
key = k;
left = l;
right = r;
}
}

public class bst {
private node root;
public bst() {
root = null;
}
protected void insert_aux(node n, int x) {
if(n==null)
n = new node(x, null, null);
else if(x < n.key)
insert_aux(n.left, x);
else if(x > n.key)
insert_aux(n.right, x);
//else x is in the tree already
}
public void insert(int x) {
insert_aux(root, x);
}
protected void inorder_aux(node n) {
if(n!=null) {
inorder_aux(n.left);
System.out.print(n.key+" ");
inorder_aux(n.right);
}
}
public void inorder() {
inorder_aux(root);
System.out.println();
}
}

public class test {
public static void main(String args[]) {
bst t = new bst();
int a[] = {4, 2, 8, 7, 5, 1, 9, 6, 0, 3};
for(int i=0; i<10; i++)
t.insert(a[i]);
t.inorder();
}
}
...全文
51 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
wobelisk 2003-11-09
  • 打赏
  • 举报
回复
algo is wrong

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧