Download code

Jump to: navigation, search

Back to Binary_search_tree_(Standard_ML)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

bst.sml

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Binary_search_tree_(Standard_ML)?oldid=18621
14 *)
15 
16 datatype 'a tree = Node of {data:'a, left:'a tree, right:'a tree}
17                  | Leaf
18 
19 fun search(tree : 'a tree, compare, data : 'a) = 
20     let fun s(Leaf) = NONE
21           | s(Node{data=nodedata,left=left,right=right}) = 
22                 (case compare(data, nodedata) of
23                       LESS    => s(left)
24                     | GREATER => s(right)
25                     | EQUAL   => SOME nodedata)
26     in
27         s(tree)
28     end
29 fun insert(tree : 'a tree, compare, data : 'a) = 
30     let fun i(Leaf) = Node{data=data, left=Leaf, right=Leaf}
31           | i(Node{data=nodedata,left=left,right=right}) = 
32                 (case compare(data, nodedata) of
33                       LESS    => Node{data=nodedata, left=i(left), right=right}
34                     | GREATER => Node{data=nodedata, left=left, right=i(right)}
35                     | EQUAL   => Node{data=nodedata, left=left, right=right})
36     in
37         i(tree)
38     end
39 fun delete(tree : 'a tree, compare, data : 'a) = 
40     let fun valueMax(Node{data=nodedata,right=Leaf,...}) = nodedata
41 	  | valueMax(Node{right=right,...})              = valueMax(right)
42 
43 	fun deleteMax(node as Node{data=nodedata,left=left,right=Leaf}) =
44 	        deleteNode(node)
45 	  | deleteMax(Node{data=nodedata,left=left,right=right}) =
46 	        Node{data=nodedata,left=left,right=deleteMax(right)}
47         and deleteNode(Node{data=nodedata,left=Leaf,right=Leaf})  = Leaf
48 	  | deleteNode(Node{data=nodedata,left=Leaf,right=right}) = right
49 	  | deleteNode(Node{data=nodedata,left=left,right=Leaf})  = left
50 	  | deleteNode(Node{data=nodedata,left=left,right=right}) =
51 	        Node{data=valueMax(left), left=deleteMax(left), right=right}
52         fun d(Leaf) = Leaf (* data was not found *)
53           | d(node as Node{data=nodedata,left=left,right=right}) = 
54                 (case compare(data, nodedata) of
55                       LESS    => Node{data=nodedata,left=d(left),right=right}
56                     | GREATER => Node{data=nodedata,left=left,right=d(right)}
57                     | EQUAL   => deleteNode(node))
58     in
59         d(tree)
60     end


hijacker
hijacker
hijacker
hijacker