AVL tree: Difference between revisions

m
→‎{{header|Logtalk}}: Test formatting.
(→‎{{header|Lua}}: Added Logtalk solution.)
m (→‎{{header|Logtalk}}: Test formatting.)
Line 8,454:
 
=={{header|Logtalk}}==
The Logtalk library comes with an AVL implementation of its `<code>dictionaryp`</code> protocol, whose definition begins thusly:
 
<syntaxhighlight lang="logtalk">
Line 8,468:
{{Out}}
 
This makes the use of an AVL tree in Logtalk dirt simple. First we load the `<code>dictionaries`</code> library.
 
<pre>
Line 8,483:
</pre>
 
Using Logtalk's broadcast notation to avoid having to repeatedly type `<code>avltree::`</code> in front of every operation we can insert some keys, update one, and look up values. Note that since variables in Logtalk, as in most declarative languages, cannot be altered, we actually have several dictionaries (`<code>D0`</code> through `<code>D4`</code>) representing the initial empty state, various intermediate states, as well as the final state.
 
<pre>
Line 8,503:
</pre>
 
To save some rote typing, the `<code>as_dictionary/2`</code> method lets a list of `<code>Key-Value`</code> pairs be used to initialize a dictionary instead:
 
<pre>
17

edits