Print siblings of a given Node in N | Structure of a node of N - ary tree ; Function to create a new node ; Function to find the siblings of the node / value ; Stores nodes level wise ; Push the root ; Continue until all levels are traversed ; Stores current node ; Enqueue all children of the current node ; If node value is found ; Print all children of current node except value as the answer ; Push the child nodes of temp into the queue ; Stores root of the constructed tree ; Print siblings of Node X
class Node { constructor ( key ) { this . child = [ ] ; this . key = key ; } } function newNode ( key ) { let temp = new Node ( key ) ; return temp ; } function Siblings ( root , value ) { let flag = 0 ; if ( root == null ) return ; let q = [ ] ; q . push ( root ) ; while ( q . length > 0 ) { let temp = q [ 0 ] ; q . shift ( ) ; for ( let i = 0 ; i < temp . child . length ; i ++ ) { if ( temp . child [ i ] . key == value ) { flag = 1 ; for ( let j = 0 ; j < temp . child . length ; j ++ ) { if ( value != temp . child [ j ] . key ) document . write ( temp . child [ j ] . key + " " ) ; } break ; } q . push ( temp . child [ i ] ) ; } } if ( flag == 0 ) document . write ( " " ) ; } function constructTree ( ) { let root = newNode ( 10 ) ; ( root . child ) . push ( newNode ( 20 ) ) ; ( root . child ) . push ( newNode ( 30 ) ) ; ( root . child ) . push ( newNode ( 40 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 50 ) ) ; ( root . child [ 0 ] . child ) . push ( newNode ( 60 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 70 ) ) ; ( root . child [ 1 ] . child ) . push ( newNode ( 80 ) ) ; ( root . child [ 2 ] . child ) . push ( newNode ( 90 ) ) ; ( root . child [ 2 ] . child ) . push ( newNode ( 100 ) ) ; ( root . child [ 2 ] . child ) . push ( newNode ( 110 ) ) ; return root ; } let root = constructTree ( ) ; let X = 30 ; Siblings ( root , X ) ;