实施:
bool tree_isBalanced(tree_t tree);
// EFFECTS: returns true if tree is balanced, false otherwise
鉴于您可以假设已经为您实现了以下功能:
bool tree_isEmpty(tree_t tree);
// EFFECTS: returns true if tree is empty, false otherwise
tree_t tree_make();
// EFFECTS: creates an empty tree.
tree_t tree_make(int elt, tree_t left, tree_t right);
// EFFECTS: creates a new tree, with elt as it's element, left as
// its left subtree, and right as its right subtree
int tree_elt(tree_t tree);
// REQUIRES: tree is not empty
// EFFECTS: returns the element at the top of tree.
tree_t tree_left(tree_t tree);
// REQUIRES: tree is not empty
// EFFECTS: returns the left subtree of tree
tree_t tree_right(tree_t tree);
// REQUIRES: tree is not empty
// EFFECTS: returns the right subtree of tree
如果树中每个节点的右子树和左子树的高度相同,则树是平衡的。树的高度定义为从树根到树中最深节点的路径中存在的节点数。只有一个节点的树的高度为 1,而空树的高度为零。因此,空树被认为是完美平衡的。
当我们向下移动一棵树时,我该如何处理递归增长?