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();
}
}
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();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货