Question

In: Computer Science

. Let BT Node be the class we often use for binary-tree nodes. Write the following...

. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children.

Solutions

Expert Solution

class BT: 
  
    # Constructor of the BT class for creating the nodes of the Tree 
    def __init__(self , key): 
        self.key = key 
        self.left = None
        self.right = None
  
 
def isEven(T): 
  
    #If current node of Tree is empty
    if T is None:     
        return True
      
    #If current node of Tree is a leaf node: No of leaves is 0 i.e Even case
    if T.left is None and T.right is None: 
        return True
  
    #If both the left and the right subtrees are not None and 
    #both left and right subtrees are even 
    #(Case of 2 childs as current node has 2 childs 
    # we recursively check for both left and right childs of
    # current node of Tree).
    if T.left is not None and T.right is not None: 
        return (isEven(T.left) and isEven(T.right)) 
      
    # We reach here when none of the above if condiitions work 
    return False
    
def numberLeaves(T):
    #If current node of Tree is null return 0
    if T is None: 
        return 0 
    #If the current node of Tree has no left and right node 
    #return 1 
    if(T.left is None and T.right is None): 
        return 1 
    #Else call numberLeaves for left and right node recursively
    #and add both to get total leaves from both side
    else:
        return numberLeaves(T.left) + numberLeaves(T.right)
  
# Driver Program 
root = BT(10) 
root.left = BT(20) 
root.right = BT(30) 
  
root.left.right = BT(40)
root.left.left = BT(50) 

#The given Binary Tree in this case.  
#            10
#          /   \
#         20   30
#       /   \     
#      40   50  
#The BT is even and has 3 leaves.

#Checking is given Binary Tree is even or not
#using the isEven()
if isEven(root): 
    print("The given Binary tree is Even")
else: 
    print("The given Binary tree is not Even")
#Calling numberLeaves() and printing the number of leaves in 
#given Binary Tree
print("Number of leaves in given Binary Tree is: ",numberLeaves(root))

Output Screenshot:

(Note: Please refer to the screenshot of the code to understand the indentation of the code.)

Code Screenshots:

BT node:   

isEven():

numberLeaves():

main function:


Related Solutions

2. Let BT Node be the class we often use for binary-tree nodes. Write the following...
2. Let BT Node be the class we often use for binary-tree nodes. Write the following recursive methods: (a) numLeaves: a method that takes a BT Node T as parameter and returns the number of leaves in the tree rooted at T. (b) isEven: a boolean method that takes a BT Node T and checks whether its tree is strictly binary: every node in the tree has an even number of children. 3. Suppose you want to improve Merge Sort...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes...
Write a binary search tree with 10 nodes in Java, each node will have 3 attributes (data, x, y). The binary tree need to have function "add()" to add new node into the tree. After added all 10 nodes, it will be sorted and turn into a balanced binary search tree.
(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...
a tree is binary, if every node has at most two children nodes. prove that the...
a tree is binary, if every node has at most two children nodes. prove that the maximum of nodes in a binary tree of height h is 2^(h+1)-1
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...
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
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...
Write a binary search tree and include the functions to find the number of nodes and...
Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal. in c++ please.
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
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree...
Write a O(n) method valuesInLevelOrder() that returns a list of the nodes of a binary tree in level-order. That is, the method should return the root, then the nodes at depth 1, followed by the nodes at depth 2, and so on. Your algorithm should begin by putting the tree root on an initially empty queue. Then dequeue a node, add it to the output, and enqueue its left and right children (if they exist). Repeat until the queue is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT