-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavl.c
More file actions
92 lines (83 loc) · 1.22 KB
/
avl.c
File metadata and controls
92 lines (83 loc) · 1.22 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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int arr[4],i=0;
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node node;
node *root=NULL;
node *temp=NULL;
node * create(int d)
{
node *ne=(node *)malloc(sizeof(node));
ne->data=d;
ne->l=NULL;
ne->r=NULL;
return ne;
}
void insert(node *ne)
{
if(root==NULL)
root=ne;
else
{
node *temp=root;
while(1)
{
if(temp->data <= ne->data)
{
if(temp->r==NULL)
{temp->r=ne;break;}
else
temp=temp->r;
}
else
{
if(temp->l==NULL)
{ temp->l=ne;break;}
else
temp=temp->l;
}
}
}
}
void inorder(node *ro)
{
if(ro!=NULL)
{
inorder(ro->l);
printf("%d ",ro->data);
arr[i++]=ro->data;
inorder(ro->r);
}
}
void fun(int l,int h)
{
int mid=(l+h)/2;
if(l<h)
{
printf("%d ",arr[mid]);
fun(l,mid);
fun(mid+1,h);
}
}
void main()
{
int a,h,n,j;
printf("Enter the values to enter in tree\n");
scanf("%d",&a);
while(a!=-1)
{
node *te=create(a);
insert(te);
scanf("%d",&a);
}
inorder(root);
printf("\n");
fun(0,5);
getch();
}