10 points Question 3 : Trees .. You are asked to implement the following recursive function: TNode* addLayer TNode *root
Posted: Sat May 14, 2022 8:23 pm
10 points Question 3 : Trees .. You are asked to implement the following recursive function: TNode* addLayer TNode *root, int val ) Here's the definition of T Node struct: typedef struct tNode { int key; struct TNode *left, *right; } TNode; //Put val in here for your new TNodes This function replaces the NULLs at the bottom of the tree with new T Nodes that contain the specified val as their key. Here's an example of what it would do to a given tree: 50 50 70 NULL 70 NULL NULL 60 NULL 60 42 NULL NULL NULL NULL before addLayer( root, 42 ) 42 42 NULL NULL NULL NULL after addLayer( root, 42 ) Remember to malloc each of the new T Nodes at the bottom of the tree. If your malloc fails, your function should call exit(-1). Like we saw with inserting into a Binary Search Tree (BST), the function addLayer should return the T Node* that the parent of this T Node should point to. To help see the point of this consider the case where the root is NULL. 1337 NULL before addLayer( root, 1337 ) NULL NULL after addLayer( root, 1337 )
The only functions you should call are malloc, sizeof, exit, and addLayer. TNode* addLayer( TNode *root, int val ) { 7/Declare any local variables you need //Base case: //Recursive case: (be sure to use the return value from the rec calls) //Return the TNode* that the parent of this node should point to }
The only functions you should call are malloc, sizeof, exit, and addLayer. TNode* addLayer( TNode *root, int val ) { 7/Declare any local variables you need //Base case: //Recursive case: (be sure to use the return value from the rec calls) //Return the TNode* that the parent of this node should point to }