-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTreesUsingLinkedList.c
130 lines (121 loc) · 2.39 KB
/
TreesUsingLinkedList.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
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
#include<stdio.h>
#include<stdlib.h>
struct binTree
{
struct binTree *left;
int info;
struct binTree *right;
};
typedef struct binTree tree;
tree *root=NULL;
void insert(tree *root, tree *node)
{
char dir;
printf("l or r");
dir=getch();
if(dir=='l')
{
if(root->left==NULL)
root->left=node;
else
insert(root->left,node);
}
else if(dir=='r')
{
if(root->right==NULL)
root->right=node;
else
insert(root->right,node);
}
}
void create()
{
tree *node= (tree *) malloc(sizeof(tree));
printf("Enter data: ");
int data;
scanf("%d",&data);
node->left=NULL;
node->right=NULL;
node->info=data;
if(root==NULL)
root=node;
else
insert(root,node);
}
int search(int data, tree *node)
{
//inorder based
int temp;
if(node==NULL)
return 0;
temp=search(data,node->left);
if(node->info==data || temp==1) //we need to send the value to upper levels
return 1;
temp=search(data,node->right);
if(node->info==data || temp==1)
return 1;
return 0;
}
int height(tree *node)
{
if(node == NULL)
return 0;
else
{
int lheight=height(node->left);
int rheight=height(node->right);
if(lheight>rheight)
return lheight+1;
else
return rheight+1;
}
}
void printer(int i,tree *node)
{
if(node==NULL)
return;
if(i==0)
printf("%d ",node->info);
else
{
printer(i-1,node->left);
printer(i-1,node->right);
}
}
void levelordertraversal()
{
int h=height(root);
printf("Height=%d",h);
getch();
for(int i=0; i<h; i++)
printer(i,root);
}
int main()
{
int in;
int num;
do
{
system("cls");
printf("1.Insert element\n2.Search element\n3.Level order traversal\n");
scanf("%d",&in);
switch(in)
{
case 1: create(); break;
case 2:
printf("Enter num to be searched: ");
scanf("%d",&num);
if(search(num,root))
printf("Found");
else
printf("Not found");
getch();
break;
case 3:
levelordertraversal();
getch();
break;
}
}while(in!=-1);
return 0;
}