Java2叉排序树
public class TreeTest {
private Node root = null;
public static void main(String[] args) {
TreeTest hw = new TreeTest();
for (int i = 0; i < 5; i++)
hw.add(i);
hw.preOrder();
}
public void add(int i) {
insert(root,i);
}
public void insert(Node root,int data) {
if (root == null){
root = new Node(data);
}
else if (root.data > data)
insert(root.left, data);
else
insert(root.right, data);
}
public void preOrder() {
preOrder(root);
}
public void preOrder(Node root) {
if (root == null){
System.out.println("null");
return;
}
System.out.println(root.data);
preOrder(root.left);
preOrder(root.right);
}
///
class Node {
int data;
Node left = null;
Node right = null;
public Node() {
}
public Node(int data) {
this.data = data;
}
}
}
java
数据结构
--------------------编程问答--------------------
算法有问题,求解释.
补充:Java , Java SE