Last active
February 23, 2022 20:13
-
-
Save evgenii-malov/eff4c42125f7ce7c4e4a456be60f29da to your computer and use it in GitHub Desktop.
create balanced tree from sorted list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- GHCi, version 8.8.4 | |
| {-# LANGUAGE ScopedTypeVariables #-} | |
| import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=379s | |
| import Data.List | |
| import Data.Maybe | |
| -- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show | |
| -- build balanced binary search tree from sorted list | |
| -- assume list is sorted | |
| -- nodes must be uniq | |
| -- https://www.youtube.com/watch?v=icRN-f408oE | |
| b :: [a] -> Btree a | |
| b [] = Empty | |
| b l = Node m (b l_) (b r_) where | |
| m = l !! mi | |
| (l_,r) = splitAt mi l | |
| r_ = tail r | |
| mi = (length l) `div` 2 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=icRN-f408oE