Answered step by step
Verified Expert Solution
Question
1 Approved Answer
How is it possible to print from a leaf node to an Ancestor root node? Problem 2 Definition: A Lowest Common Ancestor (LCA) of two
How is it possible to print from a leaf node to an Ancestor root node?
Problem 2 Definition: A Lowest Common Ancestor (LCA) of two node (given by the keys akey1 and akey2) is the node that occurs in the path from the root to the node with the key akey1 and that also occurs in the path from the root to the node with the key akey2. Moreover, LCA is the deepest such node (as far from the root as possible) For example, on Figure to the left, given two keys, ma and no, the key mo occurs on the path from the root to ma and from the root to no, and it is the farthest from the root (the keys lo and pl also occur 2on both paths, but they are not the deepest such be nodes) to ma mq no Assignment: You need to write a recursive member function (or a few functions) that accomplishes this goal: given two keys akey1 and akey2, it will print out the nodes on the path from akey1 to their LCA and the nodes in the path from akey2 to their LCA. You must to write this public member function (with exact header as shown below) void printPathsLCA(string akey1, string akey2) You may write as many functions as you need to accomplish this task, but these functions must be called from the printPathsLCA function. You must accomplish this task in O(logn)-time. This means that all functions that you will write must run in O(logn)-time. If your program runs in asymptotically larger time, you will not receive any credit. Given: Two strings akey1 and akey2 Output: Use cout to print out all the keys on two paths. Format of the output Output a space after each key; At the end of each path, output endl - Example Given a tree shown on Figure to the left, and two keys re and mq, your program will print out these two lines re sp pl mq mu mo pl 2 The path from the first key re to their LCA pl is re sp pl The path from the second key mq to their LCA is mo emq mu mo pl To test your program: use files t19.in and t19.out /runStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started