Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Balanced Binary Tree
Hi,

i have to write a program that use a cache to memorize integer numbers.
I want this cache to run fast, so i've used, in my C program, a bb-tree.
Now i must translate this program in prolog language, and i don't know
how to code a bb-tree predicate.
The C code is simple enough, but without pointers, i don't know how to do...
The length of the cache can be around 1.000.000 elements.
Some ideas, code, reference?

Thanks,
Lucas

Report this thread to moderator Post Follow-up to this message
Old Post
lucas
11-26-04 09:09 PM


Re: Balanced Binary Tree
lucas wrote:

> i have to write a program that use a cache to memorize integer numbers.
> I want this cache to run fast, so i've used, in my C program, a bb-tree.
> Now i must translate this program in prolog language, and i don't know
> how to code a bb-tree predicate.

How about using this structure:

is_bbtree([]).
is_bbtree(tr(Val,Left,Right)) :-
number(Val),
is_bbtree(Left),
is_bbtree(Right)


Best regards,
Markus.

Report this thread to moderator Post Follow-up to this message
Old Post
Markus Triska
11-30-04 01:57 AM


Re: Balanced Binary Tree
"lucas" <kingpin@freemail.it> wrote in message
news:25977234.0411261138.2ce2404f@posting.google.com...
> Hi,
>
> i have to write a program that use a cache to memorize integer numbers.
> I want this cache to run fast, so i've used, in my C program, a bb-tree.
> Now i must translate this program in prolog language, and i don't know
> how to code a bb-tree predicate.
> The C code is simple enough, but without pointers, i don't know how to do.
.
> The length of the cache can be around 1.000.000 elements.
> Some ideas, code, reference?

Maybe look for a good implementation of association lists.  For example, SWI
-prolog
includes one that's implemented as a 2-3 tree, in assoc.pl.



Report this thread to moderator Post Follow-up to this message
Old Post
Alan Baljeu
11-30-04 09:01 PM


Re: Balanced Binary Tree
kingpin@freemail.it (lucas) wrote in message news:<25977234.0411261138.2ce2404f@posting.goo
gle.com>...
> Hi,
>
> i have to write a program that use a cache to memorize integer numbers.
> I want this cache to run fast, so i've used, in my C program, a bb-tree.
> Now i must translate this program in prolog language, and i don't know
> how to code a bb-tree predicate.
> The C code is simple enough, but without pointers, i don't know how to do.
.
> The length of the cache can be around 1.000.000 elements.
> Some ideas, code, reference?
>
> Thanks,
> Lucas



I've translated a c source code that implemented simple bb-tress.
Here is the code...i have to write only the remove function (but i
don't need
it..).
Some comments (really slow, slow, normal, good...)?


% Right rotate
bbtree_rightrot(bbtree(K, bbtree(K1, Left1, Right1, Level1), Right,
Level), New) :- Level1 =:= Level,
New = bbtree(K1, Left1, bbtree(K, Right1,
Right, Level), Level1), !.
bbtree_rightrot(Tree, Tree):-!.

% Left rotate
bbtree_leftrot(bbtree(K, Left, bbtree(K1, Left1, bbtree(K2, Left2,
Right2, Level2), Level1), Level), New) :- Level2 =:= Level,
Level3 is Level1 + 1,
New = bbtree(K1, bbtree(K,Left, Left1,
Level1), bbtree(K2,Left2,Right2, Level2), Level3), !.
bbtree_leftrot(Tree,Tree):-!.





% Insert
bbtreeinsert(Key, bbtree(void), bbtree(Key, bbtree(void),
bbtree(void), 0)):-!.
bbtreeinsert(Key, bbtree(K, Left, Right, Level), New) :- Key < K,
bbtreeinsert(Key, Left, New1),!,
bbtree_rightrot(bbtree(K, New1, Right, Level), New2),
bbtree_leftrot(New2, New).
bbtreeinsert(Key, bbtree(K, Left, Right, Level), New) :- Key > K,
bbtreeinsert(Key, Right, New1),!,
bbtree_rightrot(bbtree(K, Left, New1, Level), New2),
bbtree_leftrot(New2, New).

% Find
bbtreefind(Key, bbtree(void), 0).
bbtreefind(Key, bbtree(Key, Left, Right), 1).
bbtreefind(Key, bbtree(K, Left, Right), Flag) :- Key < K, !,
bbtreefind(Key, Left, Flag).
bbtreefind(Key, bbtree(K, Left, Right), Flag) :- Key > K, !,
bbtreefind(Key, Right, Flag).

Report this thread to moderator Post Follow-up to this message
Old Post
lucas
12-02-04 01:57 AM


Re: Balanced Binary Tree
The indentation seems to be messed up. In bbtreefind/3, you could do
without the "Flag" argument:

bbtree_keythere(Key, bbtree(Key, _, _)).
bbtree_keythere(Key, bbtree(K, Left, _)) :-
Key < K,
!,
bbtree_keythere(Key, Left).
bbtree_keythere(Key, bbtree(K, _, Right)) :-
Key > K,
bbtree_keythere(Key, Right).

If the key is *not* there, the predicate will then fail - catch this
case with

...
\+ bbtree_keythere(Key,Tree)
....

if you need it.

Best regards,
Markus.

Report this thread to moderator Post Follow-up to this message
Old Post
Markus Triska
12-02-04 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Prolog archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:54 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.