logo CodeStepByStep logo

hasPath

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

Write a function named hasPath that interacts with a tree of BinaryTreeNode structures representing an unordered binary tree. The function accepts three parameters: a pointer to the root of the tree, and two integers start and end, and returns true if a path can be found in the tree from start down to end. In other words, both start and end must be element data values that are found in the tree, and end must be below start, in one of start's subtrees; otherwise the function returns false. If start and end are the same, you are simply checking whether a single node exists in the tree with that data value. If the tree is empty, your function should return false.

For example, suppose a BinaryTreeNode pointer named tree points to the root of a tree storing the following elements. The table below shows the results of several various calls to your function:

           67
        /      \
    80            52
  /    \         /
16     21      99
      /
    45
Call Result Reason
hasPath(tree, 67, 99) true path exists 67 -> 52 -> 99
hasPath(tree, 80, 45) true path exists 80 -> 21 -> 45
hasPath(tree, 67, 67) true node exists with data of 67
hasPath(tree, 16, 16) true node exists with data of 16
hasPath(tree, 52, 99) true path exists 52 -> 99
hasPath(tree, 99, 67) false nodes do exist, but in wrong order
hasPath(tree, 80, 99) false nodes do exist, but there is no path from 80 to 99
hasPath(tree, 67, 100) false end of 100 doesn't exist in the tree
hasPath(tree, -1, 45) false start of -1 doesn't exist in the tree
hasPath(tree, 42, 64) false start/end of -1 and 45 both don't exist in the tree

An empty tree does not contain any paths, so if the tree is empty, your method should return false. You should not assume that your tree is a binary search tree (BST); its elements could be stored in any order.

Constraints: Your function should not modify the tree's state; the state of the tree should remain constant with respect to your function. 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).

Assume that you are using the BinaryTreeNode structure as defined below:

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


This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

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.