From e22c6e9463cbccc77da3b54ec311757fddb96e41 Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Mon, 4 Oct 2021 22:02:38 +0530 Subject: [PATCH 1/4] Added simple functions to BinaryTree Structure --- src/BinaryTree/BinaryTree.hs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/BinaryTree/BinaryTree.hs b/src/BinaryTree/BinaryTree.hs index f703217..c84c662 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,28 @@ 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 (Node _ l r) = l == r + +-- Get sum of all elements in tree +sumTree :: (Ord a, Eq a) => BTree a -> Int +sumTree (Node x l r) = x + sumTree l + sumTree r + +-- Get an array of leaf nodes in a binary tree +getLeafNodes :: (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 From 0084a0913010bab816ed3335cc45e126512d769e Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Mon, 4 Oct 2021 22:02:58 +0530 Subject: [PATCH 2/4] Added simple functions to BinarySearchTree Structure --- src/BinaryTree/BinarySearchTree.hs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 From abc697f5beda27e860ca75d2deb106e42a5bc50d Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Mon, 4 Oct 2021 22:40:17 +0530 Subject: [PATCH 3/4] Minor fix --- src/BinaryTree/BinaryTree.hs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/BinaryTree/BinaryTree.hs b/src/BinaryTree/BinaryTree.hs index c84c662..6ab6707 100644 --- a/src/BinaryTree/BinaryTree.hs +++ b/src/BinaryTree/BinaryTree.hs @@ -84,11 +84,12 @@ isSymmetric :: (Eq a) => BTree a -> Bool isSymmetric (Node _ l r) = l == r -- Get sum of all elements in tree -sumTree :: (Ord a, Eq a) => BTree a -> Int +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 :: (Show a) => BTree a -> [a] +getLeafNodes :: (Eq a, Show a) => BTree a -> [a] getLeafNodes Empty = [] getLeafNodes (Node x l r) | l == Empty && r == Empty = [x] From d9aa5f29e4e45d4766e22e7eeb6552adb1c92897 Mon Sep 17 00:00:00 2001 From: Dhananjay Date: Mon, 4 Oct 2021 22:45:28 +0530 Subject: [PATCH 4/4] Minor fix --- src/BinaryTree/BinaryTree.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/BinaryTree/BinaryTree.hs b/src/BinaryTree/BinaryTree.hs index 6ab6707..86372c8 100644 --- a/src/BinaryTree/BinaryTree.hs +++ b/src/BinaryTree/BinaryTree.hs @@ -81,6 +81,7 @@ elementCountInTree (Node x l 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