diff --git a/src/BinaryTree/BinarySearchTree.hs b/src/BinaryTree/BinarySearchTree.hs index f955921..ec99c25 100644 --- a/src/BinaryTree/BinarySearchTree.hs +++ b/src/BinaryTree/BinarySearchTree.hs @@ -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 @@ -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) \ No newline at end of file +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 diff --git a/src/BinaryTree/BinaryTree.hs b/src/BinaryTree/BinaryTree.hs index f703217..86372c8 100644 --- a/src/BinaryTree/BinaryTree.hs +++ b/src/BinaryTree/BinaryTree.hs @@ -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 @@ -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) \ No newline at end of file +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 \ No newline at end of file