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)...
(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...
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.
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary...
PYTHON CODING Using the structural node and methods discussed in Binary Search Tree below # Binary Tree Node structure class Node: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None class BSTree(): def __init__(self, rootdata): self.root = Node(rootdata)    def insert(self, data, cur_node): if data < cur_node.data: if cur_node.left == None: cur_node.left = Node(data) else: self.insert(data, cur_node.left)    elif data > cur_node.data: if cur_node.right == None: cur_node.right = Node(data) else:...
Consider an implementation of binary trees with Scheme lists, as in the following example:
Consider an implementation of binary trees with Scheme lists, as in the following example: Before proceeding, it may be useful to define three auxiliary functions (left T), (right T) and (val T) which return the left subtree, the right subtree, and the value in the root of tree T, respectively. (a) Write a recursive function (n-nodes T), which returns the number of nodes in the tree T. The following example illustrates the use of this function: > (n-nodes T)8 (b) Write a recursive function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT