8-10-12-02-22.29.c

來源:互聯網
上載者:User

  一個類比函數,目的在於找到一對節點的最近公用祖先.實現思想是首先將樹映射到一個數組中,跟節點範圍為ROOT,其餘節點範圍為父節點索引,這樣可以輕鬆找到父節點.於是,一個顯而易見的類比函數出現了.

/*8-10-12-02-22.29.c -- 第八章第十題*/</p><p>/*This is a model that can return a proximate communal parent betwen two node*/<br />/*First, map a tree to an array*/</p><p>ElementType FindCommunalParent (const ElementType * const array, const int v, const int w) ;</p><p>ElementType FindCommunalParent (const ElementType * const array, const int v, const int w)<br />{<br />if (ROOT == v || ROOT == w)<br />return FAILED ;<br />if (array[v] < array[w])<br />{<br />while (w != ROOT && array[v] != array[w])<br />w = array[w] ;<br />}<br />else if (array[v] > array[w])<br />{<br />while (v != ROOT && array[v] != arrat[w])<br />v = array[v] ;<br />}<br />else<br />return array[v] ;/*Or array[w]*/<br />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.