Browse Source

Figure 2.9. Implementation of binary search trees

master
Aaron VonderHaar 5 years ago
parent
commit
8122849351
  1. 52
      figures/2.9 Implementation of binary search trees/UnbalancedSet.elm
  2. 14
      figures/2.9 Implementation of binary search trees/elm-package.json

52
figures/2.9 Implementation of binary search trees/UnbalancedSet.elm

@ -0,0 +1,52 @@
module UnbalancedSet exposing (..)
type alias Ordered t =
{ eq : t -> t -> Bool
, lt : t -> t -> Bool
, leq : t -> t -> Bool
}
type Tree t
= E
| T (Tree t) t (Tree t)
type alias Set =
Tree
empty : Tree x
empty =
E
member : Ordered x -> x -> Tree x -> Bool
member compare x tree =
case tree of
E ->
False
T a y b ->
if compare.lt x y then
member compare x a
else if compare.lt y x then
member compare x b
else
True
insert : Ordered x -> x -> Tree x -> Tree x
insert compare x tree =
case tree of
E ->
T E x E
T a y b ->
if compare.lt x y then
T (insert compare x a) y b
else if compare.lt y x then
T a y (insert compare x b)
else
tree

14
figures/2.9 Implementation of binary search trees/elm-package.json

@ -0,0 +1,14 @@
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "4.0.1 <= v < 5.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
Loading…
Cancel
Save