1 / 76

CHAPTER 9

CHAPTER 9. ADVANCED SEARCH TREES. 1. Min-Max Heap. A double-ended priority queue is a data structure that supports the following operation: Insert an element Delete Max Delete Min. 2. Min-Max Heap. Definition : A min-max heap is a complete binary tree such that if it is

elisha
Télécharger la présentation

CHAPTER 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 9 ADVANCED SEARCH TREES 1

  2. Min-Max Heap • A double-ended priority queueis a data structure that • supports thefollowing operation: • Insertan element • Delete Max • Delete Min 2

  3. Min-Max Heap • Definition: • A min-max heapis a complete binary tree such that if it is • not empty, each element has a field called key. • 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

  4. min max min max Min-MaxHeap 4

  5. Location Procedure min-max heap (h, n, x) 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) 5

  6. Procedure verify-max (h, i, x) 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

  7. Procedure verify-min (h, i, x) 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

  8. Time complexity = ½ Height 8

  9. Delete minimum element Step1: Remove root Step2: Letlast node =x, x Inserta min-max heap and its root is empty case 1: ifroot have no child, x placed in root case 2: if root have child but have noGchild, Findchild’s min k if (k < x) then swap (k, x) else x placed inroot 9

  10. Delete minimum element case 3: if root have Gchild, Find the min k of theGrandchild, and suppose k’sparent node isp if (k < x) then swap (k, x) if (x > p) then swap (p, x) goto step 2 else x placed inroot 10

  11. Delete maximum element Step1: Remove max (h[2] , h[3]) Step2: Letlast node =x, x inserta min-max heap and its root is empty case 1: ifroot have no child, x placed in root case 2: if root have child but have noGchild, Findchild’s max k if (k > x) then swap (k, x) else x placed inroot 11

  12. Delete maximum element case 3: if root have Gchild, Find the max k of theGrandchild, and suppose k’sparent node isp if (k > x) then swap (k, x) if (x < p) then swap (p, x) goto step 2 else x placed inroot 12

  13. Time complexity 13

  14. Double-ended Heap (Deap) • A double-ended priority queue is a data structure that • supports the following operation: • Insert • Delete Max • Delete Min 14

  15. Double-ended Heap (Deap) 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. Definition: 15

  16. Double-ended Heap (Deap) min max 16

  17. Insert Step1: x placed behindlast node Step2: case 1: ifx is inmin-heap, find its corresponding node (j)inmax-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: ifx is inmax-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

  18. Time complexity 18

  19. Delete minimum Step1: RemoveDeap[2] Step2: set last node =x Step3: inmin-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) Step4: Deap_Insert (D, i, x) 19

  20. Delete maximum Step1: RemoveDeap[3] Step2: setlast node =x Step3: inmax-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) Step4: Deap_Insert (D, i, x) 20

  21. Time complexity 21

  22. Summary 22

  23. Extended Binary Tree If a binary tree has n nodes, then it will haven+1 nulllink. If we add a special node in each null link, then we call this node external node (orfailure node), and the binary tree with external nodes is called extended binary tree. : Internal node : External node 23

  24. Internal path and External path 0 I=0+1+1+2=4 1 1 E=2+2+2+3+3=12 2 E=I+2n 2 2 2 3 3 24

  25. Balanced Binary Search Tree AVL Tree B Tree Balanced m-way Search Tree Optimal Binary Search Tree Search cost Internal and external nodes have no weighted value Search tree External node have weighted value Huffman Tree Internal and external nodes have weighted value 25

  26. Balance factor Recursive Definition AVL Tree 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 (1) (3) and are height balanced 26

  27. Balanced Binary Tree Searching cost = 1.67 Searching cost = 2 27

  28. Balanced Binary Tree 28

  29. Non-Balanced Binary Tree 29

  30. Rotation Types RR LL LR RL 30

  31. Adjustment rules Step1: Find the nearest un-balanced node, and then find the related three nodes Step2: On related three nodes, pull up the medium node small node → on the left large node → on the right Step3: Adjust subtrees 31

  32. Adjustment rules new insert node 32

  33. balance Time Complexity: 33

  34. Theorem An AVL tree with height h, Requires at leastnode 34

  35. < Value Value < < Value < M-way Search Tree (1) The key of each node is arranged from small to large , so ki<ki+1, 1≦i≦n (2) Subtree Aiisalso a m-way search tree 35

  36. B Tree of order m • Definition: • Balanced m-way search tree: commonly used in external search. Can be empty. If not empty, it has to satisfy: • 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

  37. B tree of order 3 37

  38. B tree of order 4 38

  39. Insert Step1: Find an appropriate place to insert x Step2: 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

  40. Delete Step1: Findthe location node of x case 1: ifx is inleaf nodes (1) deletex (2) check underflow, if not →stop, else →rotation, if can’t do rotation → then do combination. goto (2) →check parent node) case 2: ifx is innon-leaf nodes, relpace x with the minimum key in right subtree or the maximum key in left subtree goto case 1. Step2: rotation: 40

  41. Combination combination: 41

  42. Weighted External Path Length Definition: Given+1Different weighting values ​​of the external node (j=1~(n+1)), then 42

  43. Weighted External Path Length W.E.P.L. = 43 W.E.P.L. = 52 43

  44. Huffman Tree • Algorithm: • Construct a set of trees with root nodes that contain each of the individual symbols and their weights • Place the set of trees into a priority queue. • while the priority queue has more than one item • Remove the two trees with the smallest weights. • Combine them into a new binary tree in which the weight of the tree root is the sum of the weights of its children. • Insert the newly created tree back into the priority queue. 44

  45. Procedure Huffman (w, n) 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 End Time complexity: 45

  46. Question: Insert 5, then insert 80? 46

  47. Question: Follow the data to build min-max heap 26, 5, 33, 77, 2, 6, 19 47

  48. Question: Delete minimum element twice, then delete maximum? 48

  49. Question: Insert 4, 60? 49

  50. Question: Follow the data to build Deap 26, 5, 33, 77, 19, 2, 8 50

More Related