Creating a tree with Left | C # program to create a tree with left child right sibling representation . ; 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
using System ; class GFG { public class NodeTemp { public int data ; public NodeTemp next , child ; public NodeTemp ( int data ) { this . data = data ; next = child = null ; } } public static NodeTemp addSibling ( NodeTemp node , int data ) { if ( node == null ) { return null ; } while ( node . next != null ) { node = node . next ; } return ( node . next = new NodeTemp ( data ) ) ; } public static NodeTemp addChild ( NodeTemp node , int data ) { if ( node == null ) { return null ; } if ( node . child != null ) { return ( addSibling ( node . child , data ) ) ; } else { return ( node . child = new NodeTemp ( data ) ) ; } } public static void traverseTree ( NodeTemp root ) { if ( root == null ) { return ; } while ( root != null ) { Console . Write ( root . data + " ▁ " ) ; if ( root . child != null ) { traverseTree ( root . child ) ; } root = root . next ; } } public static void Main ( string [ ] args ) { NodeTemp root = new NodeTemp ( 10 ) ; NodeTemp n1 = addChild ( root , 2 ) ; NodeTemp n2 = addChild ( root , 3 ) ; NodeTemp n3 = addChild ( root , 4 ) ; NodeTemp n4 = addChild ( n3 , 6 ) ; NodeTemp n5 = addChild ( root , 5 ) ; NodeTemp n6 = addChild ( n5 , 7 ) ; NodeTemp n7 = addChild ( n5 , 8 ) ; NodeTemp n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; } }