Question

In: Computer Science

A binary tree data type is defined in OCaml as follows: type 'a binary_tree = |...

A binary tree data type is defined in OCaml as follows:

type 'a binary_tree =
| Empty
| Node of 'a * 'a binary_tree * 'a binary_tree;;

The mirror of a binary tree is defined as the tree obtained by reversing its left and right subtrees at each level. Write an OCaml function

is_mirror: 'a binary_tree -> 'a binary_tree -> bool = <fun>

to determine if one tree is the mirror of another.

Your function must take into account the values in the nodes as well.

Solutions

Expert Solution

Here is the complete OCaml code for the given task. I have added comments for a better understanding of the same.


let rec is_mirror a b = match (a,b) with
| (Empty,Empty) -> true (* Both are Empty then mirror*)
| (Node(x1,y1,z1), Node(x2,y2,z2)) -> if(x1 = x2 && (is_mirror y1 z2) && (is_mirror y2 z1)) then true else false (* Left must be mirror of right and right must be mirror of left*)
| (_, _) -> false;; (*Otherwise False*)

You can comment below the answer in case of any doubts and I will be happy to help.

Please give a thumbs up if the answer could be of help!

All the best!


Related Solutions

Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels...
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2^(height+1) - 1.) /** Returns true if the tree is a perfect binary tree */ public boolean isPerfectBST() Class Name: Exercise25_03
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
Write a method for binary tree in Python that can determine whether a binary tree is...
Write a method for binary tree in Python that can determine whether a binary tree is a binary search tree or not. The input should be a binary tree. The output should be true or false. True= binary tree meets the criteria to be a binary search tree. False= does not meet the criteria to be a binary search tree.
    For questions 2 – 8:   There are three projects.  Binary variablesX1, X2, and X3are defined as follows:...
    For questions 2 – 8:   There are three projects.  Binary variablesX1, X2, and X3are defined as follows: Xi=     1 if project i is selected, and Xi=     0 if project i is not selected,           for i = 1, 2, 3. 2. Write a constraint to represent: “At least one of the three projects must be selected”. 3. Write a constraint to represent: “Between project 1 and project 2, exactly one is selected”. 4. Write a constraint to represent: “At most two projects of the three...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of...
Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of elements, finding an element in the binary tree. Analyse time and space complexity of the designed algorithm Write C++ program to implement binary tree
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT