-
Notifications
You must be signed in to change notification settings - Fork 0
/
75_search_itr_bst.c
76 lines (72 loc) · 1.87 KB
/
75_search_itr_bst.c
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
#include<stdio.h>
#include<stdlib.h>
// searching through the elements with loop instead of using recursion
struct node{
int data;
struct node* right;
struct node* left;
};
void inorder(struct node* root){ //left -> root-> right
if (root!=NULL){
inorder(root->left); //recursive function
printf("the element is %d \n",root->data);
inorder(root->right);
}
}
struct node *searchiter(struct node *root,int key,int *level1){
while(root!=NULL){
*level1 = *level1+1;
if (key == root->data){
return root;
}
else if (key< root->data){
root = root->left;
}
else if (key> root->data){
root = root->right;
}
}
return NULL;
}
struct node* createNode(int data){ // to create a node (the node is not linked to any other node for now)
struct node * n;
n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
int main(){
//constructing the root node - using function
//binary tree building
struct node *p = createNode(5);
struct node *p1 = createNode(3);
struct node *p2 = createNode(6);
struct node *p3 = createNode(2);
struct node *p4 = createNode(4);
p->left = p1;
p->right =p2;
p1->left = p3;
p1->right = p4;
int level1 =0;
int *ptr ;
ptr = &level1;
//binary searchiter tree:-
// 5
// / \
// 3 6
// / \
// 2 4
// finished building the binary tree
inorder(p);//just an traversal through the elements
struct node *t =searchiter(p,6,ptr); //passing level throu call by reference
printf("Serching begins -- \n");
if (t == NULL){
printf("not found \n");
}
else{
printf("the searched element is %d\n",t->data);
printf("At the level %d \n", level1);
}
return 0;
}