Guest User

Untitled

a guest
Apr 14th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Oz 1.53 KB | None | 0 0
  1. %Dict.oz
  2.  
  3. %<BT T> ::= leaf | node(T <BT T> <BT T>)
  4.  
  5. functor
  6. import
  7.    Key
  8. export
  9.    create: Create
  10.    insert: Insert
  11.    lookup: Lookup
  12.    delete: Delete
  13. define
  14.    fun {Create} leaf end
  15.    fun {Insert Tree Klucz#Value}
  16.       case Tree
  17.       of leaf then node(Klucz#Value leaf leaf)
  18.       [] node(K#V L R) then
  19.      case {Key.compare Klucz K}
  20.      of ~1 then node(K#V {Insert L Klucz#Value} R)
  21.      [] 0 then raise duplicatedKey(Klucz) end
  22.      [] 1 then node(K#V L {Insert R Klucz#Value})
  23.      end
  24.       end
  25.    end
  26.    fun {Lookup Tree Klucz}
  27.       case Tree
  28.       of leaf then raise notFound(Klucz) end
  29.       [] node(K#V L R) then
  30.      case {Key.compare Klucz K}
  31.      of ~1 then {Lookup L Klucz}
  32.      [] 0 then V
  33.      [] 1 then {Lookup R Klucz}
  34.      end
  35.       end
  36.    end
  37.    fun {DeleteMin Tree}
  38.       case Tree
  39.       of node(K#V leaf R) then K#V#R
  40.       [] node(K#V L R) then local Key#Value#Left = {DeleteMin L}
  41.                 in Key#Value#node(K#V Left R) end
  42.       end
  43.    end
  44.    fun {Delete Tree Klucz}
  45.       case Tree
  46.       of leaf then leaf
  47.       [] node(K#V L R) then
  48.      case {Key.compare Klucz K}
  49.      of ~1 then node(K#V {Delete L Key} R)
  50.      [] 0 then
  51.         case L#R
  52.         of leaf#R then R
  53.         [] L#leaf then L
  54.         [] _ then local Ki#Vi#T_right = {DeleteMin R}
  55.               in node(Ki#Vi L T_right) end
  56.         end
  57.      [] 1 then node(K#V L {Delete R Key})
  58.      end
  59.       end
  60.    end
  61. end
  62.  
  63.  
  64.  
  65.  
  66.  
  67. %Key.oz
  68.  
  69. functor
  70. export compare:Compare
  71. define
  72.    fun {Compare X Y}
  73.       if X<Y then ~1
  74.       elseif X==Y then 0
  75.       else 1
  76.       end
  77.    end
  78. end
Add Comment
Please, Sign In to add comment