Given a Prufer sequence of a Tree, the task is to print the nodes with prime-degree in this tree.
Examples:
Input: arr[] = {4, 1, 3, 4}
Output: 1 3 4
Explanation:
The tree is:
2----4----3----1----5
|
6
Hence, the degree of 1, 3 and 4
are 2, 2 and 3 respectively
which are prime.
Input: a[] = {1, 2, 2}
Output: 1 2
Approach:
- Since the length of prufer sequence is N – 2 if N is the number of nodes. Therefore create an array degree[] of size 2 more than the length of the Prufer sequence.
- Initially, fill the degree array with 1.
- Iterate in the Prufer sequence and increase the frequency in the degree table for every element. This method works because the frequency of a node in the Prufer sequence is one less than the degree in the tree.
- Further, to check if the node degree is prime or not, we will use Sieve Of eratosthenes. Create a sieve which will help us to identify if the degree is prime or not in O(1) time.
- If a node has a prime degree, then print the node number.
Below is the implementation of the above approach:
C++
// C++ implementation to print the // nodes with prime degree from the // given prufer sequence #include <bits/stdc++.h> using namespace std; // Function to create Sieve // to check primes void SieveOfEratosthenes( bool prime[], int p_size) { // False here indicates // that it is not prime prime[0] = false; prime[1] = false; for (int p = 2; p * p <= p_size; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p]) { // Update all multiples of p, // set them to non-prime for (int i = p * 2; i <= p_size; i += p) prime[i] = false; } } } // Function to print the nodes with // prime degree in the tree // whose Prufer sequence is given void PrimeDegreeNodes(int prufer[], int n) { int nodes = n + 2; bool prime[nodes + 1]; memset(prime, true, sizeof(prime)); SieveOfEratosthenes(prime, nodes + 1); // Hash-table to mark the // degree of every node int degree[n + 2 + 1]; // Initially let all the degrees be 1 for (int i = 1; i <= nodes; i++) degree[i] = 1; // Increase the count of the degree for (int i = 0; i < n; i++) degree[prufer[i]]++; // Print the nodes with prime degree for (int i = 1; i <= nodes; i++) { if (prime[degree[i]]) { cout << i << " "; } } } // Driver Code int main() { int a[] = { 4, 1, 3, 4 }; int n = sizeof(a) / sizeof(a[0]); PrimeDegreeNodes(a, n); return 0; } |
chevron_right
filter_none
Java
// Java implementation to print the // nodes with prime degree from the // given prufer sequence import java.util.*; class GFG{ // Function to create Sieve // to check primes static void SieveOfEratosthenes( boolean prime[], int p_size) { // False here indicates // that it is not prime prime[0] = false; prime[1] = false; for (int p = 2; p * p <= p_size; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p]) { // Update all multiples of p, // set them to non-prime for (int i = p * 2; i <= p_size; i += p) prime[i] = false; } } } // Function to print the nodes with // prime degree in the tree // whose Prufer sequence is given static void PrimeDegreeNodes(int prufer[], int n) { int nodes = n + 2; boolean []prime = new boolean[nodes + 1]; Arrays.fill(prime, true); SieveOfEratosthenes(prime, nodes + 1); // Hash-table to mark the // degree of every node int []degree = new int[n + 2 + 1]; // Initially let all the degrees be 1 for (int i = 1; i <= nodes; i++) degree[i] = 1; // Increase the count of the degree for (int i = 0; i < n; i++) degree[prufer[i]]++; // Print the nodes with prime degree for (int i = 1; i <= nodes; i++) { if (prime[degree[i]]) { System.out.print(i+ " "); } } } // Driver Code public static void main(String[] args) { int a[] = { 4, 1, 3, 4 }; int n = a.length; PrimeDegreeNodes(a, n); } } // This code contributed by Princi Singh |
chevron_right
filter_none
Python3
# Python3 implementation to print the # nodes with prime degree from the # given prufer sequence # Function to create Sieve # to check primes def SieveOfEratosthenes(prime, p_size): # False here indicates # that it is not prime prime[0] = False prime[1] = False p = 2 while (p * p <= p_size): # If prime[p] is not changed, # then it is a prime if (prime[p]): # Update all multiples of p, # set them to non-prime for i in range(p * 2, p_size + 1, p): prime[i] = False p += 1 # Function to print the nodes with # prime degree in the tree # whose Prufer sequence is given def PrimeDegreeNodes(prufer, n): nodes = n + 2 prime = [True] * (nodes + 1) SieveOfEratosthenes(prime, nodes + 1) # Hash-table to mark the # degree of every node degree = [0] * (n + 2 + 1); # Initially let all the degrees be 1 for i in range(1, nodes + 1): degree[i] = 1; # Increase the count of the degree for i in range(0, n): degree[prufer[i]] += 1 # Print the nodes with prime degree for i in range(1, nodes + 1): if prime[degree[i]]: print(i, end = ' ') # Driver Code if __name__=='__main__': a = [ 4, 1, 3, 4 ] n = len(a) PrimeDegreeNodes(a, n) # This code is contributed by rutvik_56 |
chevron_right
filter_none
C#
// C# implementation to print the // nodes with prime degree from the // given prufer sequence using System; class GFG{ // Function to create Sieve // to check primes static void SieveOfEratosthenes(bool []prime, int p_size) { // False here indicates // that it is not prime prime[0] = false; prime[1] = false; for(int p = 2; p * p <= p_size; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p]) { // Update all multiples of p, // set them to non-prime for(int i = p * 2; i <= p_size; i += p) prime[i] = false; } } } // Function to print the nodes with // prime degree in the tree // whose Prufer sequence is given static void PrimeDegreeNodes(int []prufer, int n) { int nodes = n + 2; bool []prime = new bool[nodes + 1]; for(int i = 0; i < prime.Length; i++) prime[i] = true; SieveOfEratosthenes(prime, nodes + 1); // Hash-table to mark the // degree of every node int []degree = new int[n + 2 + 1]; // Initially let all the degrees be 1 for(int i = 1; i <= nodes; i++) degree[i] = 1; // Increase the count of the degree for(int i = 0; i < n; i++) degree[prufer[i]]++; // Print the nodes with prime degree for(int i = 1; i <= nodes; i++) { if (prime[degree[i]]) { Console.Write(i + " "); } } } // Driver Code public static void Main(String[] args) { int []a = { 4, 1, 3, 4 }; int n = a.Length; PrimeDegreeNodes(a, n); } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output:
1 3 4
Recommended Posts:
- Print the degree of every node from the given Prufer sequence
- Print the node with the maximum degree in the prufer sequence
- Random Tree Generator Using Prüfer Sequence with Examples
- Check if the given Prufer sequence is valid or not
- Nodes with prime degree in an undirected Graph
- Count of all prime weight nodes between given nodes in the given Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Prufer Code to Tree Creation
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Print the nodes of the Binary Tree whose height is a Prime number
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Make a tree with n vertices , d diameter and at most vertex degree k
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Count the nodes in the given tree whose weight is prime
- Print nodes between two given level numbers of a binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print all nodes between two given levels in Binary Tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


