Wiki Agenda Contact Version française

Tree of array

Build a tree of logarithmic height from an array, in linear time, while preserving the order of elements


Authors: Jean-Christophe Filliâtre

Topics: Array Data Structure / Trees / Algorithms

Tools: Why3

see also the index (by topic, by tool, by reference, by year)


Build a tree of logarithmic height from an array, in linear time, while preserving the order of elements

Author: Jean-Christophe FilliĆ¢tre (CNRS)

module TreeOfArray

  use int.Int
  use int.ComputerDivision
  use int.Power
  use array.Array
  use array.ToList
  use bintree.Tree
  use bintree.Size
  use bintree.Inorder
  use bintree.Height

  let rec tree_of_array_aux (a: array 'a) (lo hi: int) : tree 'a
    requires { 0 <= lo <= hi <= length a }
    variant  { hi - lo }
    ensures  { let n = hi - lo in
               size result = n && inorder result = to_list a lo hi &&
               (n > 0 ->
                  let h = height result in power 2 (h-1) <= n < power 2 h) }
  =
    if hi = lo then
      Empty
    else
      let mid = lo + div (hi - lo) 2 in
      let left = tree_of_array_aux a lo mid in
      let right = tree_of_array_aux a (mid + 1) hi in
      Node left a[mid] right

  let tree_of_array (a: array 'a) : tree 'a
    ensures { inorder result = to_list a 0 (length a) }
    ensures { size result > 0 -> let h = height result in
                power 2 (h-1) <= size result < power 2 h }
  =
    tree_of_array_aux a 0 (length a)

end

download ZIP archive

Why3 Proof Results for Project "tree_of_array"

Theory "tree_of_array.TreeOfArray": fully verified

ObligationsAlt-Ergo 1.30
VC tree_of_array_aux1.17
VC tree_of_array0.01