Prufer Code to Tree Creation | Prints edges of tree represented by give Prufer code ; Initialize the array of vertices ; Number of occurrences of vertex in code ; Find the smallest label not present in prufer . ; If j + 1 is not present in prufer set ; Remove from Prufer set and print pair . ; For the last element ; Driver code
def printTreeEdges ( prufer , m ) : NEW_LINE INDENT vertices = m + 2 NEW_LINE vertex_set = [ 0 ] * vertices NEW_LINE for i in range ( vertices - 2 ) : NEW_LINE INDENT vertex_set [ prufer [ i ] - 1 ] += 1 NEW_LINE DEDENT print ( " The ▁ edge ▁ set ▁ E ( G ) ▁ is ▁ : " ) NEW_LINE j = 0 NEW_LINE for i in range ( vertices - 2 ) : NEW_LINE INDENT for j in range ( vertices ) : NEW_LINE INDENT if ( vertex_set [ j ] == 0 ) : NEW_LINE INDENT vertex_set [ j ] = - 1 NEW_LINE print ( " ( " , ( j + 1 ) , " , ▁ " , prufer [ i ] , " ) ▁ " , sep = " " , end = " " ) NEW_LINE vertex_set [ prufer [ i ] - 1 ] -= 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT j = 0 NEW_LINE for i in range ( vertices ) : NEW_LINE INDENT if ( vertex_set [ i ] == 0 and j == 0 ) : NEW_LINE INDENT print ( " ( " , ( i + 1 ) , " , ▁ " , sep = " " , end = " " ) NEW_LINE j += 1 NEW_LINE DEDENT elif ( vertex_set [ i ] == 0 and j == 1 ) : NEW_LINE INDENT print ( ( i + 1 ) , " ) " ) NEW_LINE DEDENT DEDENT DEDENT prufer = [ 4 , 1 , 3 , 4 ] NEW_LINE n = len ( prufer ) NEW_LINE printTreeEdges ( prufer , n ) NEW_LINE