CLRS 15.5最優二叉搜尋樹

來源:互聯網
上載者:User

15.5-1
CONSTRUCT-OPTIMAL-BST(root)
    n ← length[root]
    print r[1, n] is the root
    PRINT-OBST(1, n)

PRINT-OBST(i, j)
    m = r[i, j]
    if i = m
         print dm−1 d_{m-1} is the left child of km k_m
    else
         print kr[i,m−1] k_{r[i, m-1]} is the left child of km k_m
         PRINT-OBST(i, m-1)
    if j = m
         print dm d_m is the right child of km k_m
     else
         print kr[m+1,j] k_{r[m+1, j]} is the right child of km k_m
         PRINT-OBST(m+1, j)

15.5-2
最右二叉搜尋樹如下圖:

最後計算的代價為:3.12。

15.5-3
若有 ω \omega 表,每次計算 ω[i,j] \omega[i,j] 只需要 Θ(1) \Theta(1) 時間,沒有維護表則需要 Θ(j−i) \Theta(j-i) 時間,所以現在相當於在第二個 for 裡面再加一個 for 迴圈(不是嵌套在第三個 for 迴圈中),最後的複雜度依然是 Θ(n3) \Theta(n^3)。

15.5-4
第9、10行替換為:
   if i = j
       r = j
       e[i,j]= pi+qi−1+qj p_i+q_{i-1}+q_j
   else
       for r = root[i,j-1] to root[i+1,j]

在計算所有的 j−i=k j-i=k 的e[i,j]時(含有 k k 個結點),每個e[i,j]需要花費 root[i+1, j] – root[i, j-1] + 1次迭代,所有的 j−i=k j-i=k 的e[i,j]花費root[k,1] – root[1,k] + n – k。由於1 ≤ root[k, 1], root[1, k] ≤ n,所以root[k,1] – root[1,k] + n – k=Θ(n)。最後 k k 的變化範圍是 0 0 到 n

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.