// newfunctions.cpp #include using namespace std; //introduces namespace std #include "CS215tree.h" // CS215tree::height // Calculate the height of a binary tree by recursion // Precondition: The node of a tree of integers is passed // Postcondition: The height of the tree is returned // If the tree is null, 0 is returned int CS215tree::height(CS215node* node1) { int hgt = 0; int lefth,righth; if (node1 == NULL) { hgt = 0; } else { lefth = height(node1->left); righth = height(node1->right); if (lefth > righth) hgt = 1 + lefth; else hgt = 1 + righth; } return (hgt); } // CS215tree::sum // Calculate the sum of all integers in a binary tree of integers by recursion // Precondition: The node of a tree of integers is passed // Postcondition: The sum of all the integers is returned // If the tree is null, 0 is returned int CS215tree::sum(CS215node* node1) { int sumnum = 0; if (node1 == NULL) { sumnum = 0; } else { sumnum = node1->info + sum(node1->left) + sum (node1->right); } return sumnum; } // CS215tree::sumLeaf // count the leaf nodes in a binary tree of integers by recursion // Precondition: The node of a tree of integers is passed // Postcondition: The number of leaf nodes is returned // If the tree is null, 0 is returned int CS215tree::sumLeaf(CS215node* node1) { int sumnodes = 0; int suml, sumr = 0; if (node1 != NULL) { if (node1->left == 0 && node1->right == 0) { sumnodes = 1; } else { if (node1->left != NULL) { suml = sumLeaf(node1->left); } if (node1->right != NULL) { sumr = sumLeaf(node1->right); } sumnodes = suml + sumr; } } return sumnodes; }