logo CodeStepByStep logo

removeMatchingLeaves

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

Write a member function named removeMatchingLeaves that could be added to the BinaryTree class. The function accepts a reference to a second BinaryTree as its parameter and removes any leaf nodes that "match" with a corresponding node in the other tree. For this problem, two nodes "match" if they contain the same data and are located in exactly the same place in the tree relative to the trees' roots. For a node to be removed, it must be a leaf in your tree; but it need not be a leaf in the other tree passed as a parameter. If the second tree doesn't contain a node at that corresponding location or the value there is different, it does not match.

For example, suppose two variables of type BinaryTree called tree1 and tree2 store the elements below. The call of tree1.removeMatchingLeaves(tree2); would modify tree1 to store the elements below at right. The leaf nodes containing 0, 2, and 6 have been removed because they match the corresponding nodes in tree2. The other leaves from tree1, which contain the values 8 and 5, are not removed because they aren't matched in tree2.

tree1 tree2 tree1 after tree1.removeMatchingLeaves(tree2);
      3
    /   \
  4       7
 / \     / \
0   9   2   5
   / \
  8   6
       3
     /   \
  28       7
 / \      /
0   9    2
 \   \    \
  8   6    31
      3
    /   \
  4       7
   \       \
    9       5
   /
  8

You should not leak memory; if your function removes nodes from the tree, free the associated memory.

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 use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). You may define helper functions, but otherwise do not call any other member functions of the tree class. You should not modify the tree passed in as the parameter. You also should not change the data of any nodes. It may be helpful for you to note that your function can directly access the other tree's root field with an expression such as tree2.root (this is allowed in C++ because they are objects of the same class). 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 C++ solution code 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.