Answered step by step
Verified Expert Solution
Question
1 Approved Answer
ThreeDigitTree Count the number of different three - digit numbers that can be created by traversing a tree. Task description The problem focuses on binary
ThreeDigitTree
Count the number of different threedigit numbers that can be created by traversing a tree.
Task description
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 null.
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 right subtrees their right subtrees are empty. The nodes containing values and are leaves their left and right subtrees are both empty.
Graphical representation of the example tree.
Assume that the following declarations are given:
Tree obj is an Object with attributes
obj.x type: int
obj.l type: Tree
obj.r type: Tree
class Tree
x: number;
l: Tree;
r: Tree;
constructorx: number l: Tree null, r: Tree null
this.x x;
this.l l;
this.r r;
An empty tree is represented by NULL. A nonempty tree is represented by an object representing its root. The attribute x holds the integer contained in the node, whereas attributes l and r hold the left and right subtrees, respectively.
In this problem, nodes of the tree contain digits. A threedigit number can be created in the following way:
choose some node x from the tree;
choose some node y which is the left or right subtree of x;
choose some node z which is the left or right subtree of y;
concatenate the digits contained in nodes x y z in that order to obtain a threedigit number.
In other words, you choose some node in the tree, move exactly two edges down the tree and concatenate the digits encountered on the way. For example, in the tree presented above, you can create number by starting in the node containing value and then going down to the left subtree twice:
Graphical representation of the coloured example tree..
Your task is to count the number of different threedigit numbers that can be created this way. Note that while some numbers may be able to be created in different ways, each number should be counted only once.
Write a function:
function solutionT: Tree: number;
that, given a binary tree T consisting of N nodes, returns the number of different threedigit numbers that can be created as described above.
Examples:
Given a tree T described in the example above which could also be denoted as:
None, None None None, None None, None None
the function should return You can create the following numbers:
Given a tree T:
Graphical representation of the second example test.
the function should return You can create the following numbers: Number can be created in three different ways but it should be counted only once in the answer.
Write an efficient algorithm for the following assumptions:
N is an integer within the range ;
each value in tree T is a digit within the range
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