-
Notifications
You must be signed in to change notification settings - Fork 6
/
AVL Tree
134 lines (130 loc) · 2.59 KB
/
AVL Tree
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
131
132
133
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *left,*right;
int height;
};
int height(struct node *root)
{
if(root)
return root->height;
return -1;
}
int max(int a, int b)
{
return a>b?a:b;
}
void setHeight(struct node *root)
{
root->height=max(height(root->left),height(root->right))+1;
}
struct node * rotateLeft(struct node *root)
{
//printf("rotateLeft := %d\n",root->data);
struct node *newroot = root->left;
root->left=newroot->right;
newroot->right=root;
setHeight(newroot);
setHeight(root);
return newroot;
}
struct node * rotateRight(struct node *root)
{
//printf("rotateRight := %d\n",root->data);
struct node *newroot=root->right;
root->right=newroot->left;
newroot->left=root;
setHeight(newroot);
setHeight(root);
return newroot;
}
struct node * doubleRotateLeft(struct node *root)
{
//printf("doubleRotateLeft := %d\n",root->data);
root->left = rotateRight(root->left);
return rotateLeft(root);
}
struct node * doubleRotateRight(struct node *root)
{
//printf("doubleRotateRight := %d\n",root->data);
root->right =rotateLeft(root->right);
return rotateRight(root);
}
struct node *newNode(int data)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data=data;
temp->left=temp->right=NULL;
temp->height=0;
return temp;
}
struct node * insert(struct node *root, int data)
{
if(root == NULL)
{
root = newNode(data);
}
else if(data<root->data)
{
root->left= insert(root->left,data);
if(height(root->left)-height(root->right)==2)
{
//printf("\t** while insert %d** \n",data);
if(data<root->left->data)
root = rotateLeft(root);
else
root = doubleRotateLeft(root);
}
}
else if(data > root->data)
{
root->right= insert(root->right,data);
if(height(root->right)-height(root->left)==2)
{
//printf("\t** while insert %d** \n",data);
if(data > root->right->data)
root = rotateRight(root);
else
root = doubleRotateRight(root);
}
}
setHeight(root);
return root;
}
void preOrder(struct node *root)
{
if(root != NULL)
{
printf("%d\n",root->data);
preOrder(root->left);
preOrder(root->right);
}
}
void postOrder(struct node *root)
{
if(root != NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d\n",root->data);
}
}
void main()
{
struct node *root = NULL;
root = insert(root,60);
root = insert(root,80);
root = insert(root,65);
root = insert(root,90);
root = insert(root,100);
root = insert(root,70);
root = insert(root,20);
root = insert(root,30);
root = insert(root,35);
printf("\t**Pre Order**\n");
preOrder(root);
printf("\t**Post Order**\n");
postOrder(root);
}