Найти кол-во элементов, заканчивающихся на 9
Есть задание: найти количество элементов в дереве, которые заканчиваются на 9. (9, 19, 29 и т.п.).
И, собственно, проблема в том, что я не могу придумать как это сделать. Находила поиск элемента через count(), но в случае с деревом оно не работает (ну или же проблема в моем не понимании)
Код:
#include <iostream>
#include <time.h>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
struct bintree
{
int inf;
bintree* left;
bintree* right;
};
bintree* proot = NULL;
void RemoveBinaryTree(bintree** proot)
{
if (*proot != NULL)
{
RemoveBinaryTree(&((*proot)->left));
RemoveBinaryTree(&((*proot)->right));
delete* proot;
*proot = NULL;
}
}
bintree* CreateNode(const int pinf)
{
bintree* node = new bintree;
node->inf = pinf;
node->left = node->right = NULL;
return node;
}
bool IsUniqueValueInsert(bintree* proot, const int pinf)
{
// "прошли" все дерево, не встретив дубликатного значения
if (proot == NULL)
return true;
// встали на узел, имеющий такой же ключ (встретили дубликат)
if (pinf == proot->inf)
return false;
// продвигаемся по дереву
if (pinf < proot->inf)
return IsUniqueValueInsert(proot->left, pinf);
else
return IsUniqueValueInsert(proot->right, pinf);
}
void Insert(bintree** proot, const int pinf)
{
if (*proot == NULL)
*proot = CreateNode(pinf);
else
if (pinf < (*proot)->inf)
Insert(&((*proot)->left), pinf);
else
Insert(&((*proot)->right), pinf);
}
int irand(const int pa, const int pb)
{
return (rand() % (pb - pa + 1) + pa);
}
void MakeBinaryTree(bintree** proot, const int pn)
{
if (*proot != NULL)
RemoveBinaryTree(proot);
srand((time)=NULL);
for (int i = 1; i <= pn; i++)
{
int key = irand(0, 100);
while (IsUniqueValueInsert(*proot, key) == false)
key = irand(0, 100);
Insert(proot, key);
}
}
void LKP(bintree* proot)
{
if (proot != NULL)
{
LKP(proot->left);
cout << setw(8) << proot->inf;
LKP(proot->right);
}
}
/*bool SearchNodeByKey(bintree* proot, const int pkey)
{
if (proot == NULL)
return false;
if (proot->inf == pkey)
return true;
if (pkey < proot->inf)
return (SearchNodeByKey(proot->left, pkey));
else
return (SearchNodeByKey(proot->right, pkey));
}*/
void postorder(bintree* proot, int indent)
{
if (proot != NULL) {
if (proot->right) {
postorder(proot->right, indent + 4);
}
if (indent) {
cout << setw(indent) << ' ';
}
if (proot->right) cout << " /\n" << setw(indent) << ' ';
cout << proot->inf << "\n ";
if (proot->left) {
cout << setw(indent) << ' ' << " \\\n";
postorder(proot->left, indent + 4);
}
}
}
void parnielementy(bintree*& proot) {
bintree* Left, * Current, * Right;
int Lefbintree, Righbintree;
Current = proot;
if (Current->right == NULL)
{
cout << "Only one element in list" << endl;
return;
}
while (Current != NULL) {
Left = Current->left;
Right = Current->right;
Lefbintree = 0;
Righbintree = 0;
while (Left != NULL) {
if (Left->inf % 2 == 0) {
Lefbintree++;
}
Left = Left->left;
}
while (Right != NULL) {
if (Right->inf % 2 == 0) {
Righbintree++;
}
Right = Right->right;
}
if (Lefbintree == Righbintree) {
cout << "Element: " << Current->inf << " has same amount of elements on the left (" << Lefbintree << ") and on the right(" << Righbintree << ") " << endl;
}
}
}
void findnine(bintree*& proot) {//конец прикола
int num = 9;
//count(proot->inf, 9);
}
int main()
{
cout << "======================================================" << endl;
cout << " Choose oprion below: " << endl;
cout << "1. Create tree" << endl;
cout << "2. Show tree." << endl;
cout << "3. Add to tree" << endl;
cout << "4. Show all elements in tree, which ends with 9" << endl;
cout << "5. Find elements, amount of paired elements in right are equal to left." << endl;
cout << "6. Delete tree" << endl;
cout << "======================================================" << endl;
int key, n;
while (true) {
cout << "\nChoose option: ";
cin >> key;
switch (key) {
case 1: {cout << "Enter amount of elements in Tree: ";
cin >> n;
MakeBinaryTree(&proot, n); } break;
case 2: {cout << "Symetrical: " << endl;
LKP(proot);
cout << endl;
cout << "Postorder: " << endl;
postorder(proot, 0); } break;
case 3: {int a;
cout << "Enter element you wanna add: ";
cin >> a;
Insert(&proot, a); LKP(proot); } break;
case 4: {findnine(proot); }break;
case 5: {parnielementy(proot); }break;
case 6: {RemoveBinaryTree(&proot); }break;
}
}
if (proot != NULL)
RemoveBinaryTree(&proot);
return 0;
}