logo CodeStepByStep logo

limitLeaves

Language/Type: C++ binary trees pointers recursion
Author: Marty Stepp (on 2016/06/16)

Write a member function named limitLeaves that could be added to the BinaryTree class. Your function called accepts an integer parameter n and removes nodes from a tree to guarantee that all leaf nodes store values that are greater than n. (In other words, no leaves with values less than or equal to n should remain after the call.)

For example, suppose a variable named tree refers to the tree below. If we then make the method call of tree.limitLeaves(20); , your method must guarantee that all leaf node values are greater than 20. So it must remove the leaves that store 8, 12, and 20. But notice that removing the 12 leaf makes the node with value 17 into a leaf. This new leaf is also not larger than 20, so it must also be removed. Thus, we end up with the tree below at right.

tree before call
            13
          /    \
      18         10
     /          /  \
   82         17    23
  /  \         \      \
92    8         12     20
after call of tree.limitLeaves(20);
            13
          /    \
      18         10
     /             \
   82               23
  /
92

Notice that the nodes storing 13, 18, and 10 remain even though those values are not greater than 20 because they are branch nodes. Also notice that you might be required to remove nodes at many levels. For example, if the node storing 23 instead had stored the value 14, then we would have removed it as well, which would have led us to remove the node storing 10. Your function should remove the minimum number of nodes that satisfies the constraint that all leaf nodes store values greater than the given n.

Constraints: Do not construct any new BinaryTreeNode objects in solving this problem (though you may create as many BinaryTreeNode* pointer variables as you like). Do not change the data field of any existing nodes of the tree. Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Do not leak memory. If you remove a node from the tree, free its memory. For full credit, your solution should be at worst O(N) time, where N is the number of elements in the tree. You must also solve the problem using a single pass over the tree, not multiple passes. Your solution must be recursive.

Write the member function as it would appear in BinaryTree.cpp. You do not need to declare the function header that would appear in BinaryTree.h. Assume that you are adding this method to the BinaryTree class as defined below:

class BinaryTree {
private:
    BinaryTreeNode* root;   // NULL for an empty tree
    ...
    
public: 
    your code goes here;
};

struct BinaryTreeNode {
    int data;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
    ...
}
Type your solution here:


This is a member function problem. Submit a member function that will become part of an existing C++ class. You do not need to write the complete class, just the member function described in the problem.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.