Sorting algorithms/Tree sort on a linked list: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: added solution)
Line 980: Line 980:
d c e b a -> a b c d e
d c e b a -> a b c d e
</pre>
</pre>

=={{header|Yabasic}}==
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Tree_sort_on_a_linked_list
// by Galileo, 04/2022

clear screen

KEY = 0 : LEFT = 1 : RIGHT = 2

index = 0 : size = 10

dim tree$(size)
dim indTree(size, 3)


sub makeNode(prev, branch, word$)
if indTree(prev, branch) = 0 then
index = index + 1
if index > size then size = size + 10 : redim tree$(size) : redim indTree(size, 3) end if
indTree(prev, branch) = index
tree$(index) = word$
indTree(index, KEY) = 1
else
insertNode(word$, indTree(prev, branch))
end if
end sub


sub insertNode(word$, prev)
local pal$, ant$
pal$ = lower$(word$)
ant$ = lower$(tree$(prev))
if ant$ <> "" then
if pal$ < ant$ then
makeNode(prev, LEFT, word$)
elseif pal$ > ant$ then
makeNode(prev, RIGHT, word$)
elseif pal$ = ant$ then
indTree(prev, KEY) = indTree(prev, KEY) + 1
end if
else
index = index + 1
tree$(index) = word$
indTree(index,KEY) = 1
end if
end sub


sub showTree(numreg)
if indTree(numreg,LEFT) then
showTree(indTree(numreg, LEFT))
end if
print tree$(numreg), " ";
if indTree(numreg, RIGHT) then
showTree(indTree(numreg, RIGHT))
end if
end sub


sub makeTree(line$)
local n, numwords, words$(1)
numwords = token(line$, words$())
for n = 1 to numwords
insertNode(words$(n), 1)
next n
end sub


makeTree("one two three four five six seven eight nine ten")
showTree(1)
print</lang>
{{out}}
<pre>eight five four nine one seven six ten three two
---Program done, press RETURN---</pre>


=={{header|zkl}}==
=={{header|zkl}}==