Page 1 of 1

You are asked to implement the following recursive function: TNode* addLayer( TNode *root, int val ) Here's the definiti

Posted: Sat May 14, 2022 8:34 pm
by answerhappygod
You Are Asked To Implement The Following Recursive Function Tnode Addlayer Tnode Root Int Val Here S The Definiti 1
You Are Asked To Implement The Following Recursive Function Tnode Addlayer Tnode Root Int Val Here S The Definiti 1 (84.86 KiB) Viewed 35 times
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; 1/Put val in here for your new tNodes struct TNode *left, right; } TNode; 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 42 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, erit, and addLayer. TNode* addLayer( TNode *root, int val ) { 1/Declare any local variables you need //Base case: //Recursive case: (be sure to use the return value from the rec calls) : 1/Return the TMode* that the parent of this node should point to