Skip to content

Instantly share code, notes, and snippets.

@jlouis
Created January 21, 2011 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlouis/790587 to your computer and use it in GitHub Desktop.
Save jlouis/790587 to your computer and use it in GitHub Desktop.
Creating a tree of size N in time O(lg N)
-module(foo).
-compile(export_all).
%% Create a binary tree in O(depth)
build(0, T) -> T;
build(K, T) -> build(K-1, {T, T}).
%% What is the size of such a tree
sz(X) when is_atom(X) -> 1;
sz({L, R}) ->
1 + sz(L) + sz(R).
%% Test
test() ->
X = this_is_an_atom,
T = build(10, X),
sz(T).
%% foo:test() ==> 2047 :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment