How to resolve the algorithm Tree traversal step by step in the Miranda programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Tree traversal step by step in the Miranda programming language
Table of Contents
Problem Statement
Implement a binary tree where each node carries an integer, and implement:
Use those traversals to output the following tree: The correct output should look like this:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Tree traversal step by step in the Miranda programming language
Source code in the miranda programming language
main :: [sys_message]
main = [Stdout (lay [show (f example)
| f <- [preorder,inorder,postorder,levelorder]])]
example :: tree num
example = Node 1 (Node 2 (Node 4 (leaf 7) Nilt)
(leaf 5))
(Node 3 (Node 6 (leaf 8) (leaf 9)) Nilt)
tree * ::= Nilt | Node * (tree *) (tree *)
leaf :: *->tree *
leaf k = Node k Nilt Nilt
preorder :: tree *->[*]
preorder Nilt = []
preorder (Node v l r) = v : preorder l ++ preorder r
inorder :: tree *->[*]
inorder Nilt = []
inorder (Node v l r) = inorder l ++ v : inorder r
postorder :: tree *->[*]
postorder Nilt = []
postorder (Node v l r) = postorder l ++ postorder r ++ [v]
levelorder :: tree *->[*]
levelorder t = f [t]
where f [] = []
f (Nilt:xs) = f xs
f (Node v l r:xs) = v : f (xs++[l,r])
You may also check:How to resolve the algorithm Levenshtein distance step by step in the C programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the Wren programming language
You may also check:How to resolve the algorithm Mertens function step by step in the Prolog programming language