Skip to content

Instantly share code, notes, and snippets.

@quinnj
Created October 9, 2013 21:30
Show Gist options
  • Save quinnj/6908770 to your computer and use it in GitHub Desktop.
Save quinnj/6908770 to your computer and use it in GitHub Desktop.
abstract BTree
type Empty <: BTree
end
type Node <: BTree
info::Int
left::BTree
right::BTree
end
const min_depth = 4
function make(val, d)
if d > 0
Node(val, make(2*val-1, d-1), make(2*val, d-1))
else
Node(val, Empty(), Empty())
end
end
check(t::Empty) = 0
check(t::Node) = t.info + check(t.left) - check(t.right)
function binary_trees(N::Int=10)
max_depth = N
stretch_depth = max_depth + 1
c = check(make(0, stretch_depth))
long_lived_tree = make(0, max_depth)
d = min_depth
for i = 0:div(max_depth - d, 2)
niter = 1 << (max_depth - d + min_depth)
c = 0
for j = 1:niter
c += check(make(i, d)) + check(make(-i, d))
end
d += 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment