Binary Tree Operations

Write a menu-driven program that will perform the following operations on a binary tree. Use the declarations shown. All pre-conditions assume that the parameter tree is initialized but may be empty or just contain one (root) node.

  1. int countreeNodes(TreeNode tree)
    // post: returns the number of nodes in the tree

  2. int countLeaves(TreeNode tree)
    // post: returns the number of leaves (non-parents)
    // in the tree

  3. int height(TreeNode tree)
    // post: returns the height of the tree; 0 if empty,
    // 1 if only one node

  4. void interchange(TreeNode tree)
    // post: swaps all the left subtrees with all the right
    // subtrees of the tree

  5. Boolean isAncestor(TreeNode tree, int p, int c)
    // post: returns true if the node containing p is an
    // ancestor of another node containing c
    // otherwise returns false

  6. Bonus
    void printLevel(TreeNode tree, int level)
    // post: displays the information stored in each node of
    // the tree at a given level >= 0

Continue to:  Unit 8 / Prev / Next