In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of November 30th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #07 exercise sheet]


[AED036] Even Nodes

In this problem you should only submit a binaryTree.h file with a self-containing definition of a class BTree<T>

Base Code

Use as a basis the class BTree<T> (see code | download code) which implements a generic binary tree that was explained in classes, with methods to read a tree from standard input, to write several node traversals, to count the number of nodes, to compute the height and to see if it contains a certain value.

The Problem

Add a new method int countEven() to the class. The function should return the number of nodes that contain an even number. Your method will only be called with trees of integers. For instance, the integer trees in the following have respectively 4, 1 and 3 even numbers in their nodes (indicated in green).

Submission

You should submit the file binaryTree.h with the method countEven added to binaryTree<T> as requested (and without removing any of the other existing methods). You can however create additional methods, if you need them.

Mooshak will use the following code to link to your class, read the inputs, build the trees and call your method, printing its result.

#include <iostream>
#include <string>

#include "binaryTree.h"

// Read a tree of ints assuming "N" as the null value
// call t.countEven() method and write its output
void read() {
  BTree<int> t;
  t.read("N");

  std::cout << "t.countEven() = " << t.countEven() << std::endl;
}

int main() {

  int n;
  std::cin >> n;
  for (int i=0; i<n; i++) read();
  
  return 0;
}

Example Input Example Output
3
6 3 2 N N 5 N N 10 8 N N 25 N N
1 13 3 N N 9 N N 34 N N
8 91 47 N N N 55 22 N N 40 N N
t.countEven() = 4
t.countEven() = 1
t.countEven() = 3

The example input corresponds to the trees in the example figure given in the problem description.



Algorithms and Data Structures (L.EIC011) 2024/2025
DCC/FCUP & DEI/FEUP - University of Porto