几种常见的数据结构的JAVA实现
依旧没有文字说明,只有少量的注释,二叉查找树有很多参考资料,这里就不多说了。下面奉上JAVA代码
package utility.structure;
import java.io.Serializable;
import java.security.InvalidParameterException;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
/**
*
* @author odie.tang
* @version 1.0 11/09/09
*/
public class BinarySearchTree<E> implements Comparable<E>,Serializable{
private static final long serialVersionUID = -8154673525487170187L;
/**
* The comparator, or null if priority queue uses elements'
* natural ordering.
*/
private Comparator<? super E> comparator = null;
private E data;
private BinarySearchTree<E> parent = null;
private BinarySearchTree<E> leftChild = null;
private BinarySearchTree<E> rightChild = null;
/**
* level indicates node's level in the whole tree, eg: root's level is 1, and its children's level is 2...
*/
private int level;
/**
* to identify the whether the child is a left child or a right one
*/
enum Child{left,right};
public BinarySearchTree() {
this.level = 0;
}
public BinarySearchTree(Comparator<? super E> comparator){
this.comparator = comparator;
this.level = 0;
}
public BinarySearchTree(E root) {
this(root,null);
}
public BinarySearchTree(E root,Comparator<? super E> comparator){
this.data = root;
this.comparator = comparator;
this.level = 1;
}
private BinarySearchTree(BinarySearchTree<E> parent,Child childType,E child){
this.data = child;
this.parent = parent;
this.level = parent.level + 1;
if (childType == Child.left)
parent.leftChild = this;
else
parent.rightChild = this;
this.comparator = parent.comparator;
}
public void add(E e){
if (this.level == 0){
this.level = 1;
this.data = e;
}
else{
BinarySearchTree<
相关文档:
1. package book.io;
2.
3. import java.io.File;
4.
5. /**
6. *
7. * @author XWZ
8. * 20 ......
JDK1.4中
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
JDK1.5中,应用新特性For-Each循环
Map m = new HashMap();
......
example: 求5的阶乘。。
如下:
public class Test {
static int multiply(int n){
if(n==1||n==0)
return n;
else
return n*multiply(n-1);
}
public static void main(String[] args){
System.out.println(multiply(10));
}
......
import java.util.Random;
public class MathOperators {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//随机数的产生
int[] arr = new int[10];
Random r = new Random();
for (int m = 0; m < arr.length; m++)
{
arr[m] = ......