introduction to algorithms and data structures lecture 15 ...uehara/course/2018/myanmar/15.pdf ·...
TRANSCRIPT
![Page 1: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/1.jpg)
Introduction to Algorithms and Data Structures
Lecture 15: Data Structure (5)Dynamic Search Tree and its balancing
Professor Ryuhei Uehara,School of Information Science, JAIST, Japan.
[email protected]://www.jaist.ac.jp/~uehara
1
![Page 2: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/2.jpg)
Dynamic search and data structure
• Sometimes, we would like to search in dynamic data, i.e., we add/remove data in the data set.
• Example: Document management in university– New students: add to list– Alumni: remove from list– When you get credit: search the list
2
![Page 3: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/3.jpg)
Naïve idea: array or linked list?
• Data in order:– Search: binary search in O(log n) time– Add and remove: O(n) time per data
• Data not in order:– Search and remove: O(n) time per data– Add: in O(1) time
Imagine: you have 10000 students, and you have 300 new students!
3
![Page 4: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/4.jpg)
Better idea: binary search tree• For every vertex v, we have the following;
– Data in v > any data in a vertex in left subtree– Data in v < any data in a vertex in right subtree
25
12 29
7 20 42
3 9 15
17
35
32 374
![Page 5: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/5.jpg)
Better idea: binary search tree
5
• When data is random:– Depth of the tree: O(log n)– Search, add, remove: O(log n) time.
• In the worst case:– Depth of the tree: n– When data is given in order,
we have the worst case.– Search, add, remove: O(n) time…
![Page 6: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/6.jpg)
Nice idea:(Self-)Balanced Binary Search Tree
• There are some algorithms that maintain to take balance of tree in depth .– e.g., AVL tree, 2-3 tree, 2-color tree (red-black tree)
6
Georgy M. Adelson-Velsky(1922−2014)
Evgenii M. Landis(1921−1997)
![Page 7: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/7.jpg)
AVL tree [G.M. Adelson-Velskii and E.M. Landis ‘62]
• Property (or assertion): at each vertex, the depth of left subtree and right subtree differs at most 1.
• Example:
7
![Page 8: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/8.jpg)
AVL tree: Insertion of data
• Find a leaf v for a new data x• Store data x into v (v is not a leaf any more)• Check the change of balance by insertion of x• From v to the root, check the balance at each
vertex, and rebalance (rotation) if necessary.18
21
25
12
1458
8
![Page 9: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/9.jpg)
AVL tree: Insertion of dataInsert x=4
before after
18
21
25
12
145
8
18
21
25
12
145
84
9
![Page 10: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/10.jpg)
AVL tree: Insertion of dataInsert x=10
before after
18
21
25
12
145
8
18
21
25
12
145
8
10
10
![Page 11: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/11.jpg)
AVL tree: Insertion of dataInsert x=20
before after
18
21
25
12
145
8
18
21
25
12
145
8
20
11
![Page 12: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/12.jpg)
AVL tree: Insertion of dataInsert x=23
before after
18
21
25
12
145
8
18
21
25
12
145
8 23
12
![Page 13: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/13.jpg)
AVL tree: Rebalance by rotations
• “Rotate” tree vertices to make the difference up to 1:– Rotation LL– Rotation RR– Double rotation LR– Double rotation RL
13
![Page 14: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/14.jpg)
AVL tree: Rebalance by rotation:Rotation LL
• Lift up the left subtree if it becomes too deep
14
![Page 15: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/15.jpg)
AVL tree: Rebalance by rotation:Rotation RR
• Lift up the right subtree if it becomes too deep
15
![Page 16: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/16.jpg)
AVL tree: Rebalance by rotation:Double rotation LR
• When right subtree of left subtree becomes too deep, lift up the left-right subtree.
16
![Page 17: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/17.jpg)
AVL tree: Rebalance by rotation:Double rotation RL
• When left subtree of right subtree becomes too deep, lift up the right-left subtree.
17
![Page 18: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/18.jpg)
AVL tree: Example
• Insertion of 8
5
9
7
3
8
5
8
7
3
9
Double rotation LR
18
![Page 19: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/19.jpg)
AVL tree: Example
• Insertion of 6
5
8
7
3
9
6
5 8
7
3 96
Double rotation RL
19
![Page 20: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/20.jpg)
AVL tree: Example
• Insertion of 4
5 8
7
3 96
4
20
![Page 21: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/21.jpg)
AVL tree: Example
• Deletion of 6
5 8
7
3 96
4
4 8
7
3 95
Double rotation LR
21
![Page 22: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/22.jpg)
AVL tree: Example
• Insertion of 6
4 8
7
3 95
6
22
![Page 23: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/23.jpg)
AVL tree: Example
• Deletion of 8
4 8
7
3 95
6
4 9
7
3 5
6
4
9
7
3
5
6
Double rotation LR
23
![Page 24: Introduction to Algorithms and Data Structures Lecture 15 ...uehara/course/2018/myanmar/15.pdf · (Self-)Balanced Binary Search Tree •There are some algorithms that maintain to](https://reader035.vdocument.in/reader035/viewer/2022062919/5f01c01b7e708231d400db9d/html5/thumbnails/24.jpg)
Time complexity of balanced binary search tree
• Search: time• Insertion/Deletion: time
– rotations– Each rotation takes constant time
• In total, on a balanced binary search tree, every operation can be done in time.(n is the number of data in the tree)
24