github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/binarytree.lua (about)

     1  local function BottomUpTree(depth)
     2    if depth > 0 then
     3      depth = depth - 1
     4      local left, right = BottomUpTree(depth), BottomUpTree(depth)
     5      return { left, right }
     6    else
     7      return { }
     8    end
     9  end
    10  
    11  local function ItemCheck(tree)
    12    if tree[1] then
    13      return 1 + ItemCheck(tree[1]) + ItemCheck(tree[2])
    14    else
    15      return 1
    16    end
    17  end
    18  
    19  local N = tonumber(arg and arg[1]) or 0
    20  local mindepth = 4
    21  local maxdepth = mindepth + 2
    22  if maxdepth < N then maxdepth = N end
    23  
    24  do
    25    local stretchdepth = maxdepth + 1
    26    local stretchtree = BottomUpTree(stretchdepth)
    27    io.write(string.format("stretch tree of depth %d\t check: %d\n",
    28      stretchdepth, ItemCheck(stretchtree)))
    29  end