Question

Write Prolog rules as described in the questions below. You may use any Prolog builtin predicates....

Write Prolog rules as described in the questions below. You may use any Prolog builtin predicates.

A binary tree is defined by the structure node(left,right), where left and right can be either another node or any Prolog data item.

Write the rule isBalanced(Tree) that determines if the tree is balanced. A binary tree is balanced if, at every node, the difference between the number of leaves appearing in the left and right subtree is at most one. (A tree which contains just one leaf is considered balanced.)

For example: ?- isBalanced(1).
true.

?- isBalanced(node(1,2)).
true.

?- isBalanced(node(1,node(1,node(1,2)))).
false.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A binary tree is made up of a root node and two successors following the same pattern.

For eg we represent the empty tree as null and non empty tree as T(V,left,right)

T= roo node or parent node

left= left subtree or subnode

right= right subtree or subnode.

Example of tree :

Tree= T(a,T(b,T(d,null,null),T(e,null,null),T(c,null,T(f,T(g,null,null),null)))

Add a comment
Know the answer?
Add Answer to:
Write Prolog rules as described in the questions below. You may use any Prolog builtin predicates....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Haskell for the above problems. Unless stated otherwise do not use library functions that are...

    Use Haskell for the above problems. Unless stated otherwise do not use library functions that are not in the Haskell standard prelude. 5. Consider the following definition for a binary tree. data Tree a Empty l Leaf a l Node a (Tree a) (Tree a) A binary tree is balanced if, at every node, the difference between the height the left and right subtree is at most one Height is defined as follows: height of an Empty node is 0...

  • ///Program needs to write in prolog/// 6. A binary tree is either empty or it is...

    ///Program needs to write in prolog/// 6. A binary tree is either empty or it is composed of a root element and two successors, which are binary trees themselves. In Prolog we represent the empty tree by the atom 'nil' and the non-empty tree by the term t(X,L,R), where X denotes the root node and L and R denote the left and right subtree, respectively. The following Prolog term represents the given binary tree below. T1 = t(a,t(b,t(d,nil,nil),t(e,nil,nil)),t(c,nil,t(f,t(g,nil, nil),nil))) d...

  • Complete in Java : Question 2: Write a program that can convert a sorted array into...

    Complete in Java : Question 2: Write a program that can convert a sorted array into a balanced binary search tree. A binary search tree is a balanced binary search tree if the size of the left and right subtree at each node differs by at most one. The size of the left subtree is defined as the number of nodes in the left subtree. The size of the right subtree is defined as the number of nodes in the...

  • C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with...

    C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with up to 3-nodes for each node is given below. This is not a binary search tree! class Tree3 { public : Tree3() {} private : class Node { public : int Val ; Node * Left ; Node * Middle ; Node * Right ; }; }; Node * Root { nullptr }; a) A Node is considered balanced if the sum of the...

  • A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ

    Data structures C++1- A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1 Out of the following choices, which is the minimum set of nodes, if removed, will make the BST balanced?2- Which of the following is true for Red-Black Trees ? Select all choices that apply! Select one or more: a. For each node in the tree, all paths from that node to any leaf nodes contain...

  • using java to write,show me the output. please write some common. You CAN NOT use inbuild...

    using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main {    public static void main(String[] args) {        BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...

  • You are given the root of a Binary Search Tree. Print the leaf elements of the...

    You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined the following node C++ Node class for you: class Node { public:         int name; Node* left = NULL;         Node* right = NULL; }; Function to code: void printLeaves(Node* root); The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access...

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

  • C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node...

    C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT