-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.c
More file actions
140 lines (137 loc) · 2.97 KB
/
Tree.c
File metadata and controls
140 lines (137 loc) · 2.97 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node*left;
struct node*right;
};
struct node* createnode(int data){
struct node *n;
n = (struct node*)malloc(sizeof(struct node));
n->data = data;
n->left = NULL;
n->right =NULL;
return n;
}
int isBST(struct node* root){
static struct node* prev = NULL;
if(root != NULL){
if(!isBST(root->left)){
return 0;
}
if(prev != NULL && root->data <= prev->data){
return 0;
}
prev = root;
return isBST(root->right);
}
else{
return 1;
}
}
struct node* search(struct node* root , int key){
if(root == NULL){
return NULL;
}
if(key == root->data){
return root;
}
else if ( key<root->data){
return search(root->left, key);
}
else{
return search(root->right, key);
}
}
struct node* iterativesearch(struct node* root , int key){
while(root!=NULL){
if(key == root->data){
return root;
}
else if(root-> data > key){
return iterativesearch(root ->left, key);
}
else{
return iterativesearch(root->right, key);
}
}
}
void insertion(struct node* root, int val){
struct node*prev = NULL;
while(root!=NULL){
prev = root;
if(val == root->data){
return;
}
else if (val> root->data){
root = root->right ;
}
else {
root = root->left;
}
}
struct node* new = createnode(val);
if(val <prev ->data){
prev->left=new;
}
else {
prev->right =new;
}
}
struct node*inorderpredeccor(struct node*root){
root = root->left;
while(root->right != NULL){
root = root -> right;
}
}
struct node* deletion(struct node*root, int key){
struct node* ipre;
if(root == NULL){
return NULL;
}
if(root->left == NULL&&root->right == NULL){
free(root);
return NULL;
}
if(key < root->data){
root -> left = deletion(root -> left,key);
}
else if(key > root->data){
root -> left = deletion(root -> right,key);
}
else{
ipre = inorderpredeccor(root);
root->data=ipre->data;
root -> left = deletion(root->left,ipre->data);
}
return root;
}
int main (){
struct node*p= createnode(4);
struct node*p1= createnode(3);
struct node*p2= createnode(6);
struct node*p3= createnode(5);
struct node*p4= createnode(11);
p->left = p1;
p->right =p2;
p2->left = p3;
p2->right = p4;
insertion(p,1);
struct node * n = search(p,1);
if(n != NULL){
printf("Found %d it exists: ", n->data );
}
else{
printf("Element not found");
}
printf("\n");
deletion(p,1);
struct node * t = search(p,1);
if(t != NULL){
printf("Found %d it exists: ", t->data );
}
else{
printf("Element not found");
}
return 0;
}