Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The problem focuses on binary trees, represented by pointer data structures.A node of a binary tree contains a single digit and pointers to two other

The problem focuses on binary trees, represented by pointer data structures.A node of a binary tree contains a single digit and pointers to two other nodes, called the left subtree and the right subtree. If left subtree or right subtree does not exist, its corresponding pointer value is nullptr, For example, the figure below shows a binary tree consisting of seven nodes. Its root contains the value 1, and the roots of its left and right subtrees contain the values 2 and 7, respectively. In this example, the nodes with values 5 and 7 do not have richt subtrees - their right subtrees s are empty. The nodes containing values 3,4 and 9 are leaves - their left and right subtrees are both empty.Assume that the following declarations are given:struct tree {int x;tree * l;tree * r;};An empty tree is represented by nullptr. A non empty tree is represented by an object representing its root. The attribute x holds the integer contalned in the node, whereas attributes l and r hold the left and right subtrees, respectively.Notice that the binary tree data structure is correct if and only if each node is a subtree of exactly one other node, except for the root, which cannot be a subtree of any other node.Nodes X and Y are connected if node X points to node Y as its left or right subtree or if node Y points to node X as its left or right subtree.The binary tree can be rerooted by changing its root to another node, which is a leaf. The rerooted tree should maintain all the connections of the original tree - If nodes X, Y are connected in the original tree then nodes X, Y should remain connected in the rerooted tree.The order of subtrees in the rerooted tree is not significant. In other words, It is permissible to swap the left and right subtree for each node.Write a function:tree * solution(tree * T, int leaf_id):that, given a binary tree T consisting of N nodes and an integer leaf_id, returns the rerooted tree T, in which node leaf_id is the root.Write an efficient algorithm for the following assumptions: N is an integer within the range (250,000); T is a correct binary tree data structure; each ID in tree T is an integer within the range [1..N] no two nodes have equal IDs; node with ID equal to leaf_id is a leaf in tree T.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And SQL

Authors: Lucy Scott

3rd Edition

1087899699, 978-1087899695

More Books

Students also viewed these Databases questions