-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree-Traverse-NodeLevel.c
More file actions
88 lines (78 loc) · 1.33 KB
/
Tree-Traverse-NodeLevel.c
File metadata and controls
88 lines (78 loc) · 1.33 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
#include<stdio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *left,*right;
};
typedef struct tree node;
int level=1;
node* insert(node *root,int data)
{
if(root==NULL)
{
node *temp=(node*)malloc(sizeof(node));
temp->data=data;
temp->left=NULL;
temp->right=NULL;
return temp;
}
if(root->data>data)
{
root->left=insert(root->left,data);
return root;
}
root->right=insert(root->right,data);
return root;
}
void traverse(node *root)
{
if(root==NULL)
return;
traverse(root->right);
printf("%d\n",root->data);
traverse(root->left);
}
void search(node *root,int data)
{
if(root->data==data && root!=NULL)
{
printf("Aradiginiz sayi %d. seviyede bulundu.%d",level,root->data);
}
if(data>root->data && root->right!=NULL)
{
level++;
search(root->right,data);
}
if(data<root->data&& root->left!=NULL)
{
level++;
search(root->left,data);
}
return;
}
int main()
{
node *root=NULL;
root=insert(root,10);
root=insert(root,7);
root=insert(root,13);
root=insert(root,40);
root=insert(root,46);
root=insert(root,64);
root=insert(root,12);
root=insert(root,39);
root=insert(root,41);
root=insert(root,24);
traverse(root);
printf("Aramak istediðiniz sayýyý girin.");
int aranan;
scanf("%d",&aranan);
while(aranan!=0)
{
search(root,aranan);
scanf("%d",&aranan);
level=1;
}
return 0;
}