How to resolve the algorithm Same fringe step by step in the Haskell programming language
How to resolve the algorithm Same fringe step by step in the Haskell programming language
Table of Contents
Problem Statement
Write a routine that will compare the leaves ("fringe") of two binary trees to determine whether they are the same list of leaves when visited left-to-right. The structure or balance of the trees does not matter; only the number, order, and value of the leaves is important. Any solution is allowed here, but many computer scientists will consider it inelegant to collect either fringe in its entirety before starting to collect the other one. In fact, this problem is usually proposed in various forums as a way to show off various forms of concurrency (tree-rotation algorithms have also been used to get around the need to collect one tree first). Thinking of it a slightly different way, an elegant solution is one that can perform the minimum amount of work to falsify the equivalence of the fringes when they differ somewhere in the middle, short-circuiting the unnecessary additional traversals and comparisons. Any representation of a binary tree is allowed, as long as the nodes are orderable, and only downward links are used (for example, you may not use parent or sibling pointers to avoid recursion).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Same fringe step by step in the Haskell programming language
Data Type Definition:
Tree a
is a polymorphic data type representing a binary tree with values of typea
.Leaf a
: Represents a leaf node containing a value of typea
.Node (Tree a) (Tree a)
: Represents an internal node with links to two child trees of typeTree a
.
Function Definitions:
-
fringe :: Tree a -> [a]
:- Extracts the values from the leaf nodes of a binary tree
t
, returning a list ofa
values. - For leaf nodes, it simply returns the single value in the leaf.
- For internal nodes, it recursively combines the fringe values of the left and right subtrees.
- Extracts the values from the leaf nodes of a binary tree
-
sameFringe :: (Eq a) => Tree a -> Tree a -> Bool
:- Compares the fringe values of two binary trees
t1
andt2
. - Returns
True
if the fringe values are the same (as lists ofa
values), otherwiseFalse
.
- Compares the fringe values of two binary trees
Main Function:
main :: IO ()
:- Defines several binary trees with different structures and values.
- Uses
mapM_ print
to print the result of comparing the fringe values of the first treea
with each of the other trees[a, b, c, x, y, z]
.
Example:
a
is a binary tree with nodes 1, 2, 3, 4, and 5.sameFringe a a
will returnTrue
because both trees have the same fringe values [1, 5].sameFringe a b
will returnFalse
because the fringe values ofb
are different: [1, 5] vs. [1, 3].
Source code in the haskell programming language
data Tree a
= Leaf a
| Node (Tree a)
(Tree a)
deriving (Show, Eq)
fringe :: Tree a -> [a]
fringe (Leaf x) = [x]
fringe (Node n1 n2) = fringe n1 ++ fringe n2
sameFringe
:: (Eq a)
=> Tree a -> Tree a -> Bool
sameFringe t1 t2 = fringe t1 == fringe t2
main :: IO ()
main = do
let a = Node (Leaf 1) (Node (Leaf 2) (Node (Leaf 3) (Node (Leaf 4) (Leaf 5))))
b = Node (Leaf 1) (Node (Node (Leaf 2) (Leaf 3)) (Node (Leaf 4) (Leaf 5)))
c = Node (Node (Node (Node (Leaf 1) (Leaf 2)) (Leaf 3)) (Leaf 4)) (Leaf 5)
x =
Node
(Leaf 1)
(Node
(Leaf 2)
(Node (Leaf 3) (Node (Leaf 4) (Node (Leaf 5) (Leaf 6)))))
y = Node (Leaf 0) (Node (Node (Leaf 2) (Leaf 3)) (Node (Leaf 4) (Leaf 5)))
z = Node (Leaf 1) (Node (Leaf 2) (Node (Node (Leaf 4) (Leaf 3)) (Leaf 5)))
mapM_ print $ sameFringe a <$> [a, b, c, x, y, z]
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Execute a Markov algorithm step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Pony programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the FOCAL programming language