0% found this document useful (0 votes)
75 views

Assignments On Trees

The document contains code to implement various tree traversal algorithms like preorder, inorder and postorder on binary trees. It includes functions to create and insert nodes into a binary tree from an array. It also contains code to check if all leaves of a binary tree are at the same level by performing a recursive traversal.

Uploaded by

Rishabh Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Assignments On Trees

The document contains code to implement various tree traversal algorithms like preorder, inorder and postorder on binary trees. It includes functions to create and insert nodes into a binary tree from an array. It also contains code to check if all leaves of a binary tree are at the same level by performing a recursive traversal.

Uploaded by

Rishabh Agrawal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

ASSIGNMENT ON TREES

Rohit Raj Anand

20175139

ECE-3

1)GIVEN AN ARRAY, WAP TO IMPLEMENT A BINARY TREE

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *left, *right;};

struct node *newnode(int data){

struct node *p=(struct node *)malloc(sizeof(struct node));

p->data=data;

p->left=p->right=NULL;

return p;

struct node *insertlevelorder(int arr[], struct node *root, int i, int n){

if(i<n)

struct node *temp=newnode(arr[i]);

root=temp;

root->left=insertlevelorder(arr, root->left, 2*i+1, n);

root->right=insertlevelorder(arr, root->right, 2*i+2, n);

return root;

}
void inorder(struct node *root)

if(root!=NULL)

inorder(root->left);

printf("%d", root->data);

inorder(root->right);

}}

int main()

int i,n;

printf("enter no. of elements of array");

scanf("%d", &n);

int arr[n];

printf("enter elements of array");

for(i=0;i<n;i++){

scanf("%d", &arr[i]);}

struct node *root=insertlevelorder(arr,root,0,n);

inorder(root);

Q) WAP TO CHECK IF ALL LEAVES ARE AT SAME LEVEL

#include <stdio.h>

#include <stdlib.h>

struct Node

{
int data;

struct Node *left, *right;

};

struct Node* newNode(int data)

struct Node* node = (struct Node*) malloc(sizeof(struct Node));

node->data = data;

node->left = node->right = NULL;

return node;

bool checkUtil(struct Node *root, int level, int *leafLevel)

// Base case

if (root == NULL) return true;

// If a leaf node is encountered

if (root->left == NULL && root->right == NULL)

// When a leaf node is found first time

if (*leafLevel == 0)

*leafLevel = level; // Set first found leaf's level

return true;

return (level == *leafLevel);

return checkUtil(root->left, level+1, leafLevel) && checkUtil(root->right, level+1, leafLevel);

}
bool check(struct Node *root)

int level = 0, leafLevel = 0;

return checkUtil(root, level, &leafLevel);

// Driver program to test above function

int main()

// Let us create tree shown in thirdt example

struct Node *root = newNode(12);

root->left = newNode(5);

root->left->left = newNode(3);

root->left->right = newNode(9);

root->left->left->left = newNode(1);

root->left->right->left = newNode(1);

if (check(root))

printf("Leaves are at same level\n");

else

printf("Leaves are not at same level\n");

getchar();

return 0;

Q) PRE-ORDER TRAVERSAL

#include<stdio.h>

#include<stdlib.h>
struct node{

int data;

struct node *left, *right;};

struct node *newnode(int data){

struct node *p=(struct node *)malloc(sizeof(struct node));

p->data=data;

p->left=p->right=NULL;

return p;

struct node *insertlevelorder(int arr[], struct node *root, int i, int n){

if(i<n)

struct node *temp=newnode(arr[i]);

root=temp;

root->left=insertlevelorder(arr, root->left, 2*i+1, n);

root->right=insertlevelorder(arr, root->right, 2*i+2, n);

return root;

void preorder(struct node *root)

if(root!=NULL)

printf("%d", root->data);

preorder(root->left);

preorder(root->right);

}}

int main()
{

int i,n;

printf("enter no. of elements of array");

scanf("%d", &n);

int arr[n];

printf("enter elements of array");

for(i=0;i<n;i++){

scanf("%d", &arr[i]);}

struct node *root=insertlevelorder(arr,root,0,n);

preorder(root);

Q) PRE-ORDER TRAVERSAL

#include<stdio.h>

#include<stdlib.h>

struct node{

int data;

struct node *left, *right;};

struct node *newnode(int data){

struct node *p=(struct node *)malloc(sizeof(struct node));

p->data=data;

p->left=p->right=NULL;

return p;

struct node *insertlevelorder(int arr[], struct node *root, int i, int n){

if(i<n)

{
struct node *temp=newnode(arr[i]);

root=temp;

root->left=insertlevelorder(arr, root->left, 2*i+1, n);

root->right=insertlevelorder(arr, root->right, 2*i+2, n);

return root;

void postorder(struct node *root)

if(root!=NULL)

printf("%d", root->data);

postorder(root->right);

postorder(root->left);

}}

int main()

int i,n;

printf("enter no. of elements of array");

scanf("%d", &n);

int arr[n];

printf("enter elements of array");

for(i=0;i<n;i++){

scanf("%d", &arr[i]);}

struct node *root=insertlevelorder(arr,root,0,n);

postorder(root);}

You might also like