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

Need help with C++, binary tree node, recursive linear-time function!! Thank you

ID: 3692865 • Letter: N

Question

Need help with C++, binary tree node, recursive linear-time function!! Thank you!!

--------------------------------------------------------------------------------------

Suppose that a node in a binary tree is represented as follows:
struct Node {
int key;
struct Node *left, *right; // pointers to the left and right children
};

Complete the code of a recursive function findPath below that takes a pointer to the root of a
binary tree and returns a vector of keys on a path (any path) from the root to the node with a
given key value. This function has the following arguments:
root is a pointer to an instance of struct Node that corresponds to the root of a tree
path is a reference to a vector of node keys
key is the key of the node to which a path needs to be found


// find a path from root node to a given node and store the found path in a vector path
// return true if a path exists otherwise false
bool findPath(Node *root, std::vector &path, int k)
{


}

--------------------------------------------------------------------------------------

Plus I gave the links to my other 4 problems if you want to do those as well! Thank you!

https://www.chegg.com/homework-help/questions-and-answers/need-help-c-binary-tree-node-recursive-linear-time-function-thank-plus-gave-links-4-proble-q12397569

https://www.chegg.com/homework-help/questions-and-answers/need-help-c-binary-tree-node-recursive-linear-time-function-thank-suppose-node-binary-tree-q12397737

https://www.chegg.com/homework-help/questions-and-answers/need-help-c-binary-tree-node-recursive-linear-time-function-thank-suppose-node-binary-tree-q12397779

https://www.chegg.com/homework-help/questions-and-answers/need-help-c-binary-tree-node-recursive-linear-time-function-thank-suppose-node-binary-tree-q12397829

Explanation / Answer

#include #include struct bin_tree { int data; struct bin_tree * right, * left; }; typedef struct bin_tree node; void insert(node ** tree, int val) { node *temp = NULL; if(!(*tree)) { temp = (node *)malloc(sizeof(node)); temp->left = temp->right = NULL; temp->data = val; *tree = temp; return; } if(val < (*tree)->data) { insert(&(*tree)->left, val); } else if(val > (*tree)->data) { insert(&(*tree)->right, val); } } void print_preorder(node * tree) { if (tree) { printf("%d ",tree->data); print_preorder(tree->left); print_preorder(tree->right); } } void print_inorder(node * tree) { if (tree) { print_inorder(tree->left); printf("%d ",tree->data); print_inorder(tree->right); } } void print_postorder(node * tree) { if (tree) { print_postorder(tree->left); print_postorder(tree->right); printf("%d ",tree->data); } } void deltree(node * tree) { if (tree) { deltree(tree->left); deltree(tree->right); free(tree); } } node* search(node ** tree, int val) { if(!(*tree)) { return NULL; } if(val < (*tree)->data) { search(&((*tree)->left), val); } else if(val > (*tree)->data) { search(&((*tree)->right), val); } else if(val == (*tree)->data) { return *tree; } } void main() { node *root; node *tmp; //int i; root = NULL; /* Inserting nodes into tree */ insert(&root, 9); insert(&root, 4); insert(&root, 15); insert(&root, 6); insert(&root, 12); insert(&root, 17); insert(&root, 2); /* Printing nodes of tree */ printf("Pre Order Display "); print_preorder(root); printf("In Order Display "); print_inorder(root); printf("Post Order Display "); print_postorder(root); /* Search node into tree */ tmp = search(&root, 4); if (tmp) { printf("Searched node=%d ", tmp->data); } else { printf("Data Not found in tree. "); } /* Deleting all nodes of tree */ deltree(root); }