ll pointers that at least halt tree can be o cldrto traverse the has exactly n l
ID: 2259955 • Letter: L
Question
ll pointers that at least halt tree can be o cldrto traverse the has exactly n leaves Whatup constant That only one child?the significance of the restriction in part c on the number of internal vertices that h, Ex. 2.8 Trees stfof the unternal vertices in the tree have at least two children, and that the tr up to a constant lactor-is the number of operations needed to traverse unt is, efficiently? d Why is the complexity a function of the number of leaves? Let T b in T e an arbitrary tree where each vertex a in T has a field e.numb. Present an algorithm that for each stores in e.numb the number of leaves the subtree rooted by e. If v is a leaf, this number should be one ggestion: use pseudocode such as if e is a leaf then . Ex. 2.9 Trees Let T be an arbitrary tree where each vertex v in T has a field e dis. Present an algorithm that for each vin , stores in e dis the distance (Le, number of edges) from e to the most distant leaf in the subtree rooted by e. If u is a leaf this distance should be zero. 2.10 Trees Let T be an arbitrary tree where each vertex e in T has the felds v.Idis and v.2dis. Present an algorithm that for each e in T. stores in v.Idis the distance (e, number of edges) from v to the most distant leaf in the subtree rooted by . If is a leaf, this distance Should be zero. Suppose the path from u to this most distant leaf goes from v through the vertex : that is a child of v and then continues down the subtree rooted tg to the t leal Fhe vatue for e.dis2 ts the distance to the most distant leaf that is in the subtree rooted by but no in the subtree rooted by the :. You can think of this v.dis2 and the associated leaf as a backup in case z gets sick and has to go home. If u has just one child, then e.dis2 should be -nhale both beloeExplanation / Answer
2.30
In adjesency, list representation graph has contained sequence of edges in order in which format we have entered in the first time so we have to store them in sequence order and move them in sequence order.if we check wether edge is present it is represented as 1 otherwise it is 0
pseudo code representation dfs traversal order in reverse format
public void RecursivePostorder(AdjGraph g, AdjGraph.Node v, Callback cb) {
if (v.isVisited()) // check wether it is visited or not if it is visited return it
return;
v.markVisited(); // else mark it as not visited
List<AdjGraph.Node> e = graph.edges(v); // creation of list which contain edges
for (AdjGraph.Node node : e) { // traversal sarts from here for every edge
if (!node.isVisited()) // if it is not visited and also check wether edge is present or not
RecursivePostOrder (graph, node, callback); // goto node which is not yet visited until last node
}
cb.nodeVisited(graph, v); //post order call to callback // from last node call back the method for reverse order
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.