linked representation of binary tree

PHOTO EMBED

Fri Jun 17 2022 09:37:46 GMT+0000 (Coordinated Universal Time)

Saved by @KanishqJ8

#include <stdio.h>
#include <malloc.h>

struct node{
    int data;
    struct node*left;
    struct node*right;
    
};


struct node *createNode(int data){
    struct node*n;//creating a node pointer 
    n=(struct node*)malloc(sizeof(struct node));//allocating the memory in the heap
    n->data=data;//setting the data
    n->left=NULL;
    n->right=NULL;
    return n;
}
int main() {
    
    //constructing the root node using function
    struct node *p=createNode(2);
    struct node *p1=createNode(1);
    struct node *p2=createNode(4);
    
    
    /*
    //consutructing the first (root) node
    struct node *p;
    p=(struct node*)malloc(sizeof(struct node));
    p->left=NULL;
    p->right=NULL;
    
    //constructing the second node
    struct node *p1;
    p1=(struct node*)malloc(sizeof(struct node));
    p1->left=NULL;
    p1->right=NULL;
    
     //constructing the second node
    struct node *p2;
    p2=(struct node*)malloc(sizeof(struct node));
    p2->left=NULL;
    p2->right=NULL;  */
    
    //linking the root node with left and right children
    p->left=p1;
    p->right=p2;
    
    return 0;
}
content_copyCOPY