Question

In: Computer Science

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

Solutions

Expert Solution

Solution:

balanced:: Tree a-> Bool

   int leftHeight //height of the left subtree

   int rightHeight //height of the right subtree

if Tree a==NULL

    return 1;

//calculating the height of the subtree

   leftHeight = height(Tree->left);

   rightHeight = height(Tree->right);

   if( abs(leftHeight -rightHeight ) <= 1 &&

balanced(Tree->left) && balanced(Tree->right))

     return 1;

   return 0;

}

max:: a-> int, b-> int

{

  return (a >= b)? a: b;

}   

height:: Tree a-> int //function to calculate the height of the tree

{

   if(Tree == NULL)

       return 0;

else

   return 1 + max(height(Tree->left), height(Tree->right));

}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)


Related Solutions

Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
Question 1: Processing Compound Data: Binary Trees A binary tree is a tuple with the following...
Question 1: Processing Compound Data: Binary Trees A binary tree is a tuple with the following recursive structure   ("btree", [val, left_tree, right_tree]) where first part of the tuple is a string "btree" and second part of the tuple is a list in which val is the value at the root and left_tree and right_tree are the binary trees for the left and right children or ("btree",[]) for the empty tree.   (A) Implement a binary tree ADT. Type Name Description Create...
(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...
(+5) Level of a node in a binary tree is distance from root to that node....
(+5) Level of a node in a binary tree is distance from root to that node. For example, level of root is 0 and levels of left and right children of the root are 1. Level      Max number of nodes 1 2 4 8 16 32 64 ..      … n       ?? The maximum number of nodes on level n of a binary tree is : A          2^(n-1)                         B          2^n C          2^(n+1)                       D            2^[(n+1)//2] In the above answers, the operator...
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...
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...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode* find(int i) { // implement } If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right;...
A binary tree is a rooted tree in which each node has at most two children....
A binary tree is a rooted tree in which each node has at most two children. Show by induction that in any binary tree the number of nodes with two children is exactly one less than the number of leaves.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT