Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple tree functions #39

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
20 changes: 19 additions & 1 deletion src/BinaryTree/BinarySearchTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ inorderWalk :: (Eq a, Ord a) => BTree a -> [a]
inorderWalk Empty = []
inorderWalk (Node x l r) = (inorderWalk l) ++ [x] ++ (inorderWalk r)

-- Perform preorder walk of the binary search tree
preorderWalk :: (Eq a, Ord a) => BTree a -> [a]
preorderWalk Empty = []
preorderWalk (Node x l r) = [x] ++ preorderWalk l ++ preorderWalk r

-- Perform postorder walk of the binary search tree
postorderWalk :: (Eq a, Ord a) => BTree a -> [a]
postorderWalk Empty = []
postorderWalk (Node x l r) = preorderWalk l ++ preorderWalk r ++ [x]

-- Function to insert a value into the tree. Returns the new tree.
-- Cormen, Thomas H., et al. Introduction to algorithms. pg. 294, MIT press, 2009.
bstInsert :: (Eq a, Ord a) => BTree a -> a -> BTree a
Expand Down Expand Up @@ -54,4 +64,12 @@ isBST Empty = True
isBST (Node x Empty Empty) = True
isBST (Node x Empty r) = (x < (nkey r)) && (isBST r) where nkey = (\(Node n ll rr) -> n)
isBST (Node x l Empty) = (x >= (nkey l)) && (isBST l) where nkey = (\(Node n ll rr) -> n)
isBST (Node x l r) = (x >= (nkey l)) && (x < (nkey r)) && (isBST l) && (isBST r) where nkey = (\(Node n ll rr) -> n)
isBST (Node x l r) = (x >= (nkey l)) && (x < (nkey r)) && (isBST l) && (isBST r) where nkey = (\(Node n ll rr) -> n)

isElementInBST :: (Ord a, Eq a) => BTree a -> a -> Bool
isElementInBST Empty _ = False
isElementInBST (Node x l r) ele =
x == ele ||
if x < ele
then isElementInBST r ele
else isElementInBST l ele
30 changes: 28 additions & 2 deletions src/BinaryTree/BinaryTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module BinaryTree.BinaryTree where

import qualified Data.List as L

data BTree a = Empty | Node a (BTree a) (BTree a) deriving (Show)
data BTree a = Empty | Node a (BTree a) (BTree a) deriving (Show, Eq)
data Side = LeftSide | RightSide deriving (Eq, Show)

-- Get subtree on specified side
Expand Down Expand Up @@ -68,4 +68,30 @@ numNodes t = length $ bfsList t
-- Pretty Print a Binary Tree
simplePrint :: (Show a) => BTree a -> String
simplePrint Empty = ""
simplePrint t = (nodeShow t) ++ " " ++ (simplePrint $ getLeftTree t) ++ (simplePrint $ getRightTree t)
simplePrint t = (nodeShow t) ++ " " ++ (simplePrint $ getLeftTree t) ++ (simplePrint $ getRightTree t)

-- Find count of element occurrence in binary tree
elementCountInTree :: (Ord a, Eq a) => BTree a -> a -> Int
elementCountInTree Empty _ = 0
elementCountInTree (Node x l r) ele =
(if ele == x then 1 else 0) +
elementCountInTree l ele +
elementCountInTree r ele

-- Find whether tree is symmetric at root
-- Uses Haskell Eq instance to compare BTree datatype
isSymmetric :: (Eq a) => BTree a -> Bool
isSymmetric Empty = True
isSymmetric (Node _ l r) = l == r

-- Get sum of all elements in tree
sumTree :: BTree Int -> Int
sumTree Empty = 0
sumTree (Node x l r) = x + sumTree l + sumTree r

-- Get an array of leaf nodes in a binary tree
getLeafNodes :: (Eq a, Show a) => BTree a -> [a]
getLeafNodes Empty = []
getLeafNodes (Node x l r)
| l == Empty && r == Empty = [x]
| otherwise = getLeafNodes l ++ getLeafNodes r