I am having a lot of trouble writing recursive functions related to trees. I can't just use google for these functions as they are not general and I won't be able to use google in an exam setting!
Is there some 'trick' to successfully coming up with an algorithm/writing psuedocode for recursive functions? Is there a certain way I should think about/approach this?
Example: Write a recursive function that determines whether a given binary tree has a structure consistent with a valid AVL tree.
Solution Expected:
template <typename Type>
bool is_avl (const Binary_node<Type> * tree) {
if (tree == NULL) {
return true;
}
return is_bst (tree)
&& is_avl (tree->left())
&& is_avl (tree->right())
&& std::abs(height(tree->left()) - height(tree->right())) <= 1;
}