범용 Java 우선순위 큐 프로그램

Download Report

Transcript 범용 Java 우선순위 큐 프로그램

실습 내용
 실습 내용 (6월 13일/ 14일)
 우선 순위 큐 (Priority Queue) : 정수, 문자열
 마지막 실습 시간임.
 Java Collection Framework는 자료구조론 2 에서 다룸.
 예습
 해당 사항 없음.
 기말고사
 6월 16일 (금) 오후 6:00, 1101A
 4장(4.7절부터), 5장, 6장
 숙제: 프로그래밍 리포트 #2




숙제: 프로그래밍 리포트 #2
P227, 5장 연습 문제 19번
5장 6,8,10,11번 풀어오기
마감일 : 6월 13일 (화) : 마지막 수업 시간.
© DBLAB, SNU
6.7.3 범용 Java 우선순위 큐 프로그램(1)

우선순위 큐를 이용한 정수 정렬(1)
public class PQapp1 {
// 우선순위 큐 응용
public static void main(String[] args) {
PQapp1 pQapp = new PQapp1();
int n = 10;
// 정렬할 원소 수
PriorityKey[] a = new PriorityKey[n];
for (int i = 0; i < n; i++) {
a[i] = new PriorityItem((3*i - 13) * (3*i - 13));
// 정수를 포함하는 10개의 PriorityItem 객체를 생성하여 저장
System.out.print(a[i] + ", ");
// 배열 a[]에 저장된 PriorityItem 객체를 프린트
// : 169, 100, 49, 16, 1, 4, 25, 64, 121, 196
}
System.out.println();
pQapp.pQsort(a);
// 배열 a[]를 pQsort()로 정렬
for (int i = 0; i < n; i++) {
System.out.print(a[i] + ", ");
// 정렬된 배열 a[]의 원소를 프린트
// :196,169,121, 100, 64, 49, 25, 16, 4, 1
}
© DBLAB, SNU
범용 Java 우선순위 큐 프로그램(2)

우선순위 큐를 이용한 정수 정렬(2)
System.out.println();
} // end main()
void pQsort(PriorityKey[] a) {
// 우선순위 큐 정렬 메소드
int i;
int n = a.length;
PriorityQ pQ = new PriorityQ();
// 우선순위 큐 PriorityQ 클래스의 객체 pQ를 생성
for (i = 0; i < n; i++)
// 배열 a[]의 원소를 전부 우선순위 큐 pQ에 삽입
pQ.insert(a[i]);
for (i = n-1; i >= 0; i--)
// 우선순위 큐 pQ의 모든 원소를 다시 배열 a[]에 삽입
a[i] = pQ.delete();
} // end pQsort()
} // end PQapp1 class
© DBLAB, SNU
범용 Java 우선순위 큐 프로그램(3)

여러 가지 우선 순위 큐 원소 타입을 처리



Java의 Interface 개념을 이용
Interface를 구현하는 클래스 정의
PriorityKey Interface

추상 메소드 compareTo(), String toString()의 시그니처
정의
 compareTo() : 정수 값을 결과로 반환, 호출 형식 : k1.compareTo(k2)
 String toString() : string 값을 반환
interface PriorityKey {
// 우선순위 큐의 원소 타입
int compareTo(PriorityKey value);
// k1과 k2가 PriorityKey 타입이라고 할 때 k1.compareTo(k2)는
// k1 > k2, k1 == k2, k1 < k2에 따라 각각 +1, 0, -1을 반환
String toString();
// PriorityKey 객체를 프린트할 수 있는 스트링으로 변환
}
© DBLAB, SNU
범용 Java 우선순위 큐 프로그램(4)

PriorityKey Interface를 구현하는 정수 PriorityItem
클래스

PriorityItem(int k) 형식의 생성자 정의
 k를 매개 변수로 받아 정수 타입의 PriorityItem 클래스 객체 생성
class PriorityItem implements PriorityKey {
// 정수를 우선순위 값으로 포함하도록 구현한 클래스 PriorityItem
private int key; // 원소의 우선순위를 표현
PriorityItem (int k) {
key = k;
}
public String toString() {
return Integer.toString(key); // 정수 객체를 스트링으로 변환
}
public int compareTo(PriorityKey value) {
int a = this.key;
int b = ((PriorityItem)value).key;
return ((a == b) ? 0 : ((a > b) ? 1 : -1));
}
} // end PriorityItem class
© DBLAB, SNU
범용 Java 우선순위 큐 프로그램(5)

PriorityKey Interface를 구현하는 스트링 PriorityItem
클래스

PriorityItem(String value) 형식의 생성자 정의
 스트링 매개 변수를 기초로 스트링 타입의 PriorityItem 클래스
객체를 생성
class PriorityItem implements PriorityKey {
//스트링을 우선순위 값으로 포함하도록 구현한 클래스 PriorityItem
private String key; // 원소의 우선순위 표현
PriorityItem(String k) {
key = k;
}
public String toString() {
return key;
}
public int compareTo(PriorityKey value) {
String a = this.key;
String b = ((PriorityItem)value).key;
return a.compareTo(b); // 스트링에 대해 정의된 CompareTo() 메소드를 상속받아 사용
}
} // end PriorityItem class
© DBLAB, SNU
범용 Java 우선순위 큐 프로그램(6)

우선 순위 큐를 이용한 스트링 정렬(1)
public class PQapp2{
public static void main(String[] args){
PQapp2 pQapp = new PQapp2();
// 정렬시킬 스트링 배열을 생성
String[]Names={"Kim","Cho","Lee","Koh","Pak","Han","Yoo"};
int n = Names.length;
PriorityKey[] a = new PriorityKey[n];
for (int i = 0; i < n; i++) {
a[i] = new PriorityItem(Names[i]);
System.out.print(a[i] + ", ");
}
System.out.println();
pQapp.pQsort(a);
for (int i = 0; i < n; i++) {
System.out.print(a[i] + ", ");
}
System.out.println();
} // end main()
© DBLAB, SNU
범용 Java 우선순위 큐 프로그램(7)

우선 순위 큐를 이용한 스트링 정렬(2)
void pQsort(PriorityKey[] a) {
// 우선순위 큐 정렬 메소드
int i;
int n = a.length;
PriorityQ pQ = new PriorityQ(); // 우선순위 큐 생성
for (i = 0; i < n; i++)
// 배열 a[]의 원소를 전부 우선순위 큐 pQ에 삽입
pQ.insert(a[i]);
for (i = n-1; i >= 0; i--)
// 우선순위 큐 pQ의 모든 원소를 다시 배열 a[]에 삽입
a[i] = pQ.delete();
} // end pQsort()
} // end PQapp2 class
© DBLAB, SNU