Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the C code below as a guide, write the insert function, which given a tree

ID: 3674986 • Letter: U

Question

Using the C code below as a guide, write the insert function, which given a tree pointer, and a value of N property inserts a new node with value N into a sorted binary tree pouted to by treeptr It should return a pointer to the modified tree You may assume that the subroutine newNode exists and that it accepts a single argument, and returns a poster to a newly allocated node You do not need to concern yourself with how newNode works or how new nodes are allocated. Node* Insert {//If the tree is empty, return a new, single node if { return newNode(N): } else {//2 Otherwise, recurse down the tree if curnode right arrowleft = insert else cumnode right arrowright = insert return curnode;//return the node pointer } }

Explanation / Answer

// A function to insert a new node with given key in sorted binary tree */
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL) return newNode(key);

/* Otherwise, recur down the tree */
if (key <= node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);   

/* return the (unchanged) node pointer */
return node;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote