Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions tree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
#include<stdio.h>

#include<stdlib.h>

#include<ctype.h>

struct tree

{

int data;

struct tree *left;

struct tree *right;

}*head;

void insert(int a)

{

int count=0;

struct tree *temp,*ptr,*prev;

temp=(struct tree *)malloc(sizeof(struct tree));

temp->data=a;

temp->left=NULL;

temp->right=NULL;

if(head==NULL)

{

head=temp;

}

else

{

ptr=head;

while(ptr!=NULL)

{

prev=ptr;

if(ptr->left!=NULL)

{

if(ptr->right==NULL)

{

ptr->right=temp;

printf("\n%d inserted",a);

return;
}

else

{

if(count==0)

{

count=1;

ptr=ptr->left;

}

else

{

count=0;

ptr=prev->right;

}

}

}

else

{

ptr->left=temp;

printf("\n%d inserted",a);

return;

}

}

}

printf("\n%d inserted",a);

}

void print(struct tree *p)

{
if(p==NULL)return;

else

{

if(p->data!=-1)

{

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

}

print(p->left);

print(p->right);

}

}

void delete(int a,struct tree *p)

{

if(p==NULL)
return;

else

{

if(p->data==a)

{

p->data=-1;

printf("\n%d deleted",a);
return;

}

delete(a,p->left);

delete(a,p->right);

}

}

int main()

{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the interface as generic as possible by taking inputs from stdin. And do test your code for large inputs, for example, 10000 elements.

insert(1);

insert(2);

insert(3);

insert(4);

insert(5);

insert(6);

insert(7);

printf("\n");

print(head);

delete(1,head);

delete(3,head);

printf("\n");

print(head);

return 0;

}