# A function that will take a tree as argument and return the number of ints that were typed?

I need a function in my project that will take a tree as argument and return the number of integers that were typed and a correct call.

wouldn't it just be preorder? like below. Please. Thanks

#include "t.h" void preorder(tnode * t){ if (t == NULL) return; cout << t->info <<endl; preorder(t->left); preorder(t->right); }

call would be preorder(t).

This is the original function i have

#ifndef T_H #define T_H #include <iostream> #include <iomanip> using namespace std; struct tnode { int info ; int count; tnode * right, *left; }; tnode * insert(int target,tnode * t); tnode * makenode(int x); tnode * tsearch(int x,tnode * t); void inorder(tnode * t); int height(tnode * t); int count(tnode * t) ; int total(tnode * t) ; #endif int main() { int n,c; tnode * t = NULL, *x; while (cin >> n) {t=insert(n,t);cout << n <<' ';} cout << endl; inorder(t); cout << endl; c = count(t); cout << "count: "<< c <<endl; cout << endl; c = height(t); cout << "height: "<< c <<endl; cout << endl; c=200; while (c-->0) if (x = tsearch(c,t)) cout << c << " is on the tree."<<endl; return 0; } #include "t.h" int count(tnode * t) { if (t == NULL) return 0; return 1 + count(t->left) + count (t->right); } #include "t.h" int height(tnode * t) { if (t == NULL) return -1; return 1 + max(height(t->left) , height (t->right)); } #include "t.h" //write out t in order void inorder(tnode * t) { if (t == NULL) return; inorder (t->left);//write out lst in order cout <<setw(5) << t->info <<setw(5) << t->count<< endl; inorder (t->right);//write out rst in order } #include "t.h" tnode * insert(int x, tnode * t) { tnode * tmp = tsearch(x,t); if (tmp != NULL) { tmp->count++; return t; } if (t == NULL) return makenode(x); if ( x < t->info ) { t->left = insert(x,t->left); return t; } t->right = insert(x,t->right); return t; } #include "t.h" tnode * makenode(int x) { tnode * t = new tnode; t->info =x; t->count =1; t->right = t->left = NULL; return t; }

## Answers

First, your function can't be void. It has to return the number of ints that were typed, so it has to return int or int*.

Secondly, is the tree a binary tree that has all the ints that were typed? If so, any tree traversal algorithm will do. You just need a variable that you will increment when you find a new node (assuming they all store an int).

int preorder(tnode * t){ if (t == NULL) return 0; else{ return 1 + preorder(t->left) + preorder(t->right); } }

If t isn't null, then it has 1 int stored in it. Then you just have to check the node's children.

When the user types a number, the insert function either inserts a new node with a count of 1 or adds to the count of an existing node. You need to sum up the counts of the elements in the tree:

int tcount(tnode * t){ if (t == NULL) return 0; return t->count + tcount(t->left) + tcount(t->right); }

A typical call would be

tnode * root = NULL; /* insert stuff into the tree */ int count = tcount(root);