-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.c
More file actions
81 lines (73 loc) · 1.95 KB
/
Copy pathbinarytree.c
File metadata and controls
81 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left, *right;
};
struct node *creatnode(int value){
struct node *newnode=malloc(sizeof(struct node));
newnode->data=value;
newnode->left=NULL;
newnode->right=NULL;
return newnode;
}
struct node* insert(struct node *root,int value){
if(root==NULL) return creatnode(value);
if(root->data>value) root->left=insert(root->left,value);
else root->right=insert(root->right,value);
return root;
}
void inorder(struct node *root){
if(root==NULL) return;
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
struct node *minvaluenode(struct node *root){
struct node *current=root;
while(current && current->left!=NULL){
current=current->left;
}
return current;
}
struct node *deletnode(struct node *root,int key){
if(root==NULL) return root;
if(key<root->data){
root->left = deletnode(root->left,key);
}
else if(key>root->data){
root->right= deletnode(root->right,key);
}
else{
if(root->left==NULL){
struct node *temp=root->right;
free(root);
return temp;
}
else if(root->right==NULL){
struct node *temp= root->left;
free(root);
return temp;
}
struct node *temp=minvaluenode(root->right);
root->data=temp->data;
root->right=deletnode(root->right,temp->data);
}
return root;
}
int main(){
struct node *root=NULL;
root=insert(root,8);
root=insert(root,3);
root=insert(root,1);
root=insert(root,6);
root=insert(root,7);
root=insert(root,10);
root=insert(root,14);
root=insert(root,4);
printf("\ninorder traversal : \n");
inorder(root);
printf("\nafter delet 3 : \n");
deletnode(root,3);
inorder(root);
}