c++ - Function to find whether a tree is mono (all the elements are unique) or not? -
im trying add total function , ismono functions code. did total already
need function ismono returns whether tree mono (all elements unique aka no element appears more 1 time) or not. please
#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; }
just traverse tree!
//gets value of node @ leftmost node int left_most_value(tnode * t) { if (t == null) return (0); //something went wrong if (t->left == null) return(t->info); else return(left_most_value(t)); } //gets value of node @ rightmost node int right_most_value(tnode * t) { if (t == null) return (0); //something went wrong if (t->right == null) return(t->info); else return(right_most_value(t)); } //returns true (1) if node not have duplicate int node_is_mono(tnode * t) { //ignore leaf nodes if (t->left == null && t->right == null) return 1; //check left side if (t->left != null && right_most_value(t->left) == t->info) return(0); //check right side if (t->right != null && left_most_value(t->right) == t->info) return(0); //this node mono return(1); } int tree_is_mono(tnode * t) { if (t == null) return(1); //if 1 node has duplicate, entire tree not mono if (node_is_mono(t) == 0) return 0; else return(tree_is_mono(t->left) && tree_is_mono(t->right)); }
explanation of algoritm
at every node in tree, need perform following steps.
- find node's left-most node node's children on right. if equal in value tree not mono (stop searching ,
return false
) - find node's right-most node node's children on left. if equal in value, tree not mono (stop searching ,
return false
) - we found no nodes equal in value, tree mono!
return true
Comments
Post a Comment