NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER
NCUE CSIE Wireless Communications and Networking Laboratory CHAPTER 9 ADVANCED SEARCH TREES 1
Min-Max Heap NCUE CSIE Wireless Communications and Networking Laboratory A double-ended priority queue is a data structure that supports the following operation: (1) Insert an element (2) Delete Max (3) Delete Min 2
Min-Max Heap NCUE CSIE Wireless Communications and Networking Laboratory Definition: A min-max heap is a complete binary tree such that if it is not empty, each element has a field called key. (1)Alternating levels of this tree are min levels and max levels respectively. (2) The root is on a min level (3) Let x be any node in a min-max heap. If x is on a min (max) level then the element in x has the minimum (maximum) key from among all elements in the subtree with root x. 3
Min-Max Heap NCUE CSIE Wireless Communications and Networking Laboratory min max 4
Procedure min-max heap (h, n, x) NCUE CSIE Wireless Communications and Networking Laboratory n=n+1 p=n/2 if p==0 then h[1]=x else case level (p) min: if x < h[p] then swap (h[n], h[p]) verify-min (h, p, x) else verify-max (h, n, x) max: if x > h[p] then swap (h[n], h[p]) verify-max (h, p, x) else verify-min (h, n, x) Location 5
Procedure verify-max (h, i, x) NCUE CSIE Wireless Communications and Networking Laboratory Gp=(i/4) while (Gp≠ 0) do if x > h[Gp] then swap (h[i], h[Gp]) i=Gp Gp=(i/4) else Gp=0 6
Procedure verify-min (h, i, x) NCUE CSIE Wireless Communications and Networking Laboratory Gp=(i/4) while (Gp ≠ 0) do if x < h[Gp] then swap (h[i], h[Gp]) i=Gp Gp=(i/4) else Gp=0 7
Time complexity NCUE CSIE Wireless Communications and Networking Laboratory = ½ Height 8
Delete minimum element NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Remove root Step 2: Let last node = x, x Insert a min-max heap and its root is empty case 1: if root have no child, x placed in root case 2: if root have child but have no Gchild, Find child’s min k if (k < x) then swap (k, x) else x placed in root 9
Delete minimum element NCUE CSIE Wireless Communications and Networking Laboratory case 3: if root have Gchild, Find the min k of the Grandchild, and suppose k’s parent node is p if (k < x) then swap (k, x) if (x > p) then swap (p, x) goto step 2 else x placed in root 10
Delete maximum element NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Remove max (h[2] , h[3]) Step 2: Let last node = x, x insert a min-max heap and its root is empty case 1: if root have no child, x placed in root case 2: if root have child but have no Gchild, Find child’s max k if (k > x) then swap (k, x) else x placed in root 11
Delete maximum element NCUE CSIE Wireless Communications and Networking Laboratory case 3: if root have Gchild, Find the max k of the Grandchild, and suppose k’s parent node is p if (k > x) then swap (k, x) if (x < p) then swap (p, x) goto step 2 else x placed in root 12
Time complexity NCUE CSIE Wireless Communications and Networking Laboratory 13
Double-ended Heap (Deap) NCUE CSIE Wireless Communications and Networking Laboratory A double-ended priority queue is a data structure that supports the following operation: (1) Insert (2) Delete Max (3) Delete Min 14
Double-ended Heap (Deap) NCUE CSIE Wireless Communications and Networking Laboratory Definition: Definition A deap is a complete binary tree that is either empty or satisfies the following properties: (1) The root contains no element. (2) The left subtree is a min-heap (3) The right subtree is a max-heap (4) If the right subtree is not empty, then let i be any node in the left subtree. Let j be the corresponding node in the right subtree. If j does not exist, then let j be the node in the right subtree that corresponds to the parent of i. The key of node i is less than or equal to the key of node j. 15
Double-ended Heap (Deap) NCUE CSIE Wireless Communications and Networking Laboratory min max 16
Insert NCUE CSIE Wireless Communications and Networking Laboratory Step 1: x placed behind last node Step 2: case 1: if x is in min-heap, find its corresponding node (j) in max-heap if (x. key j. key) then min-heap-insert (D, n, x) else swap (x. key, j. key) max-heap-insert (D, j, x) case 2: if x is in max-heap, find its corresponding node (j) in min-heap if (x. key ≧ j. key) then max-heap-insert (D, n, x) else swap (x. key, j. key) min-heap-insert (D, j, x) 17
Time complexity NCUE CSIE Wireless Communications and Networking Laboratory 18
Delete minimum NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Remove Deap[2] Step 2: set last node =x Step 3: in min-heap , find the minimum sub-node, and then (repeatedly) replace the vacancy of the parent node until a vacancy occurred in the leaf node. (suppose this node is located in i) Step 4: Deap_Insert (D, i, x) 19
Delete maximum NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Remove Deap[3] Step 2: set last node = x Step 3: in max-heap, Find the maximum sub node, and then (repeatedly) replace the vacancy of the parent point until a vacancy occurred in the leaf node. (suppose this node is located in i) Step 4: Deap_Insert (D, i, x) 20
Time complexity NCUE CSIE Wireless Communications and Networking Laboratory 21
Summary NCUE CSIE Wireless Communications and Networking Laboratory Min-Max Heap and Deap Insert O(logn) Delete Max O(logn) Delete Min O(logn) Search Max O(1) Search Min O(1) 22
Extended Binary Tree NCUE CSIE Wireless Communications and Networking Laboratory If a binary tree has n nodes, then it will have n+1 null link. If we add a special node in each null link, then we call this node external node (or failure node), and the binary tree with external nodes is called extended binary tree. : Internal node : External node 23
Internal path and External path NCUE CSIE Wireless Communications and Networking Laboratory 0 I=0+1+1+2=4 1 1 E=2+2+2+3+3=12 2 2 3 3 E=I+2 n 24
Search cost NCUE CSIE Wireless Communications and Networking Laboratory Internal and external nodes have no weighted value Balanced Binary Search Tree Balanced m-way Search Tree Search tree AVL Tree B Tree External node have weighted value Huffman Tree Internal and external nodes have weighted value Optimal Binary Search Tree 25
AVL Tree NCUE CSIE Wireless Communications and Networking Laboratory Definition: Definition An empty binary tree is height balanced. If T is a nonempty binary tree with and as its left and right subtrees, then T is height balanced iff Balance factor (1) (3) and are height balanced Recursive Definition 26
Balanced Binary Tree NCUE CSIE Wireless Communications and Networking Laboratory Searching cost = 2 Searching cost = 1. 67 27
Balanced Binary Tree NCUE CSIE Wireless Communications and Networking Laboratory 28
Non-Balanced Binary Tree NCUE CSIE Wireless Communications and Networking Laboratory 29
Rotation Types NCUE CSIE Wireless Communications and Networking Laboratory LL LR RL RR 30
Adjustment rules NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Find the nearest un-balanced node, and then find the related three nodes Step 2: On related three nodes, pull up the medium node small node → on the left large node → on the right Step 3: Adjust subtrees 31
Adjustment rules NCUE CSIE Wireless Communications and Networking Laboratory new insert node 32
Time Complexity: Complexity NCUE CSIE Wireless Communications and Networking Laboratory Operation Sequential list Linked list AVL Tree Search for x O(log n) Search for kth item O(1) O(k) O(log n) Delete x O(n) O(1) O(log n) Delete kth item O(n-k) O(log n) Insert x O(n) O(1) O(log n) Output in order O(n) balance 33
Theorem NCUE CSIE Wireless Communications and Networking Laboratory An AVL tree with height h, Requires at least node 34
M-way Search Tree NCUE CSIE Wireless Communications and Networking Laboratory < Value < (1) The key of each node is arranged from small to large , so ki < ki+1 , 1≦i≦n (2) Subtree Ai is also a m-way search tree 35
B Tree of order m NCUE CSIE Wireless Communications and Networking Laboratory Definition: Definition Balanced m-way search tree: commonly used in external search. Can be empty. If not empty, it has to satisfy: (1) Root have at least two child nodes (2)Except the root and the failed node, the degree of a node is between to m (3) All failed nodes are on the same level 36
B tree of order 3 NCUE CSIE Wireless Communications and Networking Laboratory 37
B tree of order 4 NCUE CSIE Wireless Communications and Networking Laboratory 38
Insert NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Find an appropriate place to insert x Step 2: Check overflow case 1: if not overflow →stop case 2: if overflow → split goto step 2 → (parent node) Split: Pull up the th key to the parent nodes, and the remaining nodes are divided into two nodes 39
Delete NCUE CSIE Wireless Communications and Networking Laboratory Step 1: Find the location node of x Step 2: case 1: if x is in leaf nodes (1) delete x (2) check underflow, if not → stop, else → rotation, if can’t do rotation → then do combination. goto (2) →check parent node) case 2: if x is in non-leaf nodes, relpace x with the minimum key in right subtree or the maximum key in left subtree goto case 1. rotation: 40
Combination NCUE CSIE Wireless Communications and Networking Laboratory combination: 41
Weighted External Path Length NCUE CSIE Wireless Communications and Networking Laboratory Definition: Definition Give n+1 Different weighting values of the external node (j=1~(n+1)), then 42
Weighted External Path Length NCUE CSIE Wireless Communications and Networking Laboratory W. E. P. L. = 43 W. E. P. L. = 52 43
Huffman Tree NCUE CSIE Wireless Communications and Networking Laboratory Algorithm: 1. Construct a set of trees with root nodes that contain each of the individual symbols and their weights 2. Place the set of trees into a priority queue. 3. while the priority queue has more than one item 4. Remove the two trees with the smallest weights. 5. Combine them into a new binary tree in which the weight of the tree root is the sum of the weights of its children. 6. Insert the newly created tree back into the priority queue. 44
Procedure Huffman (w, n) NCUE CSIE Wireless Communications and Networking Laboratory Begin for (i=1; i<n; i++) Begin new (t) t. left_child=least(w) t. right_child=least(w) t. weight=t. left_child. weight+t. right_child. weight Insert (w, t) End Time complexity: 45
Question: NCUE CSIE Wireless Communications and Networking Laboratory Insert 5, then insert 80? 46
Question: NCUE CSIE Wireless Communications and Networking Laboratory Follow the data to build min-max heap 26, 5, 33, 77, 2, 6, 19 47
Question: NCUE CSIE Wireless Communications and Networking Laboratory Delete minimum element twice, then delete maximum? 48
Question: NCUE CSIE Wireless Communications and Networking Laboratory Insert 4, 60? 49
Question: NCUE CSIE Wireless Communications and Networking Laboratory Follow the data to build Deap 26, 5, 33, 77, 19, 2, 8 50
Question: NCUE CSIE Wireless Communications and Networking Laboratory Delete minimum twice? 51
Question: NCUE CSIE Wireless Communications and Networking Laboratory Delete maximum? 52
Question: NCUE CSIE Wireless Communications and Networking Laboratory A Full Extened B. T. Height=k, I, E value? 53
Question: NCUE CSIE Wireless Communications and Networking Laboratory Mar, May, Nov, Aug, Apr, Jan, Dec, Jul, Feb, Jun, Oct, Sep , build an AVL Tree 54
Question: NCUE CSIE Wireless Communications and Networking Laboratory Give the following data to build an AVL tree? 26, 33, 77, 19, 2, 5, 4, 8, 6, 10 55
Question: NCUE CSIE Wireless Communications and Networking Laboratory An AVL Tree has 15 nodes, Maximum height? Minimum height? Max︰ h 4 5 6 Fh+2 -1 7 12 20 Min︰ 56
Question: NCUE CSIE Wireless Communications and Networking Laboratory Give the following sequence of data, how many rotations are needed to build an AVL tree and how many pointers are rearranged during rotation? Mon, Tue, Wed, Thu, Fri, Sat, Sun 57
Question: NCUE CSIE Wireless Communications and Networking Laboratory DAS, JFK, ZRH, HKG, KHH, FRA, ARN, LBA, MEX, GLA, ORY, MAN, TPE, ORD, NAP (1) Draw the AVL tree (2) Insert NYC, what kind of rotation is required? Sketch this tree 58
Question: NCUE CSIE Wireless Communications and Networking Laboratory Insert 72 59
Question: NCUE CSIE Wireless Communications and Networking Laboratory M-way Search tree, Height= h, the maximum node is? maximum key is? Node� Key� 60
Question: NCUE CSIE Wireless Communications and Networking Laboratory B tree of order 3 (1) Insert 38 (2) Insert 55 (3) Insert 37 61
Question: NCUE CSIE Wireless Communications and Networking Laboratory Give 26, 5, 33, 49, 17, 2, 8, 6, 10 build B tree of order 3 62
Question: NCUE CSIE Wireless Communications and Networking Laboratory Read the following data in the given order, and show the corresponding trees: 7, 8, 9, 2, 1, 5, 3, 6, 4 (1) Binary Search tree (2) AVL tree (3) 2 -3 tree 63
Question: NCUE CSIE Wireless Communications and Networking Laboratory Suppose in a B-tree must be at least one key and at most two keys in every node. Draw the B-tree after the following succession of events (i. e. , event B is executed following event A, and event, C follows event B, etc. ) (A)Insert 10, 50, 40, 80, 90, 100 (B)Delete 80 (C)Insert 20, Delete 50 (D)Delete 100 64
Answer: NCUE CSIE Wireless Communications and Networking Laboratory Question is B tree of order 3 (A) (B) 65
Answer: NCUE CSIE Wireless Communications and Networking Laboratory (C) Insert 20 Delete 50 (D) 66
Question: NCUE CSIE Wireless Communications and Networking Laboratory B tree of order 3 (1) Delete 58 (2) Delete 65 (3) Delete 80 67
Question: NCUE CSIE Wireless Communications and Networking Laboratory B tree of order 3 (1) Delete 10 68
Question: NCUE CSIE Wireless Communications and Networking Laboratory B tree of order 3 (1) Delete 10 69
Question: NCUE CSIE Wireless Communications and Networking Laboratory B tree of order 3 (1) Delete 70 (2) Delete 50 70
Question: NCUE CSIE Wireless Communications and Networking Laboratory B-tree of order 5 (1) Insert 65 (2) Delete 21 from the original B-tree (3) Delete 33 from the original B-tree 71
Question: NCUE CSIE Wireless Communications and Networking Laboratory The weighted value of the six external node is as follows : minimum W. E. P. L is? 72
Question: NCUE CSIE Wireless Communications and Networking Laboratory Briefly describe Huffman encoding and illustrate if for the following data step by step ABADCDACABBCBBBA 73
Question: NCUE CSIE Wireless Communications and Networking Laboratory Construct Huffman binary codes for eight messages whose probabilities of appearance are 0. 2, 0. 19, 0. 18, 0. 14, 0. 11, 0. 08, 0. 06, and 0. 04 74
Question: NCUE CSIE Wireless Communications and Networking Laboratory Consider a new design cpu with six instructions which have the probability of occurance I 1=0. 4, I 2=0. 3, I 3=0. 01, I 4=0. 08, I 5=0. 06, I 6=0. 06 (1) Construct the opcodes using Huffman’s method (2) Calculate the average number of bits per instruction 75
Reference NCUE CSIE Wireless Communications and Networking Laboratory Ø Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed 〝Fundamentals of Data Structures in C〞, W. H. Freeman & Co Ltd, 1992. Ø Ellis Horowitz, Sartaj Sahni, and Dinesh Mehta 〝Fundamentals of Data Structures in C++〞 Silicon Pr, 2006 Ø Richard F. Gilberg, Behrouz A. Forouzan, 〝Data Structures: A Pseudocode Approach with C〞, S Baker & Taylor Books, 2004 Ø Fred Buckley, and Marty Lewinter 〝A Friendly Introduction to Graph Theory〞 Prentice Hall, 2002 Ø 〝資料結構-使用C語言〞蘇維雅譯,松崗,2004 Ø 〝資料結構-使用C語言〞 蔡明志編著,全華,2004 Ø 〝資料結構(含精選試題)〞洪逸編著,鼎茂,2005 76
- Slides: 76