Question: P 5 . ( 2 5 points ) Write a function int follow _ path ( link root, int * A , int F )

P5.(25 points) Write a function int follow_path(link root, int * A, int F) that takes as
arguments a tree (given by the root node, root), an array, A, and a number, F, and returns the item found in the
tree, following the path given by the first F elements of A:
- if you see a 0 in A, go left in the tree;
- if you see a 1 in A go right in the tree.
Continue the same way. For the function calls below, assume that root is pointing to the root, 8, of the tree
below.
8
- follow_path (root,[0,1],2) returns 3(from root, go left, then right)
- follow_path (root,[1,0],2) returns 4
- follow_path (root,[0,1,1,0],2) returns 3
- follow_path (root,[1],1) returns 5
- follow_path (root,[0,1],0) returns 8
0
95
2
You can only assume that A has at least F elements in it whenever it is called.
1
3
4
If anything goes wrong in the function call, it should return -1.
5
7
4
You can use a helper function if you want. The nodes and links are defined as follows:
typedef struct node * link;
struct node{
int data;
link left;
link right;
(6 points) What special cases are there? Focus especially on cases that can crash your code. Give an
example (function call and tree) that show each special case.
};
a) b)(4 points) Fill in the time complexity of the code you wrote for part c).(If needed, you can assume the
tree has N nodes in total.)
O (____________)
c)(15 points) Write the complete code for the function. It should deal with the special cases that you
mentioned. Each line or section that deals with a special case, should clearly indicate what case it deals
with.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!