Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Haskell/treeDepth.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
data Tree t = Node t (Tree t) (Tree t) | Nilt deriving (Read, Show)

depth :: Tree t -> Int
depth Nilt = 0
depth (Node x tl tr) = max depthLeft depthRight
where depthLeft = (depth tl) + 1
depthRight = (depth tr) + 1
14 changes: 14 additions & 0 deletions Haskell/treeInsert.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
data Tree t = Node t (Tree t) (Tree t) | Nilt deriving (Read, Show)

insert :: Ord t => Tree t -> t -> Tree t
insert Nilt x = Node x Nilt Nilt
insert (Node x treeL treeR) num = ins num x
where ins num x
| num < x = (Node x (insert treeL num) treeR)
| otherwise = (Node x treeL (insert treeR num))

-- Receive a tree and a list of numbers and insert them into the tree
insertList :: Ord t => Tree t -> [t] -> Tree t
insertList tree (x:[]) = insert tree x
insertList tree (x:xs) = insertList t xs
where t = insert tree x