logo CodeStepByStep logo

isConsecutive

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

Write a member function named isConsecutive that could be added to the BinaryTree class. Your function should examine the tree and return true if there is some integer value k such that an in-order traversal of the tree's elements would yield a sequence of consecutive integers k, k+1, k+2, ... where the integers differ by exactly 1 from their neighbors in the sequence; otherwise the function should return false. Recall that an in-order traversal visits nodes in left-center-right order. For example, suppose BinaryTree variables named tree1, tree2, ..., tree6 store the trees of elements below.

The call of tree1.isConsecutive() would return true because an in-order traversal of tree1 produces the sequence of integers 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, which are consecutive integers from 4 through 14. The call of tree2.isConsecutive() would return true because an in-order traversal of tree2 produces the sequence of integers 41, 42, 43, 44, 45, which are consecutive integers from 41 through 45. The call of tree3.isConsecutive() would return false because an in-order traversal of tree3 produces the sequence of integers 9, 7, 38, 20, 15, which is not a sequence of consecutive integers. The call of tree4.isConsecutive() would return false because an in-order traversal of tree4 produces the sequence of integers 2, 3, 5, 6, 7, 9, 12, 15, which is not a sequence of consecutive integers. (It is ascending, but not consecutive.) The call of tree5.isConsecutive() would return false because an in-order traversal of tree5 produces the sequence of integers 2, 1, 3, 4, which is not a sequence of consecutive integers. (The integers could be rearranged into a consecutive sequence, but an in-order traversal does not produce a consecutive sequence.) The call of tree6.isConsecutive() would return false because an in-order traversal of tree6 produces the sequence of integers 1, 2, 3, 4, 5, 6, 8, 9, which is not a sequence of consecutive integers (missing a 7).

tree1
         8
      /     \
    7         11
   /        /    \
  5        9      13
 / \        \    /  \
4   6       10  12  14
tree2
   42
  /  \
41    45
     /
   43
     \
      44
tree3
   7
 /   \
9     15
     /
   20
  /
38
tree4
         6
      /     \
    5         15
   /        /
  3        9
 /        / \
2        7   12
tree5
  1
 / \
2   4
   /
  3
tree6
        4
      /   \
    3       6
   /       / \
  1       5   9
   \         /
    2       8

If the tree is empty or contains only a single element, your function should return true.

Your solution should be efficient. Specifically, you should not traverse over the same nodes or subtrees multiple times. You also should not explore regions of the tree if you do not need to. Once your code knows for sure whether the tree could or could not be consecutive, your algorithm should stop without exploring further.

Constraints: Do not use any auxiliary data structures to solve this problem (no array, vector, stack, queue, string, etc). Your member function should not modify the tree's state; the state of the tree should remain constant with respect to your function. Don't modify root, or a node's data, left, or right pointers. 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 leak memory. You should not be allocating dynamic memory or creating new node objects anyway. 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.