Answered step by step
Verified Expert Solution
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 and the roots of its left and right subtrees contain the values and respectively. In this example, the nodes with values and do not have richt subtrees their right subtrees s are empty. The nodes containing values and 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 solutiontree T int leafid:that, given a binary tree T consisting of N nodes and an integer leafid returns the rerooted tree T in which node leafid is the root.Write an efficient algorithm for the following assumptions: N is an integer within the range ; T is a correct binary tree data structure; each ID in tree T is an integer within the range N no two nodes have equal IDs; node with ID equal to leafid is a leaf in tree T
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