Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Treap 樹:用隨機優先級打造近似平衡的二叉搜尋樹

Treap 是 Tree 與 Heap 的合成詞,顧名思義,它同時滿足兩套性質,卻意外地好寫——這也是它常被拿來替代 AVL、紅黑樹的原因。

兩個屬性,兩條規則

Treap 的每個節點帶有兩個屬性:值(value)與優先級(priority)。值滿足二叉搜尋樹性質(左 < 中 < 右),優先級則滿足大頂堆性質(左右子節點的優先級都小於自己)。插入時為新節點隨機產生一個優先級,先照 BST 規則放進去,再用左旋或右旋把堆性質修回來;刪除 likewise 靠旋轉把待刪節點推到葉子再摘掉。由於優先級是隨機的,樹高在期望上為 O(logN),查詢、插入、刪除的期望複雜度都是 O(logN)。

public class TreapTree {

  public static class Node {
    public int v;
    public int p;
    Node lc;
    Node rc;
    public Node(int x) {
      v = x;
      p = (int)(Math.random() * Integer.MAX_VALUE);
      lc = null;
      rc = null;
    }
  }

  private Node root = null;

  // 檢查優先級(公共方法)
  public boolean check() { return check(root); }

  private boolean check(Node node) {
    if ( node == null ) return true;
    else {
      if ( node.lc != null && node.p < node.lc.p ) return false;
      if ( node.rc != null && node.p < node.rc.p ) return false;
      return ( check(node.lc) && check(node.rc) );
    }
  }

  // 刪除(公共方法)
  public void delete(int x) { root = searchAndDelete(root, x); }

  // 刪除節點並以旋轉維持 Treap 性質
  private Node delete(Node node) {
    if ( node.lc == null ) {
      return node.rc;
    } else if ( node.rc == null ) {
      return node.lc;
    } else {
      if ( node.lc.p > node.rc.p ) {
        node = rotateRight(node);
        node.rc = delete(node.rc);
      } else {
        node = rotateLeft(node);
        node.lc = delete(node.lc);
      }
      return node;
    }
  }

  // 深度
  public int depth() { return depth(root); }

  private int depth(Node node) {
    if ( node == null ) return 0;
    else {
      int x = depth(node.lc);
      int y = depth(node.rc);
      return ((x>y)?x:y) + 1;
    }
  }

  // 插入
  public void insert(int x) { root = insert(root, x); }

  private Node insert(Node node, int x) {
    Node cur = new Node(x);
    if ( node == null ) {
      return cur;
    } else {
      if ( node.v > x ) {
        node.lc = insert(node.lc, x);
        if ( node.lc.p > node.p ) return rotateRight(node); // 左孩子優先級大 → 右旋
      } else if ( node.v < x ) {
        node.rc = insert(node.rc, x);
        if ( node.rc.p > node.p ) return rotateLeft(node); // 右孩子優先級大 → 左旋
      }
      return node;
    }
  }

  // 中序遍歷輸出
  public void print() { print(root); }

  private void print(Node node) {
    if ( node != null ) {
      print(node.lc);
      System.out.printf("%d ", node.v);
      print(node.rc);
    }
  }

  // 左旋:右孩子成為子樹根
  private Node rotateLeft(Node node) {
    Node right = node.rc; node.rc = right.lc; right.lc = node;
    return right;
  }

  // 右旋:左孩子成為子樹根
  private Node rotateRight(Node node) {
    Node left = node.lc; node.lc = left.rc; left.rc = node;
    return left;
  }

  // 搜尋並刪除值為 x 的節點
  private Node searchAndDelete(Node node, int x) {
    if ( node == null ) return null;
    else if ( node.v > x ) {
      node.lc = searchAndDelete(node.lc, x);
    } else if ( node.v < x ) {
      node.rc = searchAndDelete(node.rc, x);
    } else {
      node = delete(node);
    }
    return node;
  }

  // 測試
  public static void main(String[] args) {
    TreapTree tree = new TreapTree();
    for ( int i=0; i<1024; i++ ) tree.insert(i);
    for ( int i=128; i<256; i++ ) tree.delete(i);
    System.out.printf("depth : %d, check : %b%n", tree.depth(), tree.check());
    tree.print();
  }
}

測試主程式先依序插入 1024 個遞增數值,再刪掉 128~255 這段,最後印出樹的深度並用 check() 驗證堆性質是否仍成立。由於插入順序是單調的,這段測試剛好能檢驗旋轉是否真的發揮了平衡效果——若沒有 Treap 的隨機優先級,遞增插入會退化成單向鏈表。

Copyright © Linux教程網 All Rights Reserved