y***m 发帖数: 7027 | 1 这个简单java的,有更简洁明了完善高效结构化的么,thx!
class BinaryTree {
private int data;
private int minNode = Integer.MAX_VALUE;
private BinaryTree leftpoiter;
private BinaryTree rightpoiter;
public BinaryTree() {
}
public BinaryTree(int data) {
this.data = data;
leftpoiter = null;
rightpoiter = null;
}
public void getMinNode(int key) {
getMinNode(this, key);
}
public void getMinNode(BinaryTree root, int key) {
if (root != null) {
if (root.data > key) {
minNode = (root.data < minNode) ? root.data : minNode;
getMinNode(root.leftpoiter, key);
} else
getMinNode(root.rightpoiter, key);
}
}
public boolean searchkey(int key) {
return searchkey(this, key);
}
public boolean searchkey(BinaryTree root, int key) {
if (root == null)
return false;
else if (root.data == key)
return true;
else if (key >= root.data)
return searchkey(root.rightpoiter, key);
return searchkey(root.leftpoiter, key);
}
public void createTree(int data[]) {
this.data = data[0];
for (int i = 1; i < data.length; i++) {
this.insertTree(this, data[i]);
}
}
public void insertTree(BinaryTree root, int data) {
if (data >= root.data) {
if (root.rightpoiter == null)
root.rightpoiter = new BinaryTree(data);
else
insertTree(root.rightpoiter, data);
} else {
if (root.leftpoiter == null)
root.leftpoiter = new BinaryTree(data);
else
insertTree(root.leftpoiter, data);
}
}
} |
|