logo CodeStepByStep logo

range

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

Write a member function named range that could be added to the BinaryTree class. The function accepts minimum and maximum integers as parameters and removes from the tree any elements that are not in that range, inclusive. You should also return an integer count of the number of elements that were removed, if any.

For this function, assume that your tree is a binary search tree (BST) and that its elements are arranged in a valid BST order. Your function should maintain the BST ordering property of the tree. For example, suppose a variable of type BinaryTree named tree stores the following elements:

(50 (38 (14 (8) (20 / (26))) (42)) (90 (54 / (72 (61) (83)))))

The table below shows what the state of the tree would be if various calls were made. The calls shown are separate; it's not a chain of calls in a row. Also notice that each call returns the number of nodes that were removed.

tree.range(25, 72) tree.range(54, 80) tree.range(18, 42) tree.range(-3, 6)
(50 (38 (26) (42)) (54 / (72 (61))))
(54 / (72 (61)))
(38 (20 / (26)) (42))
(empty tree)
returns 5 returns 9 returns 8 returns 12

Do not leak memory; if you remove a node from the tree, free the memory associated with that node. If the range is invalid (if the minimum exceeds the maximum), throw an integer exception and don't modify the tree.

Constraints: Your solution should be at worst O(N) where N is the number of elements in the tree. 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 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.