Trie implementation

Hello! I have to write a Prolog program for my university and its compiler version of Prolog is 6.6.6. However, the problem is perfectly and best done with the data structure trie. I have found this library https://www.swi-prolog.org/pldoc/man?section=trie which is perfect and I have my program done and is perfect in time running and memory usage but I need the implementation of this library, at least of the basic functions of the library in order my program to be compiled from my university grader.

If your goal is to see the source code for trie written as Prolog code, do not look for it with SWI-Prolog as trie is written in C.

Ok, but I need Swi-prolog code for trie in order my program to be compiled in version 6.6.6 of Swi-Prolog. EricGT, I have not understand your answer (I am beginner in Prolog) how can I use the c code for trie in Prolog ? Do you mean that if I copy this code in my Prolog code file the functions of trie that I have used will run ?

No.

Your teacher probably wants you to implement the trie using Prolog, but in SWI-Prolog to get the code to run faster, the low level parts of trie are implemented in C. So if you want the source that implements a tree in Prolog you will have to look elsewhere.

My teacher doesn’t want to implement trie as we have done only few classes in Prolog. Teacher just gave us a problem and I found that the perfect solution is trie. However, university compiler is old so I need the trie implemented in Prolog. Can you propose me where I can found this ? I thought this site because it is the “official” site of SWI-Prolog.

Normally I would suggest using listing/1 but that is out because the code is implemented using C as I noted. Next I would suggest RosettaCode, but before I answered I checked and it is not there. The other way would be to search for it using Google.

EricGT many many thanks for your help! I really appreciated it! The only thing I found is this https://github.com/JanWielemaker/tabling_library/blob/5ac1b7e61fa9d643057ed0cb0de8d53428ef443d/trie.pl but it lacks a lot of methods that SWI-Prolog has in its library and mostly it doesn’t have method to update the value of a key or to delete a key and for the solution of the problem I need one of them.

As this is a class, is the teacher asking you to implement a trie in Prolog?

The reason I am not taking the time to write the code for you is that it seems to be homework and that is part of the problem the teacher is expecting you to solve.

One thing that may help you is to know that trie is sometimes spelled as tree, and searching for that might give you the answer you seek. See: Binary search trees in Prolog

EricGT I guarantee you that it is not part of my homework. We just have a problem to solve and I found that instead of using lists there is this data structure which seems perfect for what I need. I can send you the pdf of my answer but it is not in English. Tree in Prolog does the same thing as trie I mean if I insert ‘Eric’ it cuts the string in char and inserts one by one and if I insert ‘Eriba’ it only inserts characters b and a ?

Maybe it would be easier if you posted the problem as the teacher gave it to you and then we can figure out how much help we can give you without giving you the answer but still help you learn.

I think that I can change my program a little and make it work with this https://github.com/JanWielemaker/tabling_library/blob/5ac1b7e61fa9d643057ed0cb0de8d53428ef443d/trie.pl but I need also the method trie_update.

If all you’re doing is maintaining a set of key-value pairs, there are other data structures:
https://www.swi-prolog.org/pldoc/man?section=assoc
https://www.swi-prolog.org/pldoc/man?section=option
https://www.swi-prolog.org/pldoc/doc/SWI/library/rbtrees.pl

(Also dicts, but they’re not in your old version of SWI-Prolog)

I think i have the same exercise, did you manage to construct the missing methods and if so could you share?