Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Just answer the question about the choice and explanation on which traversals would best address each scenario and why. We saw the mechanics of Tree
Just answer the question about the choice and explanation on which traversals would best address each scenario and why. We saw the mechanics of Tree Traversals and discussed how they're different from one another, but if we want to write practical software, we also need to know when we should be using the techniques we're learning about. In each of the scenarios described below in which a tree traversal is required, what would be the appropriate kind of tree traversal to use? For each one, briefly explain (in no more than a sentence or two) why. 1. You're writing an analysis tool for a hierarchical organization, in which there is one person fundamentally in charge, to whom some number of people report, to each of whom some number of people report, and so on. There is a ranking in the organization, so the person in charge has rank 1, the people who report to that person have the rank 2, the people who report to the rank-2 people have the rank 3, and so on. Your input is this kind of organizational hierarchy. Your desired output is to be able to find the highest-ranking person (i.e., the person with the minimum rank number) who meets some characteristic (such as the highest- ranking person under 30 years old). 2. Your input is a binary tree that is an expression tree, in which each non-leaf node contains a binary mathematical operator (such as +, -,x, or +) and each leaf node contains a numeric value (such as 2 or 9.5). Your desired output is the value of the expression represented by the tree - so, for example, if the tree had a root node + and its two children were the numeric values 3 and 6, the result would be 9. 3. You're implementing a copy constructor for a class that implements a general tree using the "list of children" technique, with the objective being the usual one when implementing a copy constructor: The newly-constructed tree is wholly separate from the original, so that any modification to one does not affect the other. Here are the notes on the Tree Traversals to help answer the question Tree traversals A tree traversal is an algorithm that visits the data element in every node in a tree exactly once. Again, the notion of "visiting" is an abstract one; what you do when you visit a data element can be anything you need done, but it doesn't change the core algorithm used to find every node and ensure that its data element is only visited once. So we should concentrate on that algorithm, because once we understand that we can use it for any purpose we'd like. The first question we need to consider is the order in which we'd like to find the nodes. In some cases, we don't care; any order will do. In others, the data is structured in a particular way - the parent/child relationship has a particular meaning - and we want to visit it in an order reflected by that structure. So, generally, we should consider the available options, understand the tradeoffs inherent in each, and then we'll be able to make a practical choice given a real problem we need solved. While there are lots of ways to traverse a tree, they fall broadly into two categories: breadth-first and depth-first. The distinction, stated simply, is between the idea of working our way across before working our way downward (that's "breadth-first") and the idea of working our way downward as far as that takes us before working our way across (that's "depth-first"). As we proceed with our discussion, we'll use the same tree we saw in the previous Notes and Examples page duplicated below. X F D C N R S M) H L
Step 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