420 likes | 754 Views
Heap and Others. 黃兆武. 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.
E N D
Heap and Others 黃兆武
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 binarytree 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
[1] [1] 9 [1] 30 14 [2] [2] [3] [2] 3 6 [3] 7 25 12 [6] [5] [4] 5 10 8 6 *Figure 5.25: Sample max heaps (p.219) [4] Property: The root of max heap (min heap) contains the largest (smallest).
[1] [1] 10 [1] 11 2 [2] [2] [3] [2] 83 20 [3] 4 21 7 [6] [5] [4] 50 10 [4] 8 6 *Figure 5.26:Sample min heaps (p.220)
Application: priority queue • machine service • amount of time (min heap) • amount of payment (max heap) • factory • time tag
Example of Insertion to Max Heap 21 20 20 20 15 5 2 15 15 2 10 14 2 10 14 10 14 insert 21 into heap insert 5 into heap initial location of new node
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; } 2k-1=n ==> k=log2(n+1) O(log2n)
Example of Deletion from Max Heap remove 20 15 10 2 2 2 15 14 15 14 10 14 10
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;
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; }
[6] [1] [9] [2] [3] [8] [4] [5] [7] 5 77 1 61 59 26 15 48 11 [10] 19 Heap Sort 1 2 3 4 5 6 7 8 9 10 26 5 77 1 61 11 59 15 48 19 input file
[6] [1] [9] [2] [3] [8] [4] [5] [7] 61 59 48 19 26 77 15 1 11 [10] 5 Heap Sort initial heap exchange
61 15 48 59 15 5 19 11 1 5 1 11 19 59 48 26 26 [2] [2] [3] [3] [6] [6] [5] [1] [5] [7] [8] [4] [1] [7] [8] [4] [9] Heap Sort 77 [10] (a) 61 77 [9] [10] (b)
48 61 5 59 48 26 1 15 19 26 15 11 11 5 1 19 59 [2] [2] [3] [3] [6] [6] [4] [7] [9] [5] [4] [8] [1] [7] [1] [8] [5] Heap Sort 59 61 77 [10] (c) 48 59 61 77 [9] [10] (d)
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+1 2i
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) bottom-up n-1 cylces top-down
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 ○
1-2. Route balanced tree A 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
Threaded Binary Trees • Two many null pointers incurrent representation of binary treesn: number of nodes number of non-null links: n-1 total links: 2nnull links: 2n-(n-1)=n+1 • Replace these null pointers with some useful “threads”.
Threaded Binary Trees (Continued) If ptr->left_child is null, replace it with a pointer to the node that would be visited beforeptr in an inorder traversal If ptr->right_child is null, replace it with a pointer to the node that would be visited afterptr in an inorder traversal
A C B G F E D I H A Threaded Binary Tree root dangling dangling inorder traversal: H, D, I, B, E, A, F, C, G
Data Structures for Threaded BT FALSE left_thread left_child data right_child right_thread TRUE FALSE: child TRUE: thread 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; };
Memory Representation of A Threaded BT root -- f f A f f C B f f f f G F E t D t t t t f t f I H t t t t
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; }
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)
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
Examples Insert a node D as a right child of B. root root A A (1) parent B parent B (3) child child C C D D (2) empty
*Figure 5.24: Insertion of child as a right child of parent in a threaded binary tree (p.217) (3) (1) (2) (4) nonempty
Selection Trees (1) winner tree (2) loser tree
18 11 20 15 20 15 15 20 16 30 25 38 50 16 sequential allocation scheme (complete binary tree) Each node represents the smaller of its two children. winner tree 1 6 3 2 8 6 7 4 5 6 17 8 6 9 11 12 13 15 9 10 8 14 6 9 9 17 8 20 10 90 ordered sequence 100 110 run 8 run 7 run 6 run 5 run 4 run 3 run 2 run 1
*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) 2 9 12 15 14 13 10 9 8 1 11 4 6 7 3 5 10 90 9 20 17 9 9 15 8 15 8 8 17 8 20 11 25 18 20 15 15 100 38 16 50 25 20 30 16 110
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)
2 9 12 15 10 14 8 13 9 6 5 4 3 7 11 1 10 9 90 9 20 17 8 17 9 20 10 6 90 8 *Figure 5.36: Tree of losers corresponding to Figure 5.34 (p.235) overall winner 6 8 9 9 15 15 Run 1 2 3 4 5 6 7 8 15 15
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
*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
*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;}
*Figure 6.39:Simulation of Program 6.13 on an AOV network (p.306) select v2 delete v2->v4, v2->v5 v1, v2, v3 no predecessor select v3 delete v3->v4, v3->v5 v0 no predecessor delete v0->v1, v0->v2, v0->v3 select v1 delete v1->v4 select v5
V0 V1 V2 0 1 2 4 5 3 NULL 1 4 NULL 1 5 NULL 1 4 NULL 3 NULL 2 NULL *Figure 6.40:Adjacency list representation of Figure 6.30(a)(p.309) headnodes node count link vertex link V3 V4 V5 v1 v4 v2 v0 v3 v5
Topological sort 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];
*Program 6.14: Topological sort (p.308) 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);} O(n)
} 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; } } } } Continued O(e) O(e+n)