ai…..Google….good? bad?
wrong way to learn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
struct Tree{ int info; Tree * left, * right; }; Tree * ReadTree(); void Level(Tree *); int IsSpecified(Tree *); int currentNode; const int MAX_NODES = 300; Tree * nodes[MAX_NODES]; // grab max nodes up front void ZeroNodes() // postcondition: all nodes are illegal (no children, 0 info) { ................ } void InitNodes() // postcondition: nodes allocated and zero'd { ................ } main() { Tree *t; InitNodes(); while (t = ReadTree()){ if (!IsSpecified(t)){ cout << "not complete" << endl; } else{ Level(t); } } } void AddTree(Tree * & t, char *s, int val) // add node with 'val' using path 's' to tree 't' { .................. .......... AddTree(t->left,s+1,val); // go left } else{ AddTree(t->right,s+1,val); // go right } ........... t->info = -1; // already seen it } } } Tree *ReadTree() { char buf[100]; Tree * t = NULL; ZeroNodes(); while (cin >> buf){ if (strcmp(buf,"()") == 0){ break; } else{ buf[strlen(buf)-1] = ''; // remove ')' char *b = buf; while (*b != ','){ // look for comma b++; } *b = ''; AddTree(t,b+1,atoi(buf+1)); } } return t; } |