Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created May 3, 2014 22:14
Show Gist options
  • Save unixpickle/714bdf2e1dc992d4daaa to your computer and use it in GitHub Desktop.
Save unixpickle/714bdf2e1dc992d4daaa to your computer and use it in GitHub Desktop.
Calculate b-tree size
# Take a binary tree of depth D, where the lowest nodes have 1 bit, the second lowest have 2 bits, etc.
# This script finds the number of bits in a tree of depth D.
if process.argv.length isnt 3
console.log 'Usage: coffee calc.coffee <depth>'
process.exit()
depth = parseInt process.argv[2]
count = 0
for i in [0...depth]
count += Math.pow(2, i) * (depth - i)
console.log "there are #{count} bits"
regularNum = (1 << depth) - 1
console.log "this is #{100*count/regularNum}% the b-tree size"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment