Skip to content

Instantly share code, notes, and snippets.

@gungorkocak
Created June 27, 2012 15:55
Show Gist options
  • Save gungorkocak/3005004 to your computer and use it in GitHub Desktop.
Save gungorkocak/3005004 to your computer and use it in GitHub Desktop.
creating a basic functional programmed bst implementation
% Module: binary_tree
% Exposes new binary tree syntax.
%
% Author: monkegjinni <gngr.kck@gmail.com>
%
% Notices:
% !These is a very naive tree implementation. In Production do not use this.
% For more efficient and product ready tree implementation; you should check
% otp_src_R<version>B<revision>/lib/stdlib/src/gb_trees.erl .
%
% Credit: goes to http://learnyousomeerlang.com/recursion#more-than-lists (:
-module( binary_tree ).
-compile([ export_all ]).
% Public: create()
% Creates an initial binary tree node with an empty Node, Right, and Left.
%
% Returns: an initial empty binary tree node as { Left, Key, Value, Right }
%
% Example:
% Tree = binary_tree:create()
% # => Tree = { nil, nil, nil, nil }
create() ->
{ nil, nil, nil, nil }.
% Public: insert( Tree, Key, Value )
% Inserts given Value to the binary tree, Tree.
%
% Parameters:
% Tree: a binary tree variable that is initialized via create()
% and then maybe modified with insert.
% Key: a key to place new node its proper place in Tree.
% Value: a value to be inserted in the new node along with the Key.
%
% Returns:
% Updated tree after insertion being successful.
%
% Example:
% % we had a Tree variable initialized via create(), inserted 5 and then 3.
% % { { nil, "Acar", "acar@mail.com", nil }, "Ahmet", "ahmet@ahmet.com", nil }
%
% NewTree = binary_tree:insert( Tree, "Veli", "veli@veli.com" ).
%
% % => NewTree = {
% % { nil, "Acar", "acar@mail.com", nil },
% % "Ahmet", "ahmet@ahmet.com",
% % { nil, "Veli", "veli@veli.com" }
% % }
%
% Notices:
% * Tree variable must be initialized with binary_tree:create() first.
% You will get your Tree variable here.
% * Insertion is seamless. If it sees corresponding node nil,
% then value will be inserted; no matter it is
% master node or not ( first node after create() ).
% * After every insertion a new tree is created.
% Any comment for improvement, appreciated (:
insert( { nil, nil, nil, nil }, Key, Value ) ->
{ nil, Key, Value, nil };
insert( nil, Key, Value ) ->
{ nil, Key, Value, nil };
insert( { Left, NodeKey, NodeValue, Right }, Key, Value ) when NodeKey =/= nil, Key < NodeKey ->
{ insert( Left, Key, Value ), NodeKey, NodeValue, Right };
insert( { Left, NodeKey, NodeValue, Right }, Key, Value ) when NodeKey =/= nil, Key > NodeKey ->
{ Left, NodeKey, NodeValue, insert( Right, Key, Value ) };
insert( { Left, NodeKey, NodeValue, Right }, Key, _ ) when NodeKey =/= nil, Key =:= NodeKey ->
{ Left, NodeKey, NodeValue, Right }.
% Public: lookup( Tree, Key )
% Searches binary tree Tree. if Key exists at any one of its nodes gives its result.
%
% Parameters:
% Tree: a binary tree form of { Left, Key, Value, Right } to be searched.
% Key: a value to be searched.
%
% Returns: Value corresponding Key given.
%
% Examples:
%
% Tree = {
% {nil, "acar", "acar@mail.com", nil},
% "Ahmet", "ahmet@mail.com",
% {
% {nil, "Gaffur", "gaffur@mail.com", nil},
% "Veysel", "Veysel.com",
% nil
% }
% }
%
% Value = binary_tree:lookup( Tree, "Gaffur" ).
% # => "gaffur@mail.com"
%
% NonExistingValue = binary_tree:lookup( Tree, "Nadide" ).
% # => nil
lookup( { _, nil, _, _ }, _ ) ->
nil;
lookup( nil, _ ) ->
nil;
lookup( { Left, NodeKey, _, _ }, Key ) when Key < NodeKey ->
lookup( Left, Key );
lookup( { _, NodeKey, _, Right }, Key ) when Key > NodeKey ->
lookup( Right, Key );
lookup( { _, NodeKey, Value, _ }, Key ) when Key =:= NodeKey ->
Value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment