-
Notifications
You must be signed in to change notification settings - Fork 0
/
inorder_preorder_postorder.c
125 lines (123 loc) · 2.46 KB
/
inorder_preorder_postorder.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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink;
struct node *rlink;
int height;
};
typedef struct node *NODE;
NODE create_node(int key);
NODE insert(NODE root,int key);
NODE deletion(NODE root,int key);
NODE left_rotate(NODE root);
NODE right_rotate(NODE root);
int height(NODE root);
int balanace_factor(NODE root);
void preorder(NODE root);
void inorder(NODE root);
void postorder(NODE root);
int max(int a,int b);
int main()
{
NODE root=NULL;
int m,n;
while(1)
{
printf("1:Insertion 2:deletion 3:preorder 4:inorder 5:postorder 6:exit\n");
scanf("%d",&m);
if(m==1)
{
printf("Enter the element\n");
scanf("%d",&n);
root=insertion(root,n);
}
else if(m==2)
{
printf("Enter the element that you want to delete\n");
scanf("%d",&n);
root=deletion(root,n);
}
else if(m==3)
{
preorder(root);
}
else if(m==4)
{
inorder(root);
}
else if(m==5)
{
postorder(root);
}
else if(m==6)
{
exit(0);
}
}
}
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int height(NODE root)
{
if(root==NULL)
return 0;
else
return root->height;
}
int balanace_factor(NODE root)
{
if(root==NULL)
return 0;
else
return (height(root->llink)-height(root->rlink));
}
NODE create_node(int key)
{
NODE newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=key;
newnode->rlink=newnode->link=NULL;
newnode->height=1;
return newnode;
}
NODE left_rotate(NODE root)
{
NODE a,b;
a=root->rlink;
b=a->llink;
a->llink=root;
root->llink=b;
}
NODE insertion(NODE root,int key);
{
int bl;
NODE newnode;
if(root==NULL)
{
newnode=create_node(key);
return newnode;
}
else
{
if(key>=root->data)
{
root->rlink=insertion(root->rlink,key);
}
else
{
root->link=insertion(root->llink,key);
}
}
root->height=1+max(height(root->llink),height(root->rllink));
bl=height(root->llink)-height(root->rlnk);
if(bl<-1 && key>)
{
}
}