Found 33676 Articles for Programming

Count pairs in a binary tree whose sum is equal to a given value x in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:55:14

227 Views

We are given an integer value and a variable x and the task is to construct the binary tree and find the pairs having sum equals to the given value x.For ExampleInputint x = 5, The tree which will be created after inputting the values is given below −OutputCount of pairs in a binary tree whose sum is equal to a given value x are: 2Explanationwe are given with an array of integer values that is used to form a binary tree and we will check whether there is a pair present in a binary tree whose sum equals to ... Read More

Construct the full k-ary tree from its preorder traversal in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:44:20

456 Views

We are given an array arr[] containing the preorder traversal of the k-ary tree in sequence. The goal is to construct the same k-ary tree from it and print its postorder traversal. A full k−ary tree is the one in which the root node has 0 or k children i.e. at most k child.For ExampleInputint arr[] = {2, 5, 1, 3, 6, 7, 2, 1 }, int size = 8, int children = 2OutputThe full k−ary tree which will be constructed with the two children from preorder traversal is given below −Explanationwe are given with an array of integer values ... Read More

Count the nodes whose sum with X is a Fibonacci number in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:37:55

155 Views

Given a binary tree with weights of its nodes as numbers. The goal is to find the number of nodes that have weights such that the number is a Fibonacci number. Numbers in Fibonacci series are: 0, 1, 1, 2, 3, 5, 8, 13….nth number is the sum of (n−1)th and (n−2)th. If weight is 13 then it is a Fibonacci number so the node will be counted.For ExampleInputtemp =1. The tree which will be created after inputting the values is given below −OutputCount the nodes whose sum with X is a Fibonacci number are: 3Explanationwe are given with the ... Read More

Construct Pushdown automata for L = {a(2*m)c(4*n)dnbm | m,n = 0} in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:33:49

734 Views

We are given with a language “L” and the task is to construct a pushdown automata for the given language which explains that the occurrences of character ‘a’ should be doubles the time of occurrence of character ‘b’ and occurrences of character ‘c’ should be quadruples the times of ‘d’ and also occurrences of all the characters should be minimum 1 which can also makes the string NULL and it should be accepted by the automata.What is pushdown Automata?A pushdown automata or pushdown automaton or PDA is a technique to implement a context-free grammar in a similar way we design ... Read More

Construct Pushdown automata for L = {0n1m2m3n | m,n = 0} in C++

Sunidhi Bansal
Updated on 07-Jan-2021 06:32:01

808 Views

We are given with a language “L” and the task is to construct a pushdown automata for the given language which explains that the occurrences of 0’s and 3’s will be equal and occurrences of 1’s and 2’s will be equal and also occurrences of all the numbers should be minimum 1 which can also makes the string NULL and it should be accepted by the automata.What is pushdown Automata?A pushdown automata or pushdown automaton or PDA is a technique to implement a context−free grammar in a similar way we design Deterministic Finite Automaton or DFA for a regular grammar. ... Read More

Construct Pushdown automata for L = {0m1(n+m)2n | m,n = 0} in C++

Sunidhi Bansal
Updated on 07-Jan-2021 05:27:09

593 Views

We are given with a language “L” and the task is to construct a pushdown automata for the given language which explains that the occurrences of 1’s will be the addition of occurrences of 0’s and 2’s and also, occurrence of 0 and 2 will be minimum one which can also makes the string NULL and it should be accepted by the automata.What is pushdown Automata?A pushdown automata or pushdown automaton or PDA is a technique to implement a context−free grammar in a similar way we design Deterministic Finite Automaton or DFA for a regular grammar. A DFA can operate ... Read More

Construct Pushdown automata for L = {0(n+m)1m2n | m, n = 0} in C++

Sunidhi Bansal
Updated on 07-Jan-2021 05:24:22

504 Views

We are given with a language “L” and the task is to construct a pushdown automata for the given language which explains that the occurrences of 0’s will be the addition of occurrences of 1’s and 2’s and also, occurrence of 1 and 2 will be minimum one which can also makes the string NULL and it should be accepted by the automata.What is pushdown Automata?A pushdown automata or pushdown automaton or PDA is a technique to implement a context−free grammar in a similar way we design Deterministic Finite Automaton or DFA for a regular grammar. A DFA can operate ... Read More

Count the nodes of the tree whose weighted string contains a vowel in C++

Sunidhi Bansal
Updated on 05-Jan-2021 14:53:08

172 Views

Given a binary tree with weights of its nodes as strings. The goal is to find the number of nodes that have weights such that the string contains a vowel. If weight is ‘aer’ then it has vowels ‘a’ and ‘e’ so the node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes of the tree whose weighted string contains a vowel are: 5Explanationwe are given with the tree nodes and the string weights associated with each node. Now we check whether the string of nodes contains vowels or not.NodeWeightvowelsyes/no2aeeyes1bcdNo ... Read More

Count the nodes whose weight is a perfect square in C++

Sunidhi Bansal
Updated on 05-Jan-2021 14:57:18

145 Views

Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is a perfect square. If weight is 36 then it is 62 so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes whose weight is a perfect square are: 4Explanationwe are given with the tree nodes and the weights associated with each node. Now we check whether the digits of nodes are perfect squares or not.NodeWeightPerfect squareYes/no212111*11yes1819*9yes437Prime numberno3255*5yes810010*10yes9701Not possiblenoInputThe tree which will be ... Read More

Count the nodes in the given tree whose weight is a power of two in C++

Sunidhi Bansal
Updated on 05-Jan-2021 14:58:58

240 Views

Given a binary tree with weights of its nodes. The goal is to find the number of nodes that have weights such that the number is power of two. If weight is 32 then it is 25 so this node will be counted.For ExampleInputThe tree which will be created after inputting the values is given below −OutputCount the nodes in the given tree whose weight is a power of two are: 3Explanationwe are given with the tree node and the weights associated with each node. Now we calculate the power of each and every weight and check whether it can ... Read More

Advertisements