Skip to content

Instantly share code, notes, and snippets.

@risingBirdSong
Created October 2, 2020 00:54
Show Gist options
  • Save risingBirdSong/fe24ecd3ea5702ecf01ed0ecf1515671 to your computer and use it in GitHub Desktop.
Save risingBirdSong/fe24ecd3ea5702ecf01ed0ecf1515671 to your computer and use it in GitHub Desktop.
--linear search
findNumSorted srch (x:xs)
| srch == x = Just x
| x > srch = Nothing
| otherwise = findNumSorted srch xs
--make a binary tree
data MyTree a = EmptyTree | Node a (MyTree a) (MyTree a) deriving (Show, Read, Eq)
singleton x = Node x EmptyTree EmptyTree
insertVal v EmptyTree = singleton v
insertVal v (Node x lefty righty)
| v == x = Node x lefty righty
| v < x = Node x (insertVal v lefty) righty
| v > x = Node x lefty (insertVal v righty)
testTree = foldr insertVal EmptyTree [9,1,8,2,7,3,6,4,5]
-- Node 5 (Node 4 (Node 3 (Node 2 (Node 1 EmptyTree EmptyTree) EmptyTree) EmptyTree) EmptyTree) (Node 6 EmptyTree (Node 7 EmptyTree (Node 8 EmptyTree (Node 9 EmptyTree EmptyTree))))
findValue _ EmptyTree = Nothing
findValue srch (Node x left_b right_b)
| srch == x = Just x
| srch < x = findValue srch left_b
| srch > x = findValue srch right_b
-- findValue 3 testTree Just 3
-- findValue 9 testTree Just 9
-- findValue (-100) testTree Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment