[prev] 14 [next]

Rebalancing Trees (cont)

Implementation of partition operation:

partition(tree,i):
|  Input  tree with n nodes, index i
|  Output tree with item #i moved to the root
|
|  m=#nodes(left(tree))
|  if i < m then
|     left(tree)=partition(left(tree),i)
|     tree=rotateRight(tree)
|  else if i > m then
|     right(tree)=partition(right(tree),i-m-1)
|     tree=rotateLeft(tree)
|  end if
|  return tree

Note:  size(tree) = n,   size(left(tree)) = m,   size(right(tree)) = n-m-1   (why -1?)