(Translated by https://www.hiragana.jp/)
AA树 - 维基百科,自由的百科全书 とべ转到内容ないよう

AA树

维基百科ひゃっか自由じゆうてき百科ひゃっかぜん

AAざい電腦でんのう科學かがく一種いっしゅ形式けいしきてき平衡へいこうげんさがせひろじゅよう於高こうそんもうか檢索けんさくじょすうよりどころ。AAじゅてき名稱めいしょうよし它的發明はつめいしゃおもねしかあま·やす德森とくのもり(Arne Andersson)而來。

AAべにくろじゅてきいちしゅ變種へんしゅ是安こりゃす德森とくのもり教授きょうじゅざい1993ねんねんざいてき論文ろんぶん《Balanced search trees made simple》ちゅうかい紹,設計せっけいてき目的もくてき減少げんしょうべにくろじゅ考慮こうりょてき同情どうじょうきょう區別くべつ於紅くろじゅてき,AAじゅてきべに節點せってんただのう作為さくいみぎ葉子ようこしたがえ而大だい簡化りょう維護2-3てき模擬もぎ。維護べにくろじゅてき平衡へいこう需要じゅよう考慮こうりょ7しゅ不同ふどうてき情況じょうきょう:

いんためAAゆう嚴格げんかくてき條件じょうけん(べに節點せってんただのうためみぎ節點せってん),ただ考慮こうりょ2しゅじょうがた:

旋轉せんてん平衡へいこう

[编辑]

平衡へいこういちべにくろじゅ需要じゅよう記錄きろく其顏しょく,而AAざいまい節點せってん記錄きろく其"level"這相とう於紅くろじゅ節點せってんてきくろ高度こうど

  1. 所有しょゆう節點せってんてきlevel1
  2. まいひだり孩子てきlevel恰好かっこうため父親ちちおやてきlevelげんいち
  3. まいみぎ孩子てきlevelとう於其父親ちちおやてきlevelあるため父親ちちおやてきlevelげんいち
  4. まいみぎ孫子まごこてきlevel嚴格げんかくしょう於其祖父そふ節點せってんてきlevel
  5. まいいちlevelだい於1てき節點せってんゆう兩個りゃんこ節點せってん

兩個りゃんこlevelしょうどうまとてんあいだてきあたり水平すいへい,也就べにくろじゅうえてきべに。往右てき水平すいへい允許いんきょてきただし不可ふか連續れんぞく(べにくろじゅ性質せいしつ);不能ふのう有向ゆうこうひだりてき水平すいへいあたり(AAじゅ性質せいしつ)。よしためAAてき條件じょうけんべにくろじゅ嚴格げんかく所以ゆえんおもしん平衡へいこういち顆AAかい比重ひじゅうしん平衡へいこういち顆紅くろじゅ容易ようい

插入そうにゅう刪除かいゆずるAAへんてき平衡へいこう(そく違反いはん它的性質せいしつ)。恢復かいふく平衡へいこうただ需兩しゅ操作そうさ:"skew""split". Skew一個右旋轉使得子樹中向左的水平邊變成向右的水平邊;Split一個左旋並增加子樹根節點的level(請看はんれい)使つかいとく連續れんぞくこうみぎてき水平すいへい消失しょうしつ平衡へいこう插入そうにゅう刪除操作そうさてき實現じつげんよしskew及split決定けってい旋轉せんてん,而不ざいしゅほどしきちゅう判斷はんだん

function skew is
    input: T, a node representing an AA tree that needs to be rebalanced.
    output: Another node representing the rebalanced AA tree.

    if nil(T) then
        return Nil
    else if nil(left(T)) then
        return T
    else if level(left(T)) == level(T) then
        Swap the pointers of horizontal left links.
        L = left(T)
        left(T) := right(L)
        right(L) := T
        return L
    else
        return T
    end if
end function

Skew:

function split is
    input: T, a node representing an AA tree that needs to be rebalanced.
    output: Another node representing the rebalanced AA tree.

    if nil(T) then
        return Nil
    else if nil(right(T)) or  nil(right(right(T))) then
        return T
    else if level(T) == level(right(right(T))) then
        We have two horizontal right links.  Take the middle node, elevate it, and return it.
        R = right(T)
        right(T) := left(R)
        left(R) := T
        level(R) := level(R) + 1
        return R
    else
        return T
    end if
end function

Split:

插入そうにゅう

[编辑]

ざい遞迴てきじつ做中,じょりょう節點せってんそとざい每次まいじてき遞迴結束けっそくよびさけべskewsplitそく

function insert is
    input: X, the value to be inserted, and T, the root of the tree to insert it into.
    output: A balanced version T including X.

    Do the normal binary tree insertion procedure. Set the result of the
    recursive call to the correct child in case a new node was created or the
    root of the subtree changes.
    if nil(T) then
        Create a new leaf node with X.
        return node(X, 1, Nil, Nil)
    else if X < value(T) then
        left(T) := insert(X, left(T))
    else if X > value(T) then
        right(T) := insert(X, right(T))
    end if
    Note that the case of X == value(T) is unspecified. As given, an insert
    will have no effect. The implementor may desire different behavior.

    Perform skew and then split. The conditionals that determine whether or
    not a rotation will occur or not are inside of the procedures, as given
    above.
    T := skew(T)
    T := split(T)

    return T
end function

刪除

[编辑]

ざいだい部分ぶぶんてきげんさがせひろじゅ,刪除いち內部節點せってん轉換てんかんなり交換こうかん內部節點せってん及其さい接近せっきんてき前驅ぜんくある後繼こうけい節點せってん,這取けつ使用しようしゃためりょう平衡へいこう這顆じゅゆういくちゅう方法ほうほう,Andersson教授きょうじゅ描述てきoriginal paper页面そん档备份そん互联网档あんさい基本きほんてき,儘管它還のうさいゆう。刪除だい一件事是降低其level(如果以),於是,せいlevel必須ひっすskewsplit,這個方法ほうほうさい受到歡迎かんげいてきいんため它的概念がいねんえき懂,以列舉成れつさん簡單かんたん驟:

  1. 如果以的ばなし減少げんしょう其level
  2. Skew其level.
  3. Split其level.
function delete is
    input: X, the value to delete, and T, the root of the tree from which it should be deleted.
    output: T, balanced, without the value X.
   
    if nil(T) then
        return T
    else if X > value(T) then
        right(T) := delete(X, right(T))
    else if X < value(T) then
        left(T) := delete(X, left(T))
    else
        If we're a leaf, easy, otherwise reduce to leaf case. 
        if leaf(T) then
            return Nil
        else if nil(left(T)) then
            L := successor(T)
            right(T) := delete(value(L), right(T))
            value(T) := value(L)
        else
            L := predecessor(T)
            left(T) := delete(value(L), left(T))
            value(T) := value(L)
        end if
    end if

    Rebalance the tree. Decrease the level of all nodes in this level if
    necessary, and then skew and split all nodes in the new level.
    T := decrease_level(T)
    T := skew(T)
    right(T) := skew(right(T))
    if not nil(right(T))
        right(right(T)) := skew(right(right(T)))
    end if
    T := split(T)
    right(T) := split(right(T))
    return T
end function
function decrease_level is
    input: T, a tree for which we want to remove links that skip levels.
    output: T with its level decreased.

    should_be = min(level(left(T)), level(right(T))) + 1
    if should_be < level(T) then
        level(T) := should_be
        if should_be < level(right(T)) then
            level(right(T)) := should_be
        end if
    end if
    return T
end function

這個もう展示てんじりょう良好りょうこうてき刪除しめせはんAndersson paper页面そん档备份そん互联网档あん).

效能こうのう

[编辑]

AAてき性能せいのうかずくれないくろじゅ類似るいじてき。儘管AAべにくろじゅ做較つぎ旋轉せんてん,卻較容易よういじつ做,しゃ效能こうのう相似そうじただしAA高度こうど較淺,查找時間じかん較快[1]

まいり

[编辑]

引用いんよう

[编辑]

外部がいぶ連結れんけつ

[编辑]