void TreeToList(TreeNode * tree, ListNode * & first, ListNode * & last) // precondition: tree is a binary search tree // postcondition: first points to first node of sorted linked list // last points to last node of the sorted list // info fields of elements in list are same as in tree // if tree is NULL then first and last are NULL { if (tree == NULL){ first = last = NULL; } else{ ListNode * list = new ListNode(tree->info); // create 'middle' // construct right half of list TreeToList(tree->right, list->next, last); if (last == NULL){ last = list; } // construct left half of list ListNode * temp; TreeToList(tree->left, first, temp); if (temp == NULL){ first = list; } else{ temp->next = list; } } } int IsBalanced2(TreeNode *t) { int dummyHeight; int retval = 1; if (t != NULL){ retval = AuxBalance(t,dummyHeight); } return retval; } int AuxBalance(TreeNode * t, int & height) // postcondition: returns 1 if t is balanced, else returns 0 // sets height to the height of tree t { int retval = 1; height = 0; // for NULL tree if (t != NULL){ int left,right,leftHeight, rightHeight; left = AuxBalance(t->left,leftHeight); right = AuxBalance(t->right,rightHeight); height = max(leftHeight,rightHeight) + 1; retval = left && right && abs(leftHeight - rightHeight) <= 1; } return retval; } int OneChildCount(TreeNode * tree) // postconditin: returns # of nodes with exactly one child { int retval = 0; if (tree != NULL){ if ( (t->left != NULL && t->right == NULL) || (t->left == NULL && t->right != NULL)){ retval++; } retval += OneChildCount(tree->left) + OneChildCount(t->right); } return retval; } int WordCount(Trie * t) // postconditiN: returns 3 of words stored in t { int retval = 0; if(t != NULL){ if (t->isWord){ retval++; } for(int k=0; k < ALPH_SIZE;k++){ retval += WordCount(t->index[k]); } } return retval; } int NoPrefixCount(Trie * t) // postcondition: returns # of words without prefixes { int retval = 0; if (t != NULL){ if (t->isWord == 1){ retval++; } else{ for(int k = 0; k < ALPH_SIZE; k++){ retval += NoPrefixCount(t->index[k]): } } } return retval; } int LengthLongest(Trie * t) { int k; int max = 0; for(k = 0; k < ALPH_SIZE; k++){ int temp = AuxLong(t->index[k],0); if (temp > max){ max = temp; } } return max; } int AuxLong(Trie * t, int depth) // pre: depth = depth of t in some "global trie" // post: returns length of longest root to word path in t { int deepest = 0; // longest so far if (t != NULL){ int k; if (t->isWord == 1){ deepest = depth + 1; // word ends here, set length } for(k=0; k < ALPH_SIZE; k++){ int temp = AuxLongest(t->index[k],depth+1); if (temp > deepest){ deepest = temp; } } } return deepest; }