我有二進制搜索樹代碼(它是硬代碼)做(插入,刪除,最大值,最小值,排序和查找),我想爲BST學習效率。我想要創建一個隨機方法來生成1000個數字,而不是輸入數字。 如何創建此方法?如何創建一個在Java中生成隨機值的方法?
public class BinarySearchTree {
private Node root;
private static class Node {
Node parent;
Node left;
Node right;
int data;
Node(int data) {
this.data = data;
}
@Override
public String toString() {
return "" + data;
}
}
public void insert(int data) {
root = insert(root, data);
}
public Node insert(Node node, int data) {
if(node == null) {
node = new Node(data);
} else if(data < node.data) {
node.left = insert(node.left, data);
node.left.parent = node;
} else {
node.right = insert(node.right, data);
node.right.parent = node;
}
return node;
}
private void swap(Node a, Node b) {
if(a.parent == null) {
root = b;
} else if(a == a.parent.left) {
a.parent.left = b;
} else {
a.parent.right = b;
}
if(b != null) {
b.parent = a.parent;
}
}
public void delete(int data) {
delete(root, data);
}
public void delete(Node node, int data) {
if(node == null) {
return;
}
else if (data == node.data) {
if(node.left == null) {
swap(node, node.right);
}
else if(node.right == null) {
swap(node, node.left);
}
else {
Node minNode = node.right;
while(minNode.left != null) {
minNode = minNode.left;
}
if(minNode.parent != node) {
swap(minNode, minNode.right);
minNode.right = node.right;
minNode.right.parent = minNode;
}
swap(node, minNode);
minNode.left = node.left;
minNode.left.parent = minNode;
}
}
// Continue searching in the left subtree.
else if(data < node.data) {
delete(node.left, data);
}
// Continue searching in the right subtree.
else {
delete(node.right, data);
}
}
public boolean lookup(int data) {
return lookup(root, data);
}
public boolean lookup(Node node, int data) {
if(node == null) {
// Can't find it.
return false;
} else if(data == node.data) {
// Found it.
return true;
} else if(data < node.data) {
// Search left subtree.
return lookup(node.left, data);
} else {
// Search right subtree.
return lookup(node.right, data);
}
}
public int minValue() {
return minValue(root);
}
public int minValue(Node node) {
Node cursor = node;
while(cursor.left != null) {
cursor = cursor.left;
}
return cursor.data;
}
public int maxValue() {
return maxValue(root);
}
public int maxValue(Node node) {
Node cursor = node;
while(cursor.right != null) {
cursor = cursor.right;
}
return cursor.data;
}
public void inorderTraversal() {
inorderTraversal(root);
}
private void inorderTraversal(Node node) {
if(node != null) {
inorderTraversal(node.left);
System.out.print(node.data + " ");
inorderTraversal(node.right);
}
}
public static void main(String[ ] args) {
BinarySearchTree bst = new BinarySearchTree();
int[ ] input = new int[ ] { 5, 10, 3, 9, 7, 8 , 1 , 4 , 6 , 10};
for(int i : input) {
bst.insert(i);
}
bst.delete(5);
bst.delete(10);
bst.delete(3);
bst.delete(7);
System.out.println("\n Sorted :");
bst.inorderTraversal();
System.out.println("\nMax Value:");
System.out.println(bst.maxValue());
System.out.println("\n Min Value:");
System.out.println(bst.minValue());
System.out.println(bst.lookup(1));
}
}
你試過Google搜索「java隨機數」嗎? – 2011-12-24 16:32:51
看看java的quickcheck:http://java.net/projects/quickcheck/pages/Home – miku 2011-12-24 16:33:38
可能的重複[在Java範圍內生成隨機整數](http://stackoverflow.com/questions/363681 /生成隨機整數範圍與Java) – 2014-07-09 17:21:20