package ch01;
import java.util.Random;
/**
* 练习1.14 设计一个泛型类OrderedCollection,它存储Comparable的对象的集合(在数组中),以及
* 该集合的当前大小。提供public方法isEmpty、makeEmpty、insert、remove、findMin和
* findMax。findMin和findMax分别返回该集合中最小和最大的Comparable对象的引用,如果
* 该集合为空,则返回null。
*
*/
class OrderedCollection<T extends Comparable<T>> {
private final static int DEFAULT_CAPACITY = 10;
private Comparable<T>[] items;
private int size;
public OrderedCollection() {
size = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public boolean isEmpty() {
return size == 0;
}
public void makeEmpty() {
if (size == 0) {
return;
}
for (int i = 0; i < size; ++i) {
items[i] = null;
}
size = 0;
}
public boolean insert(T x) {
//insert(size(), x);
int index = binarySearch0(0, size() - 1, x);
if (index >= 0)
insert(index, x);
else
insert(-index - 1, x);
return true;
}
private void insert(int idx, T x) {
if (items.length == size())
ensureCapacity(size() * 2 + 1);
for (int i = size; i > idx; i--)
items[i] = items[i - 1];
items[idx] = x;
size++;
}
/**
* 这个函数是从Arrays.java中摘抄过来的。
*/
private int binarySearch0(int fromIndex, int toIndex,
T key) {
int low = fromIndex;
int high = toIndex;
while (low <= high) {
int mid = (low + high) >>> 1;
T midVal = (T)items[mid];
if (midVal.compareTo(key) < 0)
low = mid + 1;
else if (midVal.compareTo(key) > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
private T remove(int idx) {
T removedItem = (T)items[idx];
for (int i = idx; i < size() - 1; ++i)
items[i] = items[i+1];
items[size() - 1] = null;
size--;
return removedItem;
}
public boolean remove(T x) {
int index = 0;
boolean found = false;
while (index >= 0) {
index = binarySearch0(0, size() - 1, x);
if (index >= 0) {
remove(index);
found = true;
}
}
return found;
}
public boolean isPresent(T x) {
int index = binarySearch0(0, size() - 1, x);
if (index >= 0)
return true;
return false;
}
public T findMin() {
if (size() != 0)
return (T)items[0];
return null;
}
public T findMax() {
if (size() != 0)
return (T)items[size() - 1];
return null;
}
private void ensureCapacity(int newCapacity) {
if (newCapacity < size)
return;
Comparable<T>[] old = items;
items = (Comparable<T>[])new Comparable[newCapacity];
for (int i = 0; i < size(); ++i)
items[i] = old[i];
}
public int size() {
return size;
}
public void display() {
for (int i = 0; i < size(); ++i) {
System.out.print(items[i] +" ");
}
//System.out.println();
}
}
public class EX14 {
public static void main(String[] args) {
OrderedCollection<Integer> intColl = new OrderedCollection<Integer>();
Random rand = new Random(47);
for (int i = 0; i < 15; ++i)
intColl.insert(rand.nextInt(15) + 1);
System.out.print("The Ints: "); intColl.display();System.out.println();
System.out.println("isEmpty: " + intColl.isEmpty());
System.out.println("if removed 6: " + intColl.remove(6));
System.out.println("if removed 11: " + intColl.remove(11));
System.out.println("if removed 14: " + intColl.remove(14));
System.out.print("The Ints: "); intColl.display();System.out.println();
System.out.println("find min: " + intColl.findMin());
System.out.println("find max: " + intColl.findMax());
System.out.print("make it empty:");intColl.makeEmpty();
intColl.display();
System.out.println();
System.out.println("isEmpty: " + intColl.isEmpty());
}
}
数据结构与算法分析Java版练习1.14
最新推荐文章于 2021-03-23 15:20:59 发布