queue access element element - PowerPoint PPT Presentation

About This Presentation
Title:

queue access element element

Description:

???????????????? Heap (GPA ????????????? highest priority) ... ??????? 30 ????????????????????????????????????????? ??????????????????? heap ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 83
Provided by: willia504
Category:
Tags: access | element | heap | queue

less

Transcript and Presenter's Notes

Title: queue access element element


1
(No Transcript)
2
????????
  • ??? queue ??? access element ??????????????????
    element ????
  • ????????????????????????? ????????????????????????
    ????????????? (????????????????????)
  • Printer queue ???????????????????????

3
Priority
  • ????????? ???????????????????????????? (??
    priority ??????????????)
  • ????????????????????? ????????????????????????????
    ???????????
  • ????????????????? ?????? Comparable interface
    ???? Comparator interface

4
Comparator interface
  • ?????? Comparable ??????????
  • int compare(Object o1, Object o2)

???????? class ???????? ???? ????subclass ???
??????? throw CastCastException
5
??? interface
// Postcondition ????????????????????? priority
queue ???
// Postcondition ???????? true ??? priority
queue ?????????????? //????????????????? false
// Postcondition element ????????????? priority
queue
6
// Precondition ????????????????????? ?????????
throw // NoSuchElementException //
Postcondition ??????????????????????????????????
?? PriorityQueue ???
// Precondition ????????????????????? ?????????
throw // NoSuchElementException //
Postcondition ???????????????????????????????
PriorityQueue ??? // ?????????????????????????????
????????????????????????
7
??????? implement ??????????
  • ???????????? ???????? prioriry
  • ?????????????? ????????????? priority ??????????
  • ??? ArrayList ???????????
  • getMin ?????? constant time
  • add ??? removeMin ????? linear time -????????????

8
??????? implement (???)
  • ??? LinkedList
  • getMin ??? removeMin ???? constant time
  • add ?????? linear time
  • Ok ?????? ???????

9
??? Comparable ??? default
10
(No Transcript)
11
(No Transcript)
12
?????
13
???????? previous ?
  • ???????? 1,2,4,5 ?????????? add 3
  • Loop ??????????? itr.next() ???? 4
  • ?????? iterator ?????????????? 5 ??????
  • ??????? ???? previous() ?????? ???????????? 3

14
protected int compare (Object elem1, Object
elem2)   return (comparatornull
? ((Comparable)elem1).compareTo(elem2)
comparator.compare (elem1, elem2)) //
method compare   ??? comparator null, return
elem1.compareTo (elem2) ?????????? return
comparator.compare (elem1, elem2)
15
?????????????????
  • ???????????

16
(No Transcript)
17
public void add (Object element) set.add
(element) // method add   public Object
getMin ( ) return set.first( ) // method
getMin   public Object removeMin ( ) Object
temp set.first( ) set.remove (set.first(
)) return temp // method removeMin   ????
3 method ????? worstTime(n) ???? LOGARITHMIC ???
n.
18
?????????? ?????????? Heap
ppublic class Heap implements PriorityQueue  

???????????? JAVA
COLLECTIONS
FRAMEWORK  
 
 
19
Complete Binary tree ???????????????
20
Heap ??? complete binary tree
  • ???????? empty tree ????
  • ???????? root ??????????????? (???????? minHeap)
    ???
  • left ??? right subtree ?????? heap ????
  • (?????? binary search tree ??)

21
(No Transcript)
22
Complete binary tree ?????????????????
 
???????????? child, parent
23
???????????????? Heap (GPA ????????????? highest
priority)
24
(No Transcript)
25
(No Transcript)
26
// ?? constructor ?????? string ???? input
???????????? toString ???? ?????????????   //
Postcondition ???????????????????? lt 0, 0,
???? gt 0 ?????????????? GPA ??? // this Student
????????????????????? ??????? ??????????? GPA ???
o1
27
????????? Heap ???
28
index ???????????????
29
index ???????????????
30
?? method ?? Heap
31
// Postcondition element ????????????????????
heap // ???? worst case O(n) //
??????????????????????????
??????????????? ??????
??????????? percolateUp
//????????????????????
//???????????????????????????????
32
????????????????? 30 ????
30
33
Percolate up ???...
??????? 30 ???????????????????????????????????????
?? ??????????????????? heap
34
(No Transcript)
35
(No Transcript)
36
// Postcondition ??? heap ???????????????????????
?????? ?????????????????????? //O(log n)
????????????????????????????
protected void percolateUp()   int
parent int child size-1 Object
temp while (childgt0)   parent
(child-1)/2 if(compare(heapparent,hea
pchild) lt0) break
temp heapparent heapparent
heapchild heapchild temp
child parent // while   //
method percolateUp
???????????????? ?????????????????????????????????
??????????
37
Average time -percolateUp
  • ??????????????????????????????????????????????????
    ????
  • ??????????????????????????
  • ?????????????????????????????????? heap
    ?????????????????? constant time

38
public Object getMin()   if (size 0)
throw new NoSuchElementException(Empty)
return heap0     // method
getMin
39
removeMin() ???????????
???????????? complete binary tree ????????????
40
// Precondition ????????????????????? ?????????
throw // NoSuchElementException //
Postcondition ???????????????????????????????
PriorityQueue ??? // ?????????????????????????????
???????????????????????? // ??????????????????????
O(log n)
??????? percolateDown
//????????????????????? root
//?????????????????????????
41
26
                107 48 ?????
removeMin( ) ???????? 48 ???????????? 26 ????
size ???????????????? ??????????? percolateDown
30
31
32 50 85
36
42
PercolateDown ???
  • ???? 48 ???????????????????
  • ???????????????????? ??????????????????? heap

43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
protected void percolateDown(int start)  
int parent start int child 2parent1
Object temp while (childltsize)
if(childltsize-1compare(heapchild,heapchild1
) gt0) child if
(compare(heapparent,heapchild) lt0)
break temp heapchild
heapchild heapparent heapparent
temp parent child child
2parent1 // while   // method
percolateDown
// Postcondition ??? heap ???????????????????????
?? // ???????????????? ??????????????????? O(log
n) ???????
?????????????????? ???????????????????????????????
??
????????????????? ?????? child ?????????????
47
???????
48
(No Transcript)
49
?????????
  • ?? compress file ?????????????????????????????????
    ?
  • ??? M ???? ??????????????????? compress
    ????????????????? 100000 character
    ??????????????? a ??? e ????????
  • ??? E ??? version ??? compress ????

50
????????? (???)
  • ??????? 1 character ????? 16 bit
  • ?????? 100000 character -_- ?????????????
  • ???????????????? bit

51
???????
????? bit ????????? 3 ???
52
????????????
????? bit ????????????3??? ??? 001010
?????????????? aababa ???? cbda ?????
53
?????????????????????
  • ??? prefix-free encoding
  • ???????? binary tree
  • ??????????????? 0
  • ?????????????? 1
  • ??? character ?????????

54
??????? character ??????? path ???????????????????
?
a 010 b 11 c 00 d 10 e 011
55
??????????????????
????????????? ?????????? character
??????????????? bit ??????????????????
56
????? Huffman tree
  • ????????? binary tree ??????????????
    ???????????????????????????????????????????
    character
  • ??????????????????????????????????????????????
    ???????? bit ??????????

57
????????????????????
  • a ?? 5000
  • b ?? 10000
  • c ?? 20000
  • d ?? 31000
  • e ?? 34000

58
?????? (character,???????)???Priority Q
  • Priority Q ?????? ????????????????????????????
  • ????? node ??? priority Q ???? left, right,
    parent ?????????
  • ?????? ??????????????? (a5000) (b 10000) (c
    20000) (d 31000) (e 34000)
  • ????? ????? Huffman tree ???

59
  • ???????????????????????????? ?????????????????????
    ?? ?????????????????????? ????????????????????????
    ??????????

0
1
  • ????????????????????????? priority Q
    ?????????????? ( 15000) (c 20000) (d 31000)
    (e 34000)

60
  • ???????????????????????????? ????????????
  • ????????????????????????? priority Q
    ???????????????? (d 31000) (e 34000) ( 35000)

61
  • ???????????????????????????? ????????????
  • ?????????????? tree ????????
  • ????????????????????????? priority Q
    ???????????????? ( 35000) ( 65000)

62
  • ????????????????????????? ????????????
  • priority Q ?????????? ( 100000)

63
???????
64
Huffman ???? Greedy Algorithm
  • ????????????????????????????????? ??????????????
    min ?????? tree
  • ??????????????? ???????? optimal solution ??????
    ?????????????? prefix-free code ????????

65
Huffman class
  • ????????? input ????????????????? Huffman
  • ?????????? Entry ?????????

66
(No Transcript)
67
??????????????????????
???????? access ???????
68
???????????? c ?? index 99
??????????? Entry
69
// Postcondition This Huffman object ???
initialize
// Postcondition ???? ?????????????? s
??????????????????
// Postcondition priority queue
???????????????????? input //file ??????? worst
case O(n)
70
// Postcondition ????? huffman tree ????worst
case ?????????
// Postcondition ????? huffman code
???????????????? // ????worst case ?????????
// Postcondition Huffman codes???????????????????
??????????????????? // ????worst case ???? O
(n).
71
Fields ?? Huffman class
  • Gui
  • priority queue
  • file reader
  • file writer
  • input file name ??????????????????????? 2 ??
  • ????????????
  • encode ?????????
  • boolean ??????????????????????????

72
(No Transcript)
73
Method processInput
  • ????? priority Q
  • ????? Huffman tree
  • ????????????
  • ??????????????????????????????????????

74
Method createPQ
  • ????? leafEntries
  • update freq field ???????? Entry
  • ?????????????????? priority queue ???????? freq

75
Method createHuffmanTree
  • Loop ?????? size ??? priority queue ??????? 1
  • ?????????????????????????

76
Method calculateHuffmanCodes
  • Loop ?? leafEntries
  • ????? Entry ????
  • ???????????????? empty string
  • ?? parent ???? ??????????????? parent
    ??????????????? string ??????? (???????????????
    prepend) ???????????? root

77
????????????????????
????????????????????????
???????????
78
??????? f
????????????????????????
f ????? int ???? 102 ????????????? set ???
leafEntries102.code 1010
79
Method saveToFile
  • Loop ?? leafEntries
  • ??? character ??? code ???????????????
  • ?????????? input file ?????
  • ??????? input ?????????????? ??? save ?????????

80
?? web ????????
81
????? output file ?? ???????? Huffman tree
?????????????????????????
82
???????? priority queue
Write a Comment
User Comments (0)
About PowerShow.com