public Node find(int data) {
Node p = tree;
while (p != null) {
if (data < p.data) {
p = p.left;
}
else if (data > p.data){
p = p.right;
}
else {
return p;
}
}
return null;
}
插入
根据大小关系找到对应的父节点,然后插入。
public void insert(int data) {
if (tree == null) {
tree = new Node(data);
return;
}
Node p = tree;
while (p != null) {
if (data > p.data) {
if (p.right == null) {
p.right = new Node(data);
return;
}
p = p.right;
} else { // data < p.data
if (p.left == null) {
p.left = new Node(data);
return;
}
p = p.left;
}
}
}