OOP Concept

Chapter 9
Heap and Hash
1
WATTANAPONG SUTTAPAK
SOFTWARE ENGINEERING,
SCHOOL OF INFORMATION COMMUNICATION
TECHNOLOGY,
UNIVERSITY OF PHAYAO
จุดประสงค์บทเรียนที่ 9
2
 มีความรู้ความเข้าใจเรื่อง Priority Queue
 มีความรู้ความเข้าใจเรื่อง Heap และคุณสมบัติของ Heap
 สามารถเพิ่ม ลบ ข้อมูล Heap ได้
 สามารถแปลง binary tree ไปเป็น heap ได้
 มีความรู้ความเข้าใจเรื่อง hash
 สามารถแก้ปัญหาการชนอินเด็กซ์ของ hash
Priority Queue
3
 ตัวอย่างการเข้าคิวโรงพยาบาล
 ตามลาดับการรับคิว
 กรณีฉุกเฉินให้ความสาคัญก่อนคิวตามลาดับ
 ตัวอย่างเครื่องพิมพ์(printer)
 ตามลาดับคาสั่งก่อนหลัง
 ให้ความสาคัญกับคิวที่กาหนดความสาคัญ(priority)สูงกว่า
Priority Queue
4
Time Complexity
Method
Unsorted List
Sorted List
size,isEmpty
O(1)
O(1)
insert
O(1)
O(n)
min,removeMin
O(n)
O(1)
Heap
5
 efficient realization of priority queue from heap
 heap is binary tree
 The relational property of heap
 Heap-Order Property :
 Heap-Order Property : every node v > v’s parent(min heap)
: or every node v < v’s parent(max heap)
2
5
9
9
6
7
7
6
2
5
Heap
6
 efficient realization of priority queue from heap
 heap is binary tree
 The relational property of heap
 Complete Binary Tree:
 let h be the height of the heap
 for i = 0, … ,
h -1,there are 2i nodes of depth i at depth h -1,
the internal nodes are to the left of the external nodes
Heap
7
 Complete Binary Tree:
 let h be the height of the heap
 for i = 0, … , h -1,there are 2i nodes of depth i at depth
the internal nodes are to the left of the external nodes
2
0
5
1
2
depth
9
6
7
h -1,
Heap
8
Heap-Order
Property : every node v > v’s parent(min heap)
Complete Binary Tree Property : heap is complete binary tree
if 2h-1 <= nodes <= 2h-1 and fully node in h-1
2
2
5
9
5
6
7
4<=5<=7
9
6
7
10
4<=6<=7
Heap
9
2
2
5
9
6
7
5
11
10
9
7
4<=4<7
4<=7<=7
2
5
9
6
11
20
8<7<8
15
Heap Restructuring
10
 perculate Down Algorithm
31
41
59
for i=n/2 to 1
a = perculateDown(a,i);
53
26
97
58
end for
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
60
n=9
i=4
31
41
26
50
59
53
60
58
97
Heap Restructuring
11
 perculate Down
31
Algorithm
for i=n/2 to 1
i=4
41
59
a = perculateDown(a,i);
end for
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
26
97
58
60
i=4
31
41
53
26
50
59
60
58
97
Heap Restructuring
12
 perculate Down
31
Algorithm
for i=n/2 to 1
i=3
41
59
a = perculateDown(a,i);
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
26
end for
97
58
60
i=3
31
41
26
50
59
53
60
58
97
Heap Restructuring
13
 perculate Down
i=3
31
Algorithm
for i=n/2 to 1
41
58
a = perculateDown(a,i);
end for
26
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
97
59
60
i=3
31
41
26
50
58
53
60
59
97
Heap Restructuring
14
 perculate Down
31
Algorithm
for i=n/2 to 1
i=2
41
58
a = perculateDown(a,i);
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
26
end for
97
59
60
i=2
31
41
26
50
58
53
60
59
97
Heap Restructuring
15
 perculate Down
31
Algorithm
for i=n/2 to 1
i=2
26
58
a = perculateDown(a,i);
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
41
end for
97
59
60
i=2
31
26
41
50
58
53
60
59
97
Heap Restructuring
16
 perculate Down
31
Algorithm
for i=n/2 to 1
i=1
26
58
a = perculateDown(a,i);
end for
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
41
97
59
60
31
i=1
26
53
41
50
58
60
59
97
Heap Restructuring
17
 perculate Down
26
Algorithm
for i=n/2 to 1
i=1
31
58
a = perculateDown(a,i);
end for
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
41
97
59
60
26
i=1
31
53
41
50
58
60
59
97
Heap Restructuring
18
 perculate Down
min heap
26
Algorithm
for i=n/2 to 1
31
58
a = perculateDown(a,i);
end for
50
function perculateDown(a,i)
if a[i].data < a[i].L
swap(a[i],a[i].L)
a = perculateDown(a,indexof(a[i].L) )
end if
if a[i].data <a[i].R
swap(a[i],a[i].R)
a = perculateDown(a,indexof(a[i].L) )
end if
return a;
end function
53
41
60
binary tree
31
41
26
50
97
59
59
53
60
58
97
Insert Heap
19
26
31
41
insert 50
58
53
59
97
Insert Heap
20
26
31
41
50
insert 60
58
53
59
97
Insert Heap
21
26
31
41
50
58
53
59
97
60
จะเห็นว่า สามารถเพิ่มได้เลย Average Case O(1)
ยกเว้นกรณีที่ข้อมูลตัวใหม่น้อยกว่า root
Insert Heap
22
26
31
41
50
insert 25
58
53
60
59
97
Insert Heap
23
26
58
31
41
50
53
60
59
97
25
จากนั้นใช้ algorithm perculate down เพื่อย้าย 25 ขึ้นไปด้านบนสุด
Worst Case O(log n)
Insert Heap
24
25
58
26
41
50
31
60
59
97
53
กรณี average case
ถ้ามีการ insert n โหนด แต่ละโหนดมากกว่าหรือเท่ากับ root BigO คือ O(n)
กรณี worst case
ถ้ามีการ insert n โหนด แต่ละโหนดน้อยกว่า root BigO คือ O(n log n)
Remove Heap
25
25
58
26
41
50
31
60
59
53
การลบโหนดใน Heap สามารถลบได้เฉพาะโหนด root เท่านั้น
97
Remove Heap
26
58
26
41
50
31
60
59
53
การลบโหนดใน Heap สามารถลบได้เฉพาะโหนด root เท่านั้น
จากนั้นแทนที่ด้วยโหนดสุดท้าย นั่นคือ โหนดเลเวลต่าสุดขวาสุด
97
Remove Heap
27
53
58
26
41
50
31
59
60
การลบโหนดใน Heap สามารถลบได้เฉพาะโหนด root เท่านั้น
จากนั้นแทนที่ด้วยโหนดสุดท้าย นั่นคือ โหนดเลเวลต่าสุดขวาสุด
ทาการปรับโครงสร้างโดยใช้ perculate down algorithm
97
Remove Heap
28
26
58
53
41
50
31
59
60
การลบโหนดใน Heap สามารถลบได้เฉพาะโหนด root เท่านั้น
จากนั้นแทนที่ด้วยโหนดสุดท้าย นั่นคือ โหนดเลเวลต่าสุดขวาสุด
ทาการปรับโครงสร้างโดยใช้ perculate down algorithm
97
Heap
29
Time Complexity
Method
Average Case Worst Case
min
O(1)
O(1)
insert
O(1)
O(log n)
remove
O(log n)
O(log n)
build heap
O(n)
O(n log n)
Hash
30
Hash
31
 การค้นหา O(log n) โดยไม่จาเป็นต้องเรียงลาดับข้อมูล
 ใช้การแปลงค่าข้อมูลด้วย Hash Function เพื่อแปลงข้อมูลเป็นแอดเดรส
 Hash Function ตัวอย่าง H(x) = x mod R
 x เป็นข้อมูลหรือค่าของคีย์ที่ต้องการแปลงเป็นแอดเดรส
 R เป็นตัวหารและเป็นขนาดของตารางแฮช(จานวนเฉพาะ)
 H(x) เป็นข้อมูลที่ถูกแปลง
Hash
32
 ตัวอย่าง hash table เท่ากับ 11 โดยมีข้อมูลเป็น
 11 1 3 13 30 31 32 15 16 17 18
value
hash value( mod 11 )
index
value
11
0
0
11
1
1
1
1
3
3
2
13
13
2
3
3
30
8
4
15
31
9
5
16
32
10
6
17
15
4
7
18
16
5
8
30
17
6
9
31
18
7
10
32
Hash
33
 key collision
 11 1 3 13 30 31 32 15 16 17 18 5
value
hash value( mod 11 )
index
value
11
0
0
11
1
1
1
1
3
3
2
13
13
2
3
3
30
8
4
15
31
9
5
5 , 16
32
10
6
17
15
4
7
18
16
5
8
30
17
6
9
31
18
7
10
32
5
5
Hash
34
 key collision
 Open
Hashing(separate Chaining)
 Closed Hashing(Open Addressing)
Linear Probing หาที่ว่างแอดเดรสใหม่
Double Hashing เพิ่มตารางใหม่
Open Hashing
35
 เป็นวิธีที่นิยมใช้
ถ้าค่าซ้าให้เก็บเป็น list ตัวต่อไป
value
hash value( mod 10)
index
value
1
1
0
10
10
0
1
1
21
4
4
2
25
5
3
16
6
4
4
64
9
9
5
25
21
1
6
16
36
64
4
7
36
6
8
49
9
9
9
49
Linear Hashing
36
 ถ้าค่าซ้าให้หา index ตัวต่อไปที่ว่าง แล้วแทนที่ลงไป
value
hash value( mod 10)
index
value
1
1
0
10
10
0
1
1
4
4
25
index
value
0
10
1
1
2
2
21
5
3
3
49
16
6
4
4
4
4
9
9
5
25
5
25
21
1
6
16
6
16
64
4
7
7
64
36
6
8
8
36
49
9
9
9
9
9
21
64
36
49
Linear Hashing
37
 ถ้าค่าซ้าให้ใช้ค่า index = hash + double hash
value hash value( mod 10)
double(7 – value mod 7) index
index
value
1
1
6
1
0
10
10
0
4
0
1
1
4
4
3
4
2
25
5
3
5
3
16
6
5
6
4
4
9
9
5
9
5
25
21
1
7
8
6
16
64
4
6
10
7
49
36
6
6
12
8
21
49
9
7
7
9
9
10
64
11
12
36