80 likes | 175 Views
1 2 3 4 5 6 7 8 9 0 1 b a a b c b b a b c $ a a b c b b a b c $ a b c b b a b c $ b c b b a b c $ c b b a b c $ b b a b c $ b a b c $ a b c $ b c $ c $ $. Provided by 89902098 吳亭範. [11,-]. [5,5]. [1,1]. [2,2]. [5,-]. 1. 1. 1. [2,-]. [11,-]. [3,-]. [5,-]. 1. [2,2]. [5,5].
E N D
1 2 3 4 5 6 7 8 9 0 1 b a a b c b b a b c $ a a b c b b a b c $ a b c b b a b c $ b c b b a b c $ c b b a b c $ b b a b c $ b a b c $ a b c $ b c $ c $ $ Provided by 89902098吳亭範 [11,-] [5,5] [1,1] [2,2] [5,-] 1 1 1 [2,-] [11,-] [3,-] [5,-] 1 [2,2] [5,5] [1,-] [4,5] [11,-] [6,-] [2,-] [11,-] 1 2 1 [7,-] [4,-] [6,-] Case 2: 節外生枝 [6,-] [3,-] [9,-] Case 3: 若無其事
Create Minimal Tree in Linear TimeSymbol definition • S = given sequence of numbers. • “Right branch” = The tree branch can be travered from root to right-most node by following only right-child • I construct the tree by directly reading the sequence one by one for every new-coming number • Please view in play mode for animation
New A[i+1] A[n] General Case • Assume right branch is referred as A[0](root), A[1], A[2]…A[n](right most node) • For each new coming node, linear search the right branch for proper place where S[A[i+1]]>S[new] and S[A[i]]<S[new] • Insert new between A[i] ,A[i+1] set that new = Right-Child[A[i]] and A[i+1] = Left-Child[new] A[0] A[1] A[i] < <
New A[i+1] A[n] Boundary CaseValue(New) > Value(A[n]) • Attach New at right-hand side of A[n] • Let new = Right-Child[A[n]] A[0] A[1] A[i] <
New A[i+1] A[n] Boundary CaseValue(New)<Value(A[0]) • Attach A[0] at left-hand side of New • Let A[n] = Left-Child[New] • The new “Right Branch” created. < A[0] A[1] A[i]
Proof of Correctness • In order traversal sequence • At each step, the newly added number is always at right-most side, thus being an appending to original sequence of “in-order traversal”. • The property of sequence S[1…n-1] will not be changed while new number appended at the end. • Heap like Optimal substructure • The minimal tree is like a Heap. Each S[parent] is smaller-equal than S[child] • Each Step Keep the optimality • As we insert New number between A[i],A[i+1]. • S(A[i])<=S(New)<=S(A[i+1]) • <=S(Subtree rooted by A[i+1]) • And S(0) <= S(1) <= S(2) <= …. <= S(A[i]) <= S(New) • Due to the correct traversal sequence and the number relation, this algorithm is guaranteed to generate a minima tree for the current string S[1…n].
Proof of Linearity • For each stepk • Costk = O(Search from Right) + O(Attach) • = Ф(n-i) + O(1) • = Фk • Total Cost • Фi= {Numbers of Nodes that < Value(New), which was removed from “Right brancn” and becoming child of New} At Step I • Each node is compared to New number only one time before removed. So |Comparisons of Search|= O(|S|) • Costall = Ф1+ Ф2+ …+ Ф|s| • = O(|S|)
Acknowledgements • Thanks to 王姵瑾 and 李沛倫 for helpful discussion.