760 likes | 1.01k Views
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
E N D
CHAPTER 9 ADVANCED SEARCH TREES 1
Min-Max Heap • A double-ended priority queueis a data structure that • supports thefollowing operation: • Insertan element • Delete Max • Delete Min 2
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
min max min max Min-MaxHeap 4
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
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
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
Time complexity = ½ Height 8
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
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
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
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
Double-ended Heap (Deap) • A double-ended priority queue is a data structure that • supports the following operation: • Insert • Delete Max • Delete Min 14
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
Double-ended Heap (Deap) min max 16
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
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
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
Summary 22
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
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
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
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
Balanced Binary Tree Searching cost = 1.67 Searching cost = 2 27
Rotation Types RR LL LR RL 30
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
Adjustment rules new insert node 32
balance Time Complexity: 33
Theorem An AVL tree with height h, Requires at leastnode 34
< 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
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
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
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
Combination combination: 41
Weighted External Path Length Definition: Given+1Different weighting values of the external node (j=1~(n+1)), then 42
Weighted External Path Length W.E.P.L. = 43 W.E.P.L. = 52 43
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
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
Question: Insert 5, then insert 80? 46
Question: Follow the data to build min-max heap 26, 5, 33, 77, 2, 6, 19 47
Question: Delete minimum element twice, then delete maximum? 48
Question: Insert 4, 60? 49
Question: Follow the data to build Deap 26, 5, 33, 77, 19, 2, 8 50