Creating a tree with Left | Creating new Node ; Adds a sibling to a list with starting with n ; Add child Node to a Node ; Check if child is not empty . ; Traverses tree in depth first order ; Driver code
class NodeTemp { constructor ( data ) { this . data = data ; this . next = this . child = null ; } } function addSibling ( node , data ) { if ( node == null ) return null ; while ( node . next != null ) node = node . next ; return ( node . next = new NodeTemp ( data ) ) ; } function addChild ( node , data ) { if ( node == null ) return null ; if ( node . child != null ) return ( addSibling ( node . child , data ) ) ; else return ( node . child = new NodeTemp ( data ) ) ; } function traverseTree ( root ) { if ( root == null ) return ; while ( root != null ) { document . write ( root . data + " " ) ; if ( root . child != null ) traverseTree ( root . child ) ; root = root . next ; } } let root = new NodeTemp ( 10 ) ; let n1 = addChild ( root , 2 ) ; let n2 = addChild ( root , 3 ) ; let n3 = addChild ( root , 4 ) ; let n4 = addChild ( n3 , 6 ) ; let n5 = addChild ( root , 5 ) ; let n6 = addChild ( n5 , 7 ) ; let n7 = addChild ( n5 , 8 ) ; let n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ;