summaryrefslogtreecommitdiff
path: root/05-27.md
blob: 3a617ea81e41c88416ffc07693baf8936af3ab07 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
[\<- 05/22](05-22.md)

---

## Just One of Many

- Today we will go over AVL tree insertion and deletion
	- The best way to learn AVL trees: practice, practice, practice!

- AVL trees are just one type of balanced search tree:
	- Red-Black trees (not as strict as AVL);
	- Splay trees (change tree on search as well);
	- 2-3 trees (2 or 3 children per node);
	- B-trees (generalization of 2-3 trees);
	- B+ trees (a B-tree on disk in which full data is stored only in the leaves, which are linked together as a linked list)

## Coding

- Coding a BST isn't that bad
- Coding an AVL tree is much harder

## Not Just Numbers

- Students always ask why are we inserting numbers into a BST?
	- In reality the number is just a key that identifies a much larger piece of data
	- The key might be your student ID number, which identifies your student record
- We can use strings (just like in a hash table) on a search tree as easily as we can use numbers
	- We'll have fun with some strings today

# Examples

## Example 1

Insert 8, 6, 7, 5, 3, 0, 9

- Insert 8
	- 8 is the root

- Insert 6
	- 6 is less than 8, so it becomes the left child
	- 6 is even, 8 is left high

- Insert 7
	- 7 is less than 8, and higher than 6, so 7 becomes the right child of 6
	- Now 8 is unbalanced
	- Rotate 6 left: now the tree is root 8, 7 is the left child, 6 is the left child of 7
	- Rotate 8 right: now 7 is the root, 6 is the left child, 8 is the right child

- Insert 5
	- 5 is less than 7 and less than 6, so 5 becomes the left child of 6
	- 6 and 7 are left high, the other nodes are even

- Insert 3
	- 3 is less than 7, less than 6, and less than 5, so it becomes the left child of 5
	- 6 and 7 are unbalanced
	- Rotate 6 right: Now 7 is the root, 8 is the right child, 5 is the left child, 3 is the left child of 5, 6 is the right child of 5

- Insert 0
	- 0 is less than 7, less than 5, less than 3, so it becomes the left child of 3
	- Now the root is unbalanced
	- Rotate 7 (the root) right: Now, 5 is the root, 3 is the left child, 0 is the left child of 3, 7 is the right child of the root, 6 is the left child of 7, 8 is the left child of 7

- Insert 9
	- 9 is greater than 5, greater than 7, greater than 8, so it becomes the right child of 8.
	- 5, 7, and 8 are right high

- Resulting Tree Stats:
	- Height = 4
	- Root = 5
	- Leaves = {0, 6, 9}
	- Single Rotations = 2
	- Double Rotations = 1

- Possible scenarios
	- Inserting 4 -> no rotation
	- Inserting 10 -> single rotation
	- Inserting 2 -> double rotation

## Example 2

- We have a tree: 
	- 5 is the root
		- 3 is the left child
			- 0 is the left child
		- 7 is the right child
			- 6 is the left child
			- 8 is the right child
				- 9 is the right child

- Delete 3: The resulting tree is:
	- 5 is the root
		- 0 is the left child
		- 7 is the right subchild
			- 6 is the left subchild
			- 8 is the right subchild
				- 9 is the right subchild

- The tree is right of right: 5 needs to be rotated left
	- 7 is the root
		- 5 is the left child
			- 0 is the left child
			- 6 is the right child
		- 8 is the right child
			- 9 is the right child

- Delete 9: The resulting tree is:
	- 7 is the root
		- 5 is the left child
			- 0 is the left child
			- 6 is the right child
		- 8 is the right child

- The emphasis will be on insertion, but it is good to practice deletion regardless

## Example 3

- Insert: 10, 20, 30, 40, 50, 60, 70
	- This is a worst case for a BST, but not for an AVL tree!

- Insert 10: The resulting tree is:
	- 10 is the root

- Insert 20: The resulting tree is:
	- 10 is the root
		- 20 is the right child

- Insert 30: The resulting tree is:
	- 10 is the root
		- 20 is the right child
			- 30 is the right child

- The tree is right of right: rotate 10 left:
	- 20 is the root
		- 10 is the left child
		- 30 is the right child

- Insert 40: the resultign tree is:
	- 20 is the root
		- 10 is the left child
		- 30 is the right child
			- 40 is the right child

- Insert 50: the resultign tree is:
	- 20 is the root
		- 10 is the left child
		- 30 is the right child
			- 40 is the right child
				- 50 is the right child

- The tree is right of right: rotate 30 left:
	- 20 is the root
		- 10 is the left child
		- 40 is the right child
			- 30 is the left child
			- 50 is the right child

- etc.

## Example 4: With Words!

- Insert dog, cat, bat, fox, elk, gnu, ant

- Insert dog:
	- "dog" is the root

- Insert cat:
	- "dog" is the root
		- "cat" is the left child

- Insert bat:
	- "dog" is the root
		- "cat" is the left child
			- "bat" is the left child

- The root is left of left: rotate "dog" right:
	- "cat" is the root
		- "bat" is the left child
		- "dog" is the right child

- Insert fox:
	- "cat" is the root
		- "bat" is the left child
		- "dog" is the right child
			- "fox" is the right child

- Insert elk:
	- "cat" is the root
		- "bat" is the left child
		- "dog" is the right child
			- "fox" is the right child
				- "elk" is the left child

- "dog" is left of right
	- first rotate "fox" right, then rotate "dog" left
	- "cat" is the root
		- "bat" is the left child
		- "elk" is the right child
			- "dog" is the left child
			- "fox" is the right child

- Insert gnu:
	- "cat" is the root
		- "bat" is the left child
		- "elk" is the right child
			- "dog" is the left child
			- "fox" is the right child
				- "gnu" is the right child

- the root is right of right: rotate "cat" left:
	- "elk" is the root
		- "cat" is the left child
			- "bat" is the left child
			- "dog" is the right child
		- "fox" is the right child
			- "gnu" is the right child

- etc.

## Example 5: The Mammoth Example

- Insert doe, emu, gar, fly, boa, cow, sow, jay, hog, yak, lar, nit, ass, owl, pug, rat, man

- Try this on your own. I will only write out one guide to constructing the tree (as opposed to rewriting each time a new entry is added

- "jay" is the root
	- emu" is the left child
		- "cow" is the left child
			- "boa" is the left child
				- "ass" is the left child
			- "doe" is the right child
		- "gar" is the right child
			- "fly" is the left child
			- "hog" is the right child
	- "sow" is the right child
		- "lar" is the left child
			- "nit" is the right child
		- "yak" is the right child

- This is the AVL tree with most of the entries. Try adding the rest yourself

---

[05/29 ->](05-29.md)