rite 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 Binary
Language/Type:
C++ binary trees pointers recursion
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
88
52
1
21
16
99
45
Call
Result
Reason
hasPath(tree,
67, 99)
true
path exists 67-> 52 -> 99
hasPath(tree,
80,
true
path exists 80->21-> 45
node exists with data of 67
45)
hasPath(tree, 67, 67) true
hasPath(tree, 16,
16)
true
node exists with data of 16
hasPath(tree, 52, 99)
path exists 52-> 99
true 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 function 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: You must implement your function recursively and without using loops. 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,
Assume that you are using the BinaryTreeNode structure as defined below:
struct BinaryTreeNode {
int data;
BinaryTreeNode left;
BinaryTreeNode right;
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images