📜  打印prufer序列中度数最大的节点

📅  最后修改于: 2021-10-25 05:06:09             🧑  作者: Mango

给定树的普鲁弗序列,任务是打印给定普鲁弗序列的树中度数最大的节点。如果有很多度数最大的节点,则打印编号最小的节点。
例子:

Input: a[] = {4, 1, 3, 4} 
Output: 4
The tree is:
2----4----3----1----5
     |
     6 

Input: a[] = {1, 2, 2} 
Output: 2

一种简单的方法是使用 Prufer 序列创建树,然后找到所有节点的度数,然后找到其中的最大值。
有效的方法:创建一个大小为 2 的degree[]数组,该数组的长度大于 Prufer 序列的长度,因为如果N是节点数,则 prufer 序列的长度为N – 2 。最初,用1填充度数组。在 Prufer 序列中迭代并增加每个元素在度表中的频率。这种方法有效是因为 Prufer 序列中节点的频率比树中的度数小 1。现在在度数组中迭代并找到具有最大频率的节点,这将是我们的答案。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the node with
// the maximum degree in the tree
// whose Prufer sequence is given
int findMaxDegreeNode(int prufer[], int n)
{
    int nodes = n + 2;
 
    // 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]]++;
 
    int maxDegree = 0;
    int node = 0;
 
    // Find the node with maximum degree
    for (int i = 1; i <= nodes; i++) {
        if (degree[i] > maxDegree) {
            maxDegree = degree[i];
            node = i;
        }
    }
 
    return node;
}
 
// Driver code
int main()
{
    int a[] = { 1, 2, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << findMaxDegreeNode(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
         
    // Function to return the node with
    // the maximum degree in the tree
    // whose Prufer sequence is given
    static int findMaxDegreeNode(int prufer[], int n)
    {
        int nodes = n + 2;
     
        // 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]]++;
     
        int maxDegree = 0;
        int node = 0;
     
        // Find the node with maximum degree
        for (int i = 1; i <= nodes; i++)
        {
            if (degree[i] > maxDegree)
            {
                maxDegree = degree[i];
                node = i;
            }
        }
     
        return node;
    }
     
    // Driver code
    public static void main (String[] args)
    {
 
        int []a = { 1, 2, 2 };
        int n = a.length;
        System.out.println(findMaxDegreeNode(a, n));
    }
}
 
// This code is contributed by ajit_00023


Python3
# Python implementation of the approach
 
# Function to return the node with
# the maximum degree in the tree
# whose Prufer sequence is given
def findMaxDegreeNode(prufer, n):
    nodes = n + 2;
  
    # 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(n):
        degree[prufer[i]]+=1;
  
    maxDegree = 0;
    node = 0;
  
    # Find the node with maximum degree
    for i in range(1,nodes+1):
        if (degree[i] > maxDegree):
            maxDegree = degree[i];
            node = i;
 
    return node;
 
  
# Driver code
a = [ 1, 2, 2 ];
n = len(a);
print(findMaxDegreeNode(a, n));
 
# This code has been contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the node with
    // the maximum degree in the tree
    // whose Prufer sequence is given
    static int findMaxDegreeNode(int []prufer, int n)
    {
        int nodes = n + 2;
     
        // 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]]++;
     
        int maxDegree = 0;
        int node = 0;
     
        // Find the node with maximum degree
        for (int i = 1; i <= nodes; i++)
        {
            if (degree[i] > maxDegree)
            {
                maxDegree = degree[i];
                node = i;
            }
        }
     
        return node;
    }
     
    // Driver code
    static public void Main ()
    {
        int []a = { 1, 2, 2 };
        int n = a.Length;
         
        Console.WriteLine(findMaxDegreeNode(a, n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:
2

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程