-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_insert.c
146 lines (134 loc) · 3.63 KB
/
avl_insert.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
** avl_insert.c : implementation of AVL Tree insert
** Copyright (C) 2018 Tim Whisonant
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "avl.h"
#include "avl_util.h"
static avl_tree_node * avl_tree_insert_node(avl_tree *t,
void *item,
avl_tree_node *node,
int *inserted)
{
int64_t res;
int32_t balance;
if (!node) {
node = t->allocate_node(item);
if (node) {
*inserted = 1;
node->height = 1;
}
return node;
}
res = t->compare_items(item, node->item);
if (res < 0)
node->left = avl_tree_insert_node(t, item, node->left, inserted);
else if (res > 0)
node->right = avl_tree_insert_node(t, item, node->right, inserted);
else // item collision - new item not inserted
return node;
node->height = 1 + avl_tree_max(avl_tree_height_node(node->left),
avl_tree_height_node(node->right));
balance = avl_tree_balance_node(node);
/*
// Left Left Case
// - node == z.
// - item was inserted under x.
// - fix by rotating z right.
//
// z y
// / \ / \
// y T4 x z
// / \ / \ / \
// x T3 T1 T2 T3 T4
// / \
// T1 T2
*/
if (balance > 1) {
res = t->compare_items(item, node->left->item);
if (res < 0)
return avl_tree_ror_node(node);
}
/*
// Right Right Case
// - node == z.
// - item was inserted under x.
// - fix by rotating z left.
//
// z y
// / \ / \
// T1 y z x
// / \ / \ / \
// T2 x T1 T2 T3 T4
// / \
// T3 T4
*/
if (balance < -1) {
res = t->compare_items(item, node->right->item);
if (res > 0)
return avl_tree_rol_node(node);
}
/*
// Left Right Case
// - node == z.
// - item was inserted under x.
// - fix by rotating y left, then z right.
//
// z z x
// / \ / \ / \
// y T4 x T4 y z
// / \ / \ / \ / \
// T1 x y T3 T1 T2 T3 T4
// / \ / \
// T2 T3 T1 T2
//
*/
if (balance > 1) {
res = t->compare_items(item, node->left->item);
if (res > 0) {
node->left = avl_tree_rol_node(node->left);
return avl_tree_ror_node(node);
}
}
/*
// Right Left Case
// - node == z.
// - item was inserted under x.
// - fix by rotating y right, then z left.
//
// z z x
// / \ / \ / \
// T1 y T1 x z y
// / \ / \ / \ / \
// x T4 T2 y T1 T2 T3 T4
// / \ / \
// T2 T3 T3 T4
*/
if (balance < -1) {
res = t->compare_items(item, node->right->item);
if (res < 0) {
node->right = avl_tree_ror_node(node->right);
return avl_tree_rol_node(node);
}
}
return node; // no change
}
int avl_tree_insert(avl_tree *t, void *item)
{
int inserted = 0;
t->root = avl_tree_insert_node(t, item, t->root, &inserted);
return inserted;
}