Guest User

Untitled

a guest
Mar 14th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Oz 1.87 KB | None | 0 0
  1. declare BST=node("ekran"#"screen"
  2.          node("adapter"#"adapter"
  3.               empty
  4.               node("klawiatura"#"keyboard"
  5.                empty
  6.                empty
  7.               )
  8.              )
  9.          node("komputer"#"computer"
  10.               empty
  11.               node("mysz"#"mouse"
  12.                empty
  13.                empty
  14.               )
  15.              )
  16.         )
  17.  
  18. fun {LessStr A B}
  19.    case A#B of
  20.       nil#nil then false
  21.    []
  22.       nil#_ then false
  23.    []
  24.       _#nil then false
  25.    []
  26.       _#_ then
  27.       if {StringToAtom A} < {StringToAtom B} then
  28.      true
  29.      else
  30.      false
  31.       end
  32.    end
  33. end
  34.  
  35. declare
  36. fun {GetNode BT Word}
  37.    case BT
  38.    of node(W#T L R) then
  39.       if W==Word then T
  40.       elseif {LessStr W Word} then {GetNode R Word}
  41.       else {GetNode L Word}
  42.       end
  43.    else raise 'brak podanego slowa' end
  44.    end
  45. end
  46.  
  47. %{Inspect {GetNode BST "mysz"}}
  48.  
  49. declare
  50. fun {AddNode BT Word#Translate}
  51.    if BT == empty then node(Word#Translate empty empty) else
  52.    local node(W#T L R) = BT in
  53.       if Word == W then node(Word#Translate L R)
  54.       elseif {LessStr W Word} then node(W#T L (if R==empty then node(Word#Translate empty empty)
  55.                      else {AddNode R Word#Translate} end)
  56.                  )
  57.       else node(W#T (if L==empty then node(Word#Translate empty empty)
  58.              else {AddNode L Word#Translate} end) R)
  59.       end
  60.    end
  61.    end
  62. end
  63.  
  64. %{Inspect {AddNode BST "aaaaa"#"aaaaa"}}
  65.  
  66. declare fun {Preorder T}
  67.            local fun {Preord T#Labels}
  68.                     case T#Labels
  69.                     of empty#Labels then Labels
  70.                     [] node(V T1 T2)#Labels then
  71.                        V|{Preord T1#{Preord T2#Labels}}
  72.                     end
  73.                  end
  74.            in {Preord T#nil}
  75.            end
  76.     end
  77.  
  78. %{Inspect {Preorder BST}}
  79.  
  80. declare
  81. fun {ListToTree L}
  82.    local fun {ToTree LI BT}
  83.         case LI
  84.         of H|T then {ToTree T {AddNode BT H.2#H.1}}
  85.         [] nil then BT
  86.         end
  87.      end
  88.    in {ToTree L empty}
  89.    end
  90. end
  91.  
  92. declare LISTA = {Preorder BST}
  93.  
  94. {Inspect {ListToTree LISTA}}
Add Comment
Please, Sign In to add comment