Difference between revisions of "Size Balanced Tree"

From PEGWiki
Jump to: navigation, search
(Added properties)
(Added Rotations)
Line 12: Line 12:
  
 
<pre>
 
<pre>
          T
+
      T
        / \
+
      / \
        /  \
+
    /  \
      L    R
+
    L    R
      / \  / \
+
  / \  / \
    A  B C  D
+
  A  B C  D
 
</pre>
 
</pre>
  
Line 23: Line 23:
 
*<math>size(L) \ge size(C), size(D)</math>
 
*<math>size(L) \ge size(C), size(D)</math>
 
*<math>size(R) \ge size(A), size(B)</math>
 
*<math>size(R) \ge size(A), size(B)</math>
 +
 +
==Rotations==
 +
The rotations of SBTs are analogous to those in other self-balancing BSTs.
 +
 +
<pre>
 +
  -------------    Right Rotation    ------------
 +
  |    Q      |  --------------->    |    P    |
 +
  |  / \    |                      |    / \  |
 +
  -- P  C    |                      |  A  Q --
 +
    / \    <---    Left Rotation    --->  / \ 
 +
  A  B          <---------------          B  C
 +
</pre>
 +
 +
===Left Rotation===
 +
<pre>
 +
left-rotate(t):
 +
    k &larr; t.right
 +
    t.right &larr; k.left
 +
    k.left &larr; t
 +
    k.size &larr; t.size
 +
    t.size &larr; t.left.size + t.right.size + 1
 +
    t &larr; k
 +
</pre>
 +
 +
===Right Rotation===
 +
<pre>
 +
right-rotate(t):
 +
    k &larr; t.left
 +
    t.left &larr; k.right
 +
    k.right &larr; t
 +
    k.size &larr; t.size
 +
    t.size &larr; t.left.size + t.right.size + 1
 +
    t &larr; k
 +
</pre>

Revision as of 19:49, 19 August 2014

A size balanced tree (SBT) is a self-balancing binary search tree first published by Chinese student Qifeng Chen in 2007. The tree is rebalanced by examining the sizes of each node's subtrees. Its abbreviation resulted in many nicknames given by Chinese informatics competitors, including "Sha bi" tree (Chinese: 傻屄树; pinyin: Shǎ bī shù; literally meaning "dumb cunt tree") and "Super BT", which is a homophone to the Chinese term for snot (Chinese: 鼻涕; pinyin: bítì) suggesting that it is messy to implement. Contrary to what its nicknames suggest, this data structure can be very useful, and is also known to be easy to implement. Since the only extra piece of information that needs to be stored is sizes of the nodes (instead of other "useless" fields such as weights in treaps or colours in red–black tress), this makes it very convenient to implement the select and rank operations in dynamic order statistics problems. It supports standard binary search tree operations such as insertion, deletion, and searching in O(log n) time. According to Chen's paper, "this is the fastest known advanced binary search tree to date."

Properties

The size balanced tree examines each node's size (i.e. the number of nodes in the subtree rooted at that node) to determine when rotations should be performed. Each node T in the tree satisfies the following properties:

  • size(T.left) \ge size(T.right.left), size(T.right.right)
  • size(T.right) \ge size(T.left.left), size(T.left.right)

In other words, each child node of T is not smaller in size than the child nodes of its sibling. Clearly, we should consider the sizes of nonexistent children and siblings to be 0.

Consider the following example where T is the node in question, L, R are its child nodes, and A, B, C, D are subtrees which also satisfy the above SBT properties on their own.

       T
      / \
     /   \
    L     R
   / \   / \
  A   B C   D

Then, the node T must satisfy:

  • size(L) \ge size(C), size(D)
  • size(R) \ge size(A), size(B)

Rotations

The rotations of SBTs are analogous to those in other self-balancing BSTs.

  -------------    Right Rotation     ------------
  |    Q      |   --------------->    |     P    |
  |   / \     |                       |    / \   |
  -- P   C    |                       |   A   Q --
    / \    <---     Left Rotation     --->   / \  
   A   B          <---------------          B   C 

Left Rotation

left-rotate(t):
    k ← t.right
    t.right ← k.left
    k.left ← t
    k.size ← t.size
    t.size ← t.left.size + t.right.size + 1
    t ← k

Right Rotation

right-rotate(t):
    k ← t.left
    t.left ← k.right
    k.right ← t
    k.size ← t.size
    t.size ← t.left.size + t.right.size + 1
    t ← k