Download presentation

Presentation is loading. Please wait.

Published byLogan Corvin Modified about 1 year ago

1
1 Heap and Others 黃兆武

2
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
3 *Figure 5.25: Sample max heaps (p.219) [4] [1] [2][3] [5] [6] [1] [2] [3] [4] [1] [2] Property: The root of max heap (min heap) contains the largest (smallest).

4
[1] [2][3] [5] [6] [1] [2] [3] [4] [1] [2] [4] *Figure 5.26:Sample min heaps (p.220)

5
5 Application: priority queue machine service –amount of time (min heap) –amount of payment (max heap) factory –time tag

6
6 Example of Insertion to Max Heap initial location of new node insert 21 into heap insert 5 into heap

7
7 Insertion into a Max Heap void 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; } 2 k -1=n ==> k= log 2 (n+1) O(log 2 n)

8
8 Example of Deletion from Max Heap 20 remove

9
9 Deletion from a Max Heap element 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
10 while (child <= *n) { /* find the larger child of the current parent */ if ((child < *n)&& (heap[child].key

11
11 26 [1] 5 [2] 77 [3] 1 [4] 61 [5] 11 [6] 59 [7] 15 [8] 48 [9] 19 [10] Heap Sort input file

12
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
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
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) Heap Sort

15
15 Heap Sort void 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; } i 2i 2i+1

16
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
17 AVL 樹 (Balanced Binary Tree) 1.T 是一個非空的二元樹， Tl 及 Tr 分別是它的左右子樹，若符合下列兩條件，則稱Ｔ為高度平 衡樹。（ Height Balancing Binary Tree) (1) TL 及 TR 也是高度平衡樹 (2) | HL-HR | <= 1 ， HL 及 HR 分別為 TL 及 TR 的高度。 X ○

18
Route balanced tree A binary tree with n nodes, where all nodes are at depth └ lg(n) ┘ or less, and where there are exactly 2 d 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
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
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
21 A Threaded Binary Tree ABCGEIDHF root dangling inorder traversal: H, D, I, B, E, A, F, C, G

22
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: child TRUE: thread

23
23 Memory Representation of A Threaded BT f f -- f f A f f C f f B t t E t t F t t G f f D t t I t t H root

24
24 Next Node in Threaded BT threaded_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
25 Inorder Traversal of Threaded BT void 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
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
27 Examples root parent A B C D child root parent A B C D child empty Insert a node D as a right child of B. (1) (2) (3)

28
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
29 Selection Trees (1)winner tree (2)loser tree

30
30 winner tree run 1 run 2 run 3 run 4 run 5 run 6 run 7 run 8 ordered sequence sequential allocation scheme (complete binary tree) Each node represents the smaller of its two children

31
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)

32
32 Analysis K: # of runs n: # of records setup time: O(K)(K-1) restructure time: O(log 2 K) log 2 (K+1) merge time: O(nlog 2 K) slight modification: tree of loser –consider the parent node only (vs. sibling nodes)

33
Run overall winner *Figure 5.36: Tree of losers corresponding to Figure 5.34 (p.235)

34
34 Activity on Vertex (AOV) Network n definition A directed graph in which the vertices represent tasks or activities and the edges represent precedence relations between tasks. n 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. n partial order a precedence relation which is both transitive ( i, j, k, i j & j k => i k ) and irreflexive ( x x x). n acylic graph a directed graph with no directed cycles

35
35 *Figure 6.38: An AOV network (p.305) Topological order: linear ordering of vertices of a graph i, j if i is a predecessor of j, then i precedes j in the linear 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
36 *Program 6.13: Topological sort (p.306) for (i = 0; i

37
37 *Figure 6.39:Simulation of Program 6.13 on an AOV network (p.306) v0 no predecessor delete v0->v1, v0->v2, v0->v3 v1, v2, v3 no predecessor select v3 delete v3->v4, v3->v5 select v2 delete v2->v4, v2->v5 select v5 select v1 delete v1->v4

38
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 V 0 V 1 V 2 V 3 V 4 V 5 v0 v1 v2 v3 v4 v5 count link headnodes vertex link node

39
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
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
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; } } } }

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google