《Java语言程序设计与数据结构》编程练习答案(第二十三章)(二)
英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition
23.7
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}
class Heap<T extends Comparable<T>>{
private ArrayList<T> list = new ArrayList<>();
public Heap(T[] objects){
for (T object : objects) {
add(object);
}
}
public void add (T newObject){
list.add(newObject);
int currentIndex = list.size()-1;
while(currentIndex > 0){
int parentIndex = (currentIndex-1) / 2;
if(list.get(currentIndex).compareTo(list.get(parentIndex)) < 0){
T tmp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, tmp);
}
else {
break;
}
currentIndex = parentIndex;
}
}
public T remove(){
if(list.size() == 0){
return null;
}
T removeObject = list.get(0);
list.set(0, list.get(list.size()-1));
list.remove(list.size()-1);
int currentIndex = 0;
while(currentIndex < list.size()){
int leftChildIndex = 2*currentIndex + 1;
int rightChildIndex = 2*currentIndex + 2;
if(leftChildIndex >= list.size()){
break;
}
int minIndex = leftChildIndex;
if(rightChildIndex < list.size()){
if(list.get(minIndex).compareTo(list.get(rightChildIndex)) > 0){
minIndex = rightChildIndex;
}
}
if(list.get(currentIndex).compareTo(list.get(minIndex)) > 0){
T tmp = list.get(minIndex);
list.set(minIndex, list.get(currentIndex));
list.set(currentIndex, tmp);
currentIndex = minIndex;
}
else {
break;
}
}
return removeObject;
}
}
23.8
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
public static <T extends Comparable<T>> void insertionSort(T[] list){
for(int i=0;i<list.length;i++){
T currentElement = list[i];
int k;
for(k=i-1;k>=0 && list[k].compareTo(currentElement) > 0;k--){
list[k+1] = list[k];
}
list[k+1] = currentElement;
}
}
public static <T> void insertionSort(T[] list, Comparator<T> comparator){
for(int i=0;i<list.length;i++){
T currentElement = list[i];
int k;
for(k=i-1;k>=0 && comparator.compare(list[k], currentElement) > 0;k--){
list[k+1] = list[k];
}
list[k+1] = currentElement;
}
}
}
23.9
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
public static <T extends Comparable<T>> void heapSort(T[] list){
HeapWithComparator<T> heapWithComparator = new HeapWithComparator<>();
for (T t : list) {
heapWithComparator.add(t);
}
for(int i=list.length-1;i>=0;i--){
list[i] = heapWithComparator.remove();
}
}
public static <T> void heapSort(T[] list, Comparator<? super T> comparator){
HeapWithComparator<T> heapWithComparator = new HeapWithComparator<>(comparator);
for(T t: list){
heapWithComparator.add(t);
}
for(int i=list.length-1;i>=0;i--){
list[i] = heapWithComparator.remove();
}
}
}
class HeapWithComparator<T> {
private ArrayList<T> list = new ArrayList<>();
private Comparator<? super T>comparator;
public HeapWithComparator(){
this.comparator = new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
return o1.toString().compareTo(o2.toString());
}
};
}
public HeapWithComparator(Comparator<? super T> comparator){
this.comparator = comparator;
}
public void add(T newObject){
list.add(newObject);
int currentIndex = list.size()-1;
while(currentIndex > 0){
int parentIndex = (currentIndex-1) / 2;
if(comparator.compare(list.get(currentIndex),list.get(parentIndex)) > 0){
T tmp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, tmp);
}else{
break;
}
currentIndex = parentIndex;
}
}
public T remove(){
if(list.size() == 0)
return null;
T removeObject = list.get(0);
list.set(0, list.get(list.size()-1));
list.remove(list.size()-1);
int currentIndex = 0;
while(currentIndex < list.size()){
int leftChildIndex = 2*currentIndex + 1;
int rightChildIndex = 2*currentIndex + 2;
if(leftChildIndex >= list.size())
break;
int maxIndex = leftChildIndex;
if(rightChildIndex < list.size()){
if(comparator.compare(list.get(maxIndex), list.get(rightChildIndex)) < 0){
maxIndex = rightChildIndex;
}
}
if(comparator.compare(list.get(currentIndex), list.get(maxIndex)) < 0){
T tmp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, tmp);
currentIndex = maxIndex;
}
else{
break;
}
}
return removeObject;
}
public int getSize(){
return list.size();
}
}
23.10
🐎
23.11
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
}
class Heap<T extends Comparable<T>> implements Cloneable{
private ArrayList<T> list = new ArrayList<>();
public Heap(){
}
public Heap(T[] objects){
for(T t: objects){
add(t);
}
}
public void add(T newObject){
}
public T remove(){
return null;
}
public int size(){
return list.size();
}
public Heap<T> clone(){
Heap<T> ret= new Heap<>();
for(T t: this.list){
ret.add(t);
}
return ret;
}
public boolean equals(Object o){
Heap<T> target = (Heap<T>)o;
Heap<T> avatar = target.clone();
Heap<T> shadow = this.clone();
if(this.size() != avatar.size()){
return false;
}
else{
for(int i=0;i<this.size();i++){
if(!shadow.remove().equals(avatar.remove())){
return false;
}
}
}
return true;
}
}
23.12
public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
}
public static void bucketSort(int[] list){
ArrayList<Integer> buckets[] = new ArrayList[10];
for(int i=0;i<10;i++){
buckets[i] = new ArrayList<Integer>();
}
for(int i=0;i<7;i++){
for(int j=0;j<list.length;j++){
//get key
int key = list[j]/((int)Math.pow(10,j)) % 10;
buckets[key].add(list[j]);
//rebuild
int currentPtr = 0;
for(int k=0;k<10;k++){
if(!buckets[k].isEmpty()){
for(int m=0;m<buckets[k].size();m++){
list[currentPtr] = buckets[k].get(m);
currentPtr++;
}
}
}
}
}
}
}