-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathquestion9.c
More file actions
74 lines (64 loc) · 1.59 KB
/
question9.c
File metadata and controls
74 lines (64 loc) · 1.59 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
/*
Get the level of a given key in a binary tree
METHOD:
Traversal. Any traversal can be used. Simply do it and check it its present at the node else check for RST
and LST. Also keep track of the level by keeping another variable
Time complexity: O(n)
Space complexity: O(h) //worst case O(n) best case O(logn)
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right= NULL;
return temp;
}
int searchLevel(struct node *root,int data, int *level){
if(!root){
return 0;
}
*level = *level + 1;
if(root->data == data){
return *level;
}
int left = searchLevel(root->left, data, level);
int right = searchLevel(root->right, data, level);
*level = *level - 1;
return (left)?left:right;
}
int main(){
int step, elm, level = 0;
struct node *root = newNode(10);
root->left = newNode(12);
root->left->left = newNode(14);
root->left->right = newNode(16);
root->right = newNode(20);
root->right->left = newNode(22);
root->right->right = newNode(26);
while(1){
printf("1. Find element's level\n");
printf("2. exit\n");
scanf("%d",&step);
switch(step){
case 1: printf("enter the element to be searched\n");
scanf("%d",&elm);
level = searchLevel(root,elm, &level);
if(level > 0){
printf("level at which %d is present is %d\n", elm,level);
}else{
printf("element is not a part of the tree\n");
}
level = 0;
break;
case 2: exit(1);
break;
}
}
return 0;
}