NCUE CSIE Wireless Communications and Networking Laboratory
CHAPTER 9ADVANCED SEARCH TREESADVANCED SEARCH TREES
1
NCUE CSIE Wireless Communications and Networking Laboratory
Min-Max Heap
2
A double-ended priority queuedouble-ended priority queue is a data structure that supports the following operation:
(1) Insert an element(2) Delete Max(3) Delete Min
NCUE CSIE Wireless Communications and Networking Laboratory
Min-Max Heap
Definition: A min-max heapmin-max heap is a complete binary treecomplete 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
NCUE CSIE Wireless Communications and Networking Laboratory
Min-Max Heap
minmin
maxmax
minmin
maxmax
4
NCUE CSIE Wireless Communications and Networking Laboratory
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)
Location
5
NCUE CSIE Wireless Communications and Networking Laboratory
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
NCUE CSIE Wireless Communications and Networking Laboratory
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
NCUE CSIE Wireless Communications and Networking Laboratory
Time complexity
)(log nO = ½ Height
8
NCUE CSIE Wireless Communications and Networking Laboratory
Delete minimum element
Step1: Remove root
Step2: 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 rootcase 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
NCUE CSIE Wireless Communications and Networking Laboratory
if (k < x) then swap (k, x) if (x > p) then swap (p, x) goto step 2else x placed in root
case 3: if root have Gchild, Find the min k of the Grandchild, and suppose k’s parent node is p
10
Delete minimum element
NCUE CSIE Wireless Communications and Networking Laboratory
Delete maximum element
Step1: Remove max (h[2] , h[3])
Step2: 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 rootcase 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
NCUE CSIE Wireless Communications and Networking Laboratory
if (k > x) then swap (k, x) if (x < p) then swap (p, x) goto step 2else x placed in root
case 3: if root have Gchild, Find the max k of the Grandchild, and suppose k’s parent node is p
12
Delete maximum element
NCUE CSIE Wireless Communications and Networking Laboratory
Time complexity
13
)(log nO
NCUE CSIE Wireless Communications and Networking Laboratory
Double-ended Heap (Deap)
14
A double-ended priority queue is a data structure that supports the following operation:
(1) Insert(2) Delete Max(3) Delete Min
NCUE CSIE Wireless Communications and Networking Laboratory
DefinitionDefinition:
15
Double-ended Heap (Deap)
A deap is a complete binary treecomplete 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.
NCUE CSIE Wireless Communications and Networking Laboratory
min max
)2
()(
2 2)1log(
jjthennjif
ij i
16
Double-ended Heap (Deap)
NCUE CSIE Wireless Communications and Networking Laboratory
Insert
Step1: x placed behind last nodecase 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)
Step2:
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
NCUE CSIE Wireless Communications and Networking Laboratory
Time complexity
)(log nO
18
NCUE CSIE Wireless Communications and Networking Laboratory
Delete minimum
Step1: Remove Deap[2]
Step2: set last node =x
Step3: 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)Step4: Deap_Insert (D, i, x)
19
NCUE CSIE Wireless Communications and Networking Laboratory
Delete maximum
Step1: Remove Deap[3]
Step2: set last node = x
Step3: 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)Step4: Deap_Insert (D, i, x)
20
NCUE CSIE Wireless Communications and Networking Laboratory
Time complexity
)(log nO
21
NCUE CSIE Wireless Communications and Networking Laboratory
Summary
Min-Max Heap and DeapMin-Max Heap and Deap
InsertInsert O(logn)O(logn)
Delete MaxDelete Max O(logn)O(logn)
Delete MinDelete Min O(logn)O(logn)
Search MaxSearch Max O(1)O(1)
Search MinSearch Min O(1)O(1)
22
NCUE CSIE Wireless Communications and Networking Laboratory
Extended Binary Tree
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
NCUE CSIE Wireless Communications and Networking Laboratory
Internal path and External path
n
i
I1
)node internal theroot to thefrom length the(
1
1
)node external theroot to thefrom length the(n
i
E
I=0+1+1+2=4
E=2+2+2+3+3=12
0
1 1
32 2
2
23
E=I+2nE=I+2n
24
NCUE CSIE Wireless Communications and Networking Laboratory
Search tree
Internal and external nodes have no weighted value
External node have weighted value
Internal and external nodes have weighted value
Search cost
AVL Tree
B Tree
Balanced Binary Search Tree
Balanced m-way Search Tree
Huffman Tree
Optimal Binary Search Tree
25
NCUE CSIE Wireless Communications and Networking Laboratory
AVL Tree
1 RL hh
26
DefinitionDefinition: An empty binary tree is height balanced. If T is a nonempty binarytree with and as its left and right subtrees, then T is heightbalanced iff (1) (3) and are height balanced
Balance factor
Recursive Definition
LT RT
RTLT
NCUE CSIE Wireless Communications and Networking Laboratory
Searching cost = 2 Searching cost = 1.67
Internal and external nodes have no weighted value
should be as balanced as possible
27
Balanced Binary Tree
NCUE CSIE Wireless Communications and Networking Laboratory
28
Balanced Binary Tree
NCUE CSIE Wireless Communications and Networking Laboratory
29
Non-Balanced Binary Tree
NCUE CSIE Wireless Communications and Networking Laboratory
Rotation Types
LLLL LRLR RLRL RRRR
30
NCUE CSIE Wireless Communications and Networking Laboratory
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
NCUE CSIE Wireless Communications and Networking Laboratory
32
Adjustment rules
new insert node
NCUE CSIE Wireless Communications and Networking Laboratory
Time ComplexityTime Complexity:
OperationOperation Sequential listSequential list Linked listLinked list AVL TreeAVL Tree
Search for xSearch for x O(log n)O(log n) O(n)O(n) O(log n)O(log n)
Search for kth Search for kth itemitem
O(1)O(1) O(k)O(k) O(log n)O(log n)
Delete xDelete x O(n)O(n) O(1)O(1) O(log n)O(log n)
Delete kth itemDelete kth item O(n-k)O(n-k) O(k)O(k) O(log n)O(log n)
Insert xInsert x O(n)O(n) O(1)O(1) O(log n)O(log n)
Output in orderOutput in order O(n)O(n) O(n)O(n) O(n)O(n)
balance
33
NCUE CSIE Wireless Communications and Networking Laboratory
An AVL tree with height h, Requires at least node12 hF
Theorem
34
NCUE CSIE Wireless Communications and Networking Laboratory
M-way Search Tree
1k 2k 3k4k 1mk
Value < 1k< Value < 1k 2k
< Value1mk
oA
1A
1mA
(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 tree35
4k
NCUE CSIE Wireless Communications and Networking Laboratory
B Tree of order m
DefinitionDefinition: 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
2
m
1__.12
deg2
mKeyofNom
mreem
36
NCUE CSIE Wireless Communications and Networking Laboratory
B tree of order 3
3deg2
32 ree
37
NCUE CSIE Wireless Communications and Networking Laboratory
B tree of order 4
4deg2
42 ree
38
NCUE CSIE Wireless Communications and Networking Laboratory
39
Step1: Find an appropriate place to insert x
Step2: Check overflowcase 1: if not overflow →stopcase 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
2
m
Insert
NCUE CSIE Wireless Communications and Networking Laboratory
Delete
Step1: Find the location node of x
Step2: 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
NCUE CSIE Wireless Communications and Networking Laboratory
combination:
41
Combination
NCUE CSIE Wireless Communications and Networking Laboratory
Weighted External Path Length
DefinitionDefinition: Give n+1 Different weighting values of the external node (j=1~(n+1)), then
jq
1
1
)j node externalan root to ofpath the(....n
jjqLPEW
42
NCUE CSIE Wireless Communications and Networking Laboratory
W.E.P.L. = 43 W.E.P.L. = 52
43
Weighted External Path Length
NCUE CSIE Wireless Communications and Networking Laboratory
Huffman Tree
Algorithm: 1. Construct a set of trees with root nodes that contain each of the
individual symbols and their weights2. Place the set of trees into a priority queue.3. while the priority queue has more than one item4. 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
NCUE CSIE Wireless Communications and Networking Laboratory
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) EndEnd
Time complexity:Time complexity: )log( nnO45
NCUE CSIE Wireless Communications and Networking Laboratory
Question:
Insert 5, then insert 80?
46
NCUE CSIE Wireless Communications and Networking Laboratory
Follow the data to build min-max heap26, 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 Deap26, 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?
))log((22)2( nnOkI k
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 ︰
4)115(log2 Min ︰
h 4 5 6
Fh+2-1 7 12 20
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?
1
1... 110
m
mmmm
hh
Node ︰
)1()1
1(... 110
mm
mmmm
hhKey ︰
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
Question:
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
Answer:
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
95 q 136 q
The weighted value of the six external node is as follows : minimum W.E.P.L is?
21 q 32 q 53 q 74 q
72
Question:
NCUE CSIE Wireless Communications and Networking Laboratory
Briefly describe Huffman encoding and illustrate if for the following data step by stepA B A D C D A C A B B C B B B A
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 occuranceI1=0.4, I2=0.3, I3=0.01, I4=0.08, I5=0.06, I6=0.06 (1) Construct the opcodes using Huffman’s method(2) Calculate the average number of bits per instruction
75
Question:
NCUE CSIE Wireless Communications and Networking Laboratory
76
Reference
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 〞 , SBaker & Taylor Books, 2004
Fred Buckley, and Marty Lewinter 〝 A Friendly Introduction to Graph Theory 〞 Prentice Hall, 2002
〝資料結構 -使用 C語言〞蘇維雅譯,松崗, 2004 〝資料結構 -使用 C語言 〞 蔡明志編著,全華, 2004 〝資料結構 (含精選試題 )〞洪逸編著,鼎茂, 2005