diff --git a/src/BinaryTree/BinarySearchTree.hs b/src/BinaryTree/BinarySearchTree.hs index f955921..1fd91ad 100644 --- a/src/BinaryTree/BinarySearchTree.hs +++ b/src/BinaryTree/BinarySearchTree.hs @@ -10,9 +10,17 @@ nodeKey (Node x _ _) = Just x -- Perform inorder walk of the binary search tree. -- Cormen, Thomas H., et al. Introduction to algorithms. pg. 288, MIT press, 2009. -inorderWalk :: (Eq a, Ord a) => BTree a -> [a] -inorderWalk Empty = [] -inorderWalk (Node x l r) = (inorderWalk l) ++ [x] ++ (inorderWalk r) +inorder :: (Eq a, Ord a) => BTree a -> [a] +inorder Empty = [] +inorder (Node x l r) = inorder l ++ [x] ++ inorder r + +preorder :: BTree a -> [a] +preorder Empty = [] +preorder (Node a left right) = a : preorder left ++ preorder right + +postorder :: BTree a -> [a] +postorder Empty = [] +postorder (Node a left right) = postorder left ++ postorder right ++ [a] -- 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. diff --git a/src/BinaryTree/BinaryTree.hs b/src/BinaryTree/BinaryTree.hs index f703217..6879af7 100644 --- a/src/BinaryTree/BinaryTree.hs +++ b/src/BinaryTree/BinaryTree.hs @@ -68,4 +68,17 @@ 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) + +foldTree :: (a -> b -> b) -> b -> BTree a -> b +foldTree f acc Empty = acc +foldTree f acc (Node a left right) = foldTree f foldedRightSubtree left where + result = f a acc + foldedRightSubtree = foldTree f result right + +mapTree :: (a -> b) -> BTree a -> BTree b +mapTree _ Empty = Empty +mapTree f (Node a left right) = Node (f a) left' right' where + left' = mapTree f left + right' = mapTree f right + \ No newline at end of file