|
| 1 | +namespace Test |
| 2 | + |
| 3 | +module Test = |
| 4 | + /// finds min element in list if possible |
| 5 | + /// <param name="ls">list in which we trying to find min element</param> |
| 6 | + /// <returns>min element</returns> |
| 7 | + let findMinElement ls = |
| 8 | + match ls with |
| 9 | + | h :: tail -> Some <| List.fold (fun acc x -> if x < acc then x else acc) h tail |
| 10 | + | _ -> None |
| 11 | + |
| 12 | + /// represents BinaryTree |
| 13 | + type BinaryTree<'a> = |
| 14 | + | Node of 'a * BinaryTree<'a> * BinaryTree<'a> |
| 15 | + | Empty |
| 16 | + |
| 17 | + /// represents Continuation steps with tree on currentStep and intermediate int calculation |
| 18 | + type ContinuationStep<'a> = |
| 19 | + | Finished |
| 20 | + | Step of BinaryTree<'a> * int * (unit -> ContinuationStep<'a>) |
| 21 | + |
| 22 | + /// linearizes binary tree traversal using steps of ContinuationStep |
| 23 | + /// <param name="binTree">tree to find traversal</param> |
| 24 | + /// <param name="currDistance">distance from root to current Node to store in step</param> |
| 25 | + /// <param name="currDistance">continuation function</param> |
| 26 | + /// <returns>step</returns> |
| 27 | + let rec linearize binTree currDistance cont = |
| 28 | + match binTree with |
| 29 | + | Empty -> cont () |
| 30 | + | Node(_, l, r) -> |
| 31 | + Step( |
| 32 | + binTree, |
| 33 | + currDistance, |
| 34 | + (fun () -> linearize l (currDistance + 1) (fun () -> linearize r (currDistance + 1) cont)) |
| 35 | + ) |
| 36 | + |
| 37 | + /// finds if possible min distance from node to root in tree |
| 38 | + /// <param name="binTree">tree where we want to find min distance</param> |
| 39 | + /// <returns>min distance</returns> |
| 40 | + let findMinDistance binTree = |
| 41 | + if binTree = Empty then |
| 42 | + None |
| 43 | + else |
| 44 | + let steps = linearize binTree 0 (fun () -> Finished) |
| 45 | + |
| 46 | + let rec processSteps step distanceList = |
| 47 | + match step with |
| 48 | + | Finished -> distanceList |
| 49 | + | Step(x, currDistance, getNext) -> |
| 50 | + match x with |
| 51 | + | Node(_, Empty, Empty) -> processSteps (getNext ()) (currDistance :: distanceList) |
| 52 | + | _ -> processSteps (getNext ()) distanceList |
| 53 | + |
| 54 | + findMinElement (processSteps steps []) |
| 55 | + |
| 56 | + /// represents hash-table |
| 57 | + type HashTable<'a when 'a: equality>(size: int, hashFunction: 'a -> int) = |
| 58 | + let mutable table = Array.init size (fun _ -> []) |
| 59 | + |
| 60 | + let rec removeElement element list = |
| 61 | + match list with |
| 62 | + | [] -> [] |
| 63 | + | head :: tail when head = element -> tail |
| 64 | + | head :: tail -> head :: (removeElement element tail) |
| 65 | + |
| 66 | + let getHashIndex element = |
| 67 | + let hash = hashFunction element |
| 68 | + hash % size |
| 69 | + |
| 70 | + /// adds element to hash-table by hash index |
| 71 | + /// <param name="element">element</param> |
| 72 | + member this.Add(element) = |
| 73 | + let index = getHashIndex element |
| 74 | + table.[index] <- element :: table.[index] |
| 75 | + |
| 76 | + /// checks if element is in hash-table |
| 77 | + /// <param name="element">element</param> |
| 78 | + /// <returns>true if element in hash-table false otherwise</returns> |
| 79 | + member this.Contains(element) = |
| 80 | + let index = getHashIndex element |
| 81 | + List.contains element table.[index] |
| 82 | + |
| 83 | + /// removes element from hash-table |
| 84 | + /// <param name="element">element</param> |
| 85 | + /// <returns>true if element was in hash-table (was removed) false otherwise</returns> |
| 86 | + member this.Remove(element) = |
| 87 | + let index = getHashIndex element |
| 88 | + |
| 89 | + match this.Contains element with |
| 90 | + | false -> false |
| 91 | + | _ -> |
| 92 | + table.[index] <- removeElement element table.[index] |
| 93 | + true |
0 commit comments