Created
January 21, 2011 22:47
-
-
Save jlouis/790587 to your computer and use it in GitHub Desktop.
Creating a tree of size N in time O(lg N)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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