View
230
Download
2
Category
Preview:
Citation preview
1
Heap and Others
黃兆武
2
Heap• A max tree is a tree in which the key value in
each node is no smaller than the key values in its children. A max heap is a complete binary tree that is also a max tree.
• A min tree is a tree in which the key value in each node is no larger than the key values in its children. A min heap is a complete binary tree that is also a min tree.
• Operations on heaps– creation of an empty heap– insertion of a new element into the heap; – deletion of the largest element from the heap
3
*Figure 5.25: Sample max heaps (p.219)
[4]
14
12 7
810 6
9
6 3
5
30
25
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
Property:The root of max heap (min heap) contains the largest (smallest).
4
2
7 4
810 6
10
20 83
50
11
21
[1]
[2] [3]
[5] [6]
[1]
[2] [3]
[4]
[1]
[2]
[4]
*Figure 5.26:Sample min heaps (p.220)
5
Application: priority queue
• machine service– amount of time (min heap)– amount of payment (max heap)
• factory– time tag
6
Example of Insertion to Max Heap
20
15 2
14 10
initial location of new node
21
15 20
14 10 2
insert 21 into heap
20
15 5
14 10 2
insert 5 into heap
7
Insertion into a Max Heapvoid insert_max_heap(element item, int *n){ int i; if (HEAP_FULL(*n)) { fprintf(stderr, “the heap is full.\n”); exit(1); } i = ++(*n); while ((i!=1)&&(item.key>heap[i/2].key)) { heap[i] = heap[i/2]; i /= 2; } heap[i]= item;}
2k-1=n ==> k=log2(n+1)O(log2n)
8
Example of Deletion from Max Heap
20remove
15 2
14 10
10
15 2
14
15
14 2
10
9
Deletion from a Max Heapelement delete_max_heap(int *n){ int parent, child; element item, temp; if (HEAP_EMPTY(*n)) { fprintf(stderr, “The heap is empty\n”); exit(1); } /* save value of the element with the highest key */
item = heap[1]; /* use last element in heap to adjust heap */ temp = heap[(*n)--]; parent = 1; child = 2;
10
while (child <= *n) { /* find the larger child of the current parent */ if ((child < *n)&& (heap[child].key<heap[child+1].key)) child++; if (temp.key >= heap[child].key) break; /* move to the next lower level */ heap[parent] = heap[child]; child *= 2; } heap[parent] = temp; return item;}
11
26[1]
5[2] 77[3]
1[4] 61[5] 11[6] 59[7]
15[8] 48[9] 19[10]
Heap Sort
1 2 3 4 5 6 7 8 9 1026 5 77 1 61 11 59 15 48 19
input file
12
77[1]
61[2] 59[3]
48[4] 19[5] 11[6] 26[7]
15[8] 1[9] 5[10]
initial heap
exchange
Heap Sort
13
61[1]
48[2] 59[3]
15[4] 19[5] 11[6]26[7]
5[8] 1[9] 77[10]
59[1]
48[2] 26[3]
15[4] 19[5] 11[6]1[7]
5[8] 61[9] 77[10]
(a)
(b)
Heap Sort
14
48[1]
19[2] 26[3]
15[4] 5[5] 11[6]1[7]
59[8] 61[9] 77[10]
26[1]
19[2] 11[3]
15[4] 5[5] 1[6]48[7]
59[8] 61[9] 77[10]
(c)
(d)
59
6159
48
Heap Sort
15
Heap Sortvoid adjust(element list[], int root, int n){ int child, rootkey; element temp; temp=list[root]; rootkey=list[root].key; child=2*root; while (child <= n) { if ((child < n) && (list[child].key < list[child+1].key)) child++; if (rootkey > list[child].key) break; else { list[child/2] = list[child]; child *= 2; } } list[child/2] = temp;}
i2i 2i+1
16
Heap Sort
void heapsort(element list[], int n)
{
int i, j;
element temp;
for (i=n/2; i>0; i--) adjust(list, i, n);
for (i=n-1; i>0; i--) {
SWAP(list[1], list[i+1], temp);
adjust(list, 1, i);
}
}
ascending order (max heap)
n-1 cylces
top-down
bottom-up
17
AVL樹 (Balanced Binary Tree)
1.T是一個非空的二元樹, Tl及 Tr分別是它的左右子樹,若符合下列兩條件,則稱T為高度平衡樹。( Height Balancing Binary Tree)
(1) TL及 TR也是高度平衡樹 (2) | HL-HR | <= 1 , HL及 HR分別為 TL及 TR的高度。
X ○
18
1-2. Route balanced treeA binary tree with n nodes, where all nodes are at depth └ lg(n) ┘ or less, and where there are exactly 2d nodes at depth d for each depth 0 <= d < └ lg(n) ┘ , will be called route balanced.
Perfectly balanced tree
All six are route balanced
19
Threaded Binary Trees• Two many null pointers incurrent
representation of binary trees n: number of nodes number of non-null links: n-1 total links: 2n null links: 2n-(n-1)=n+1
• Replace these null pointers with some useful “threads”.
20
Threaded Binary Trees (Continued)
If ptr->left_child is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal
If ptr->right_child is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal
21
A Threaded Binary Tree
A
B C
GE
I
D
H
F
root
dangling
dangling
inorder traversal:H, D, I, B, E, A, F, C, G
22
TRUE FALSE
Data Structures for Threaded BT
typedef struct threaded_tree *threaded_pointer;
typedef struct threaded_tree {
short int left_thread;
threaded_pointer left_child;
char data;
threaded_pointer right_child;
short int right_thread; };
left_thread left_child data right_child right_thread
FALSE: childTRUE: thread
23
Memory Representation of A Threaded BT
f f--
f fA
f fCf fB
t tE t tF t tGf fD
t tIt tH
root
24
Next Node in Threaded BTthreaded_pointer insucc(threaded_pointer tree)
{
threaded_pointer temp;
temp = tree->right_child;
if (!tree->right_thread)
while (!temp->left_thread)
temp = temp->left_child;
return temp;
}
25
Inorder Traversal of Threaded BTvoid tinorder(threaded_pointer tree){/* traverse the threaded binary tree inorder */ threaded_pointer temp = tree; for (;;) { temp = insucc(temp); if (temp==tree) break; printf(“%3c”, temp->data); }}
O(n)
26
Inserting Nodes into Threaded BTs
• Insert child as the right child of node parent– change parent->right_thread to FALSE– set child->left_thread and child->right_thread to
TRUE– set child->left_child to point to parent– set child->right_child to parent->right_child– change parent->right_child to point to child
27
Examples
root
parent
A
B
C Dchild
root
parent
A
B
C D child
empty
Insert a node D as a right child of B.
(1)
(2)
(3)
28
*Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree (p.217)
nonempty
(1)
(3)
(4)
(2)
29
Selection Trees
(1) winner tree(2) loser tree
30
winner tree6
6 8
9 6 8 17
8 9 90 1720 610 9
1516
2038
2030
1525
1550
1116
100110
1820
run 1 run 2 run 3 run 4 run 5 run 6 run 7 run 8
ordered sequence
sequentialallocationscheme(completebinarytree)
Each node representsthe smaller of its twochildren.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
31
*Figure 5.35: Selection tree of Figure 5.34 after one record has been output and the tree restructured(nodes that were changed are ticked)
15
16
20
30
15
50
25
25
20
38
11
16
100
110
18
20
10 8
9 9
2010
15 11
8 12
913
9014
1715
9 4
15 5
8 6
17 7
9 2
8 3
8 1
32
Analysis
• K: # of runs• n: # of records• setup time: O(K) (K-1)
• restructure time: O(log2K) log2(K+1)
• merge time: O(nlog2K)
• slight modification: tree of loser– consider the parent node only (vs. sibling nodes)
33
10 8
9 9
2010
6 11
8 12
913
9014
1715
10 4
20 5
9 6
90 7
9 2
17 3
8 1
6
Run 1 2 3 4 5 6 7 8
overallwinner
*Figure 5.36: Tree of losers corresponding to Figure 5.34 (p.235)
15
15
9
8
15
15
9
34
Activity on Vertex (AOV) Network definitionA directed graph in which the vertices represent tasks or activities and the edges represent precedence relations between tasks.predecessor (successor)vertex i is a predecessor of vertex j iff there is a directed path from i to j. j is a successor of i.partial ordera precedence relation which is both transitive (i, j, k, ij & jk => ik ) and irreflexive (x xx).acylic grapha directed graph with no directed cycles
35
*Figure 6.38: An AOV network (p.305)
Topological order:linear ordering of verticesof a graphi, j if i is a predecessor ofj, then i precedes j in thelinear ordering
C1, C2, C4, C5, C3, C6, C8,C7, C10, C13, C12, C14, C15, C11, C9
C4, C5, C2, C1, C6, C3, C8,C15, C7, C9, C10, C11, C13,C12, C14
36
*Program 6.13: Topological sort (p.306)
for (i = 0; i <n; i++) { if every vertex has a predecessor { fprintf(stderr, “Network has a cycle. \n “ ); exit(1); } pick a vertex v that has no predecessors; output v; delete v and all edges leading out of v from the network;}
37
*Figure 6.39:Simulation of Program 6.13 on an AOV network (p.306)
v0 no predecessordelete v0->v1, v0->v2, v0->v3
v1, v2, v3 no predecessorselect v3delete v3->v4, v3->v5
select v2delete v2->v4, v2->v5
select v5 select v1delete v1->v4
38
*Figure 6.40:Adjacency list representation of Figure 6.30(a)(p.309)
0 1 2 3 NULL
1 4 NULL
1 4 5 NULL
1 5 4 NULL
3 NULL
2 NULL
V0
V1
V2
V3
V4
V5
v0
v1
v2
v3
v4
v5
count linkheadnodes
vertex linknode
39
typedef struct node *node_pointer;typedef struct node { int vertex; node_pointer link; };typedef struct { int count; node_pointer link; } hdnodes;hdnodes graph[MAX_VERTICES];
Topological sort
40
*Program 6.14: Topological sort (p.308)
O(n)
void topsort (hdnodes graph [] , int n){ int i, j, k, top; node_pointer ptr; /* create a stack of vertices with no predecessors */ top = -1; for (i = 0; i < n; i++) if (!graph[i].count) {no predecessors, stack is linked through graph[i].count = top; count field top = i; }for (i = 0; i < n; i++) if (top == -1) { fprintf(stderr, “\n Network has a cycle. Sort terminated. \n”); exit(1);}
41
O(e)
O(e+n)
Continued} else { j = top; /* unstack a vertex */ top = graph[top].count; printf(“v%d, “, j); for (ptr = graph [j]. link; ptr ;ptr = ptr ->link ){ /* decrease the count of the successor vertices of j */ k = ptr ->vertex; graph[k].count --; if (!graph[k].count) { /* add vertex k to the stack*/ graph[k].count = top; top = k; } } } }
Recommended