Red black tree sort: Difference between revisions

Realize in F#
(Red black tree sort in Python)
(Realize in F#)
Line 10:
 
 
=={{header|F_Sharp|F#}}==
===The Functions===
This task extends [[Algebraic data types#F.23]] adding the following functions:
<lang fsharp>
// Red black tree sort. Nigel Galloway: June 17th., 2022
let fromSeq n=n|>Seq.fold(fun n g->insert g n) Empty
let toSeq g=let rec fN g=seq{match g with N(i,g,e,l)->yield l; yield! fN g; yield! fN e |_->()} in fN g
let delSeq n g=toSeq g|>Seq.except n|>fromSeq
let rec printN n s t=match n with N(i,g,e,l)->printN g (s+" ") "L";printfn "%s %s %A %d" s t i l; printN e (s+" ") "R" |_->()
</lang>
===The Task===
<lang fsharp>
let n=fromSeq [85; 57; 51; 50; 86; 39; 95; 49; 44; 48; 21; 83; 11; 59; 88; 66; 4; 40; 24; 82; 63; 22; 37; 32; 91; 74; 28; 75; 62; 81]
printfn "Populated Red Black Tree"; printN n "" "X"
let g=delSeq [32; 40; 57; 63; 66; 75; 86; 59; 83; 51; 24; 62; 82; 39; 37] n
printfn "\nRed Black Tree after deletions"; printN g "" "X"
</lang>
{{out}}
<pre>
Populated Red Black Tree
L Red 4
L Black 11
L Black 21
R Black 22
L Red 24
L Red 28
L Black 32
L Red 37
R Black 39
R Red 40
R Black 44
L Red 48
R Black 49
L Black 50
L Black 51
R Black 57
L Black 59
R Red 62
R Black 63
X Black 66
L Black 74
L Black 75
L Red 81
R Black 82
R Black 83
L Black 85
R Black 86
L Black 88
R Red 91
R Black 95
 
Red Black Tree after deletions
L Red 4
L Black 11
L Black 21
R Black 22
R Red 28
X Black 44
L Black 48
L Black 49
R Black 50
R Red 74
L Black 81
R Black 85
L Red 88
R Black 91
R Red 95
</pre>
=={{header|FreeBASIC}}==
Code originally by AGS.
Line 612 ⟶ 680:
{{out}}
[https://www.dropbox.com/s/hbrtaahsd6jmlyl/FreeBASIC_Red-black-tree_sort.bmp?dl=0 FreeBasic Red black tree sort image]
 
 
=={{header|Julia}}==
2,171

edits