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


i need function in project take tree argument , return number of integers typed , correct call.

wouldn't preorder? 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 preorder(t).

this original function 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 << " on 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; } 

first, function can't void. has return number of ints typed, has return int or int*.

secondly, tree binary tree has ints typed? if so, tree traversal algorithm do. need variable increment when find new node (assuming store int).

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

if t isn't null, has 1 int stored in it. have check node's children.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -