Difference between revisions of "Binary Search Tree Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 23: Line 23:
  
 
=Code to Implement=
 
=Code to Implement=
{{SMLToImplement|binary_tree|insert<br/>remove<br/>find<br/>...|binary_tree}}
+
{{SMLToImplement|binary_tree|create_empty_tree<br/>insert<br/>remove<br/>find<br/>fold_order_hof<br/>to_string|binary_tree}}
  
 
  <nowiki>signature BINARY_SEARCH_TREE = sig
 
  <nowiki>signature BINARY_SEARCH_TREE = sig
Line 44: Line 44:
 
end</nowiki>
 
end</nowiki>
  
 +
<nowiki>(* TODO: replace unit with the type you decide upon *)
 +
type ('a,'k) tree = unit</nowiki>
 
==create_empty_tree==
 
==create_empty_tree==
 
  <nowiki>fun create_empty_tree(cmp : 'k compare_function, to_key : ('a,'k) to_key_function) : ('a,'k) tree =
 
  <nowiki>fun create_empty_tree(cmp : 'k compare_function, to_key : ('a,'k) to_key_function) : ('a,'k) tree =
Line 53: Line 55:
  
 
==remove==
 
==remove==
 +
<nowiki>fun remove(t:('a,'k) tree, item_key:'k) : (('a,'k) tree * 'a option) =
 +
    raise NotYetImplemented</nowiki>
 +
 
Remove contains the most challenging aspect of this studio.  When instructed to remove a node from a tree, there are several cases:
 
Remove contains the most challenging aspect of this studio.  When instructed to remove a node from a tree, there are several cases:
  
Line 76: Line 81:
 
[https://en.wikipedia.org/wiki/Binary_search_tree#Deletion Wikipedia BST Deletion]
 
[https://en.wikipedia.org/wiki/Binary_search_tree#Deletion Wikipedia BST Deletion]
  
fun remove (equal_less_greater_function:'a -> order) (t:'a tree) : 'a tree =
 
    raise NotYetImplemented
 
  
 
==find==
 
==find==
 
reference: [http://sml-family.org/Basis/list.html#SIG:LIST.find:VAL List.find]
 
reference: [http://sml-family.org/Basis/list.html#SIG:LIST.find:VAL List.find]
  
  fun find (equal_less_greater_function : 'a -> order) (t:'a tree) : 'a option =  
+
  <nowiki>fun find(t:('a,'k) tree, item_key:'k) : 'a option =  
    raise NotYetImplemented
+
    raise NotYetImplemented</nowiki>
  
 
==fold_order_hof==
 
==fold_order_hof==
Line 89: Line 92:
  
 
{{CurriedFunction}}
 
{{CurriedFunction}}
  fun fold_order_hof (order:traversal_order) (f : 'a * 'b -> 'b) (init : 'b) (t : 'a tree) : 'b =  
+
  <nowiki>fun fold_order_hof (order:traversal_order) (f : 'a * 'b -> 'b) (init : 'b) (t : 'a tree) : 'b =  
 
     raise NotYetImplemented
 
     raise NotYetImplemented
  
Line 95: Line 98:
 
     fold_order_hof LNR f init t
 
     fold_order_hof LNR f init t
 
  fun fold_rnl f init t =  
 
  fun fold_rnl f init t =  
     fold_order_hof RNL f init t
+
     fold_order_hof RNL f init t</nowiki>
  
 
=Testing=
 
=Testing=

Revision as of 18:03, 15 October 2020

We will implement a binary search tree data structure as well as a few Higher Order Function Hall of Fame inductees.

Background

order

SML's General structure's order

datatype order = LESS | EQUAL | GREATER

Values of type order are used when comparing elements of a type that has a linear ordering.

Functions which take (('k * 'k) -> order) functions behave as Int.compare does:

compare (i, j)
returns LESS, EQUAL, or GREATER when i is less than, equal to, or greater than j, respectively.

list

We will implement some of the higher ordered functions list provides on our binary tree.

traversal order

LNR would produce: ABCDEFGHI
RNL would produce: IHGFEDCBA

In-order LNR

Reverse in-order RNL

 datatype traversal_order = LNR | RNL

Code to Implement

file: src/main/sml/binary_tree/binary_tree.sml Smlnj-logo.png
functions: create_empty_tree
insert
remove
find
fold_order_hof
to_string
signature BINARY_SEARCH_TREE = sig
    type 'k compare_function = (('k * 'k) -> order)
    type ('a,'k) to_key_function = 'a -> 'k

    type ('a,'k) tree;

    val create_empty_tree : ('k compare_function * ('a,'k) to_key_function) -> ('a,'k) tree
    val create_empty_simple_tree : ('a compare_function) -> ('a,'a) tree

    val insert : (('a,'k) tree * 'a) -> (('a,'k) tree * 'a option)
    val remove : (('a,'k) tree * 'k) -> (('a,'k) tree * 'a option)
    val find : (('a,'k) tree * 'k) -> 'a option

    val fold_lnr : (('a * 'b) -> 'b) -> ('b) -> (('a,'k) tree) -> 'b 
    val fold_rnl : (('a * 'b) -> 'b) -> ('b) -> (('a,'k) tree) -> 'b 
    
    val to_string : ('a -> string) -> (('a,'k) tree) -> string
end
(* TODO: replace unit with the type you decide upon *)
type ('a,'k) tree = unit

create_empty_tree

fun create_empty_tree(cmp : 'k compare_function, to_key : ('a,'k) to_key_function) : ('a,'k) tree =
    raise NotYetImplemented

insert

fun insert(t:('a,'k) tree, item:'a) : (('a,'k) tree * 'a option) =
    raise NotYetImplemented

remove

fun remove(t:('a,'k) tree, item_key:'k) : (('a,'k) tree * 'a option) =
    raise NotYetImplemented

Remove contains the most challenging aspect of this studio. When instructed to remove a node from a tree, there are several cases:

not found

What will indicate that you reached the point where you know the node is not found in the tree?
note: this has a trivial solution.

no child in the left tree

How will you detect this pattern?
note: this has a trivial solution.

no child in the right tree

how will you detect this pattern?
note: this has a trivial solution.

no children are present

If you need to remove a node and it has both children, now you have a legit problem. You must maintain a correct binary search tree.

A common approach is to choose one of the following:

  • remove the right most descendant in the left child, and promote it to be the node at the current level, or
  • remove the left most descendant in right left child, and promote it to be the node at the current level

The image below shows finding the left most child in the right subtree for promotion:

AVL-tree-delete.svg

Building a helper function will likely be helpful.

Wikipedia BST Deletion


find

reference: List.find

fun find(t:('a,'k) tree, item_key:'k) : 'a option = 
    raise NotYetImplemented

fold_order_hof

reference: List.foldl foldr

note: this function is curried.

fun fold_order_hof (order:traversal_order) (f : 'a * 'b -> 'b) (init : 'b) (t : 'a tree) : 'b = 
     raise NotYetImplemented

 fun fold_lnr f init t = 
    fold_order_hof LNR f init t
 fun fold_rnl f init t = 
    fold_order_hof RNL f init t

Testing

file: unit_test_binary_search_tree.sml
source folder: src/test/sml/binary_search_tree

Pledge, Acknowledgments, Citations

file: studio-binary-search-tree-pledge-acknowledgments-citations.txt

More info about the Honor Pledge