Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare BST=node("ekran"#"screen"
- node("adapter"#"adapter"
- empty
- node("klawiatura"#"keyboard"
- empty
- empty
- )
- )
- node("komputer"#"computer"
- empty
- node("mysz"#"mouse"
- empty
- empty
- )
- )
- )
- fun {LessStr A B}
- case A#B of
- nil#nil then false
- []
- nil#_ then false
- []
- _#nil then false
- []
- _#_ then
- if {StringToAtom A} < {StringToAtom B} then
- true
- else
- false
- end
- end
- end
- declare
- fun {GetNode BT Word}
- case BT
- of node(W#T L R) then
- if W==Word then T
- elseif {LessStr W Word} then {GetNode R Word}
- else {GetNode L Word}
- end
- else raise 'brak podanego slowa' end
- end
- end
- %{Inspect {GetNode BST "mysz"}}
- declare
- fun {AddNode BT Word#Translate}
- if BT == empty then node(Word#Translate empty empty) else
- local node(W#T L R) = BT in
- if Word == W then node(Word#Translate L R)
- elseif {LessStr W Word} then node(W#T L (if R==empty then node(Word#Translate empty empty)
- else {AddNode R Word#Translate} end)
- )
- else node(W#T (if L==empty then node(Word#Translate empty empty)
- else {AddNode L Word#Translate} end) R)
- end
- end
- end
- end
- %{Inspect {AddNode BST "aaaaa"#"aaaaa"}}
- declare fun {Preorder T}
- local fun {Preord T#Labels}
- case T#Labels
- of empty#Labels then Labels
- [] node(V T1 T2)#Labels then
- V|{Preord T1#{Preord T2#Labels}}
- end
- end
- in {Preord T#nil}
- end
- end
- %{Inspect {Preorder BST}}
- declare
- fun {ListToTree L}
- local fun {ToTree LI BT}
- case LI
- of H|T then {ToTree T {AddNode BT H.2#H.1}}
- [] nil then BT
- end
- end
- in {ToTree L empty}
- end
- end
- declare LISTA = {Preorder BST}
- {Inspect {ListToTree LISTA}}
Add Comment
Please, Sign In to add comment