From 377cbad6813ad614f84c0a6d06591b3e7d2a58fb Mon Sep 17 00:00:00 2001 From: SumangalaRao Date: Tue, 25 Dec 2018 16:18:01 +0530 Subject: [PATCH] codeforinsertiondeletion --- tree.c | 206 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 tree.c diff --git a/tree.c b/tree.c new file mode 100644 index 0000000..3147f73 --- /dev/null +++ b/tree.c @@ -0,0 +1,206 @@ +#include + +#include + +#include + +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() + +{ + + 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; + +} +