![Page 1: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/1.jpg)
자료구조 강의 노트 , 문병로
Ch. 13-1 Balanced Search Trees
• Search time in a tree depends on the tree’s height• Balanced binary search trees
– Guarantees O(log n)-time search, insertion, and deletion
– AVL tree, red-black tree• Balanced k-ary trees
– 2-3 tree, 2-3-4 tree, B-trees
![Page 2: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/2.jpg)
자료구조 강의 노트 , 문병로
Worst balanced
Best balanced
![Page 3: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/3.jpg)
자료구조 강의 노트 , 문병로
2-3 Tree
• A special case of B-trees• k-ary search tree
TT11
key1 key2 key3 … keyk-1
TT22TTkkTT33 TT44 …
TTii TTi+1i+1< key i <
![Page 4: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/4.jpg)
자료구조 강의 노트 , 문병로
• T is a 2-3 tree of height h ifT is empty (height 0)
orT is of the form
where TL , TM , and TR are all 2-3 trees, each of height h-1. Here only TR can be empty w/ only one key in the node r.
TTLL
rr
TTRRTTMM
![Page 5: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/5.jpg)
자료구조 강의 노트 , 문병로
• 2-3 tree (intuitive description)– Each internal node has either two or three
children– All leaves are at the same level
![Page 6: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/6.jpg)
자료구조 강의 노트 , 문병로
A 2-3 Tree of Height 3
![Page 7: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/7.jpg)
자료구조 강의 노트 , 문병로
A 2-Node A 3-Node
Nodes in a 2-3 Tree3
![Page 8: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/8.jpg)
자료구조 강의 노트 , 문병로
An Example 2-3 Tree
![Page 9: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/9.jpg)
자료구조 강의 노트 , 문병로
A Balanced Binary Search Tree
A 2-3 Tree w/ the Same Elements
![Page 10: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/10.jpg)
자료구조 강의 노트 , 문병로
Continued… Continued… After a Sequence of InsertionsAfter a Sequence of Insertions
![Page 11: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/11.jpg)
자료구조 강의 노트 , 문병로
Node Structure
public class Tree23Node {private Comparable smallItem; private Comparable largeItem;private Tree23Node leftChild;private Tree23Node midChild;private Tree23Node rightChild;
}
![Page 12: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/12.jpg)
자료구조 강의 노트 , 문병로
A Sequence of Example Insertions in 2-3 Trees
Starting 2-3 Tree
40
![Page 13: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/13.jpg)
자료구조 강의 노트 , 문병로
inserting 39
40
![Page 14: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/14.jpg)
자료구조 강의 노트 , 문병로
inserting 38
![Page 15: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/15.jpg)
자료구조 강의 노트 , 문병로
inserting 37
38
![Page 16: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/16.jpg)
자료구조 강의 노트 , 문병로
inserting 36
![Page 17: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/17.jpg)
자료구조 강의 노트 , 문병로
inserting 35, 34, and 33
![Page 18: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/18.jpg)
자료구조 강의 노트 , 문병로
Inserting 32
32
30
50
35
34
33
37
![Page 19: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/19.jpg)
자료구조 강의 노트 , 문병로
Splitting a Leaf in a 2-3 TreeSplitting a Leaf in a 2-3 Tree
![Page 20: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/20.jpg)
자료구조 강의 노트 , 문병로
Splitting an Internal Node in a 2-3 TreeSplitting an Internal Node in a 2-3 Tree
![Page 21: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/21.jpg)
자료구조 강의 노트 , 문병로
Splitting the Root of a 2-3 TreeSplitting the Root of a 2-3 Tree
![Page 22: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/22.jpg)
자료구조 강의 노트 , 문병로
Insertion Algorithm
insertItem(theTree, key){
Find the leaf leafNode to which key should belong;Add key to leafNode;if (leafNode now has three items) { // overflow
split(leafNode);}
}
![Page 23: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/23.jpg)
자료구조 강의 노트 , 문병로
split (n) // n: node with 3 keys. If n is internal, it has 4 children.{
if (n is the root) {Create a new node p;
} else {Let p be the parent of n;
}Move up the middle key of n to p;Split the remaining two keys of node n into two nodes;Adjust links appropriately;if (p now has three items) {
split(p);}
}
![Page 24: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/24.jpg)
자료구조 강의 노트 , 문병로
Deletion in 2-3 Trees
• A sequence of example deletions
Starting 2-3 tree
40
![Page 25: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/25.jpg)
자료구조 강의 노트 , 문병로
Deleting 70
Binary search tree 에서의 삭제와 연관성을 생각해 볼 것
![Page 26: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/26.jpg)
자료구조 강의 노트 , 문병로
![Page 27: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/27.jpg)
자료구조 강의 노트 , 문병로
Deleting 100
![Page 28: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/28.jpg)
자료구조 강의 노트 , 문병로
DDeleting 80
![Page 29: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/29.jpg)
자료구조 강의 노트 , 문병로
![Page 30: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/30.jpg)
자료구조 강의 노트 , 문병로
b) Merging a leaf
a) Redistributing values
![Page 31: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/31.jpg)
자료구조 강의 노트 , 문병로
c) Redistributing values and children
d) Merging internal nodes
![Page 32: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/32.jpg)
자료구조 강의 노트 , 문병로
e) Deleting the root
![Page 33: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/33.jpg)
자료구조 강의 노트 , 문병로
Deletion AlgorithmdeleteItem(searchKey){
Find searchKey in the tree; if (searchKey is present) {
if (searchKey is not in a leaf) { Swap searchKey with its inorder successor, which should be in a leaf node, say theLeaf;
}Delete searchKey from theLeaf;if (theLeaf now has no item) {
fix(theLeaf);}return true;
} else {return false;
}}
![Page 34: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/34.jpg)
자료구조 강의 노트 , 문병로
fix (n) // n: empty node{
if (n is the root) {Remove the root;
} else {Let p be the parent of n;if (some sibling of n has more than one item) { // moving a spare
Distribute items appropriately among n, p, and the sibling;
if (n is internal) // adjust linkMove the appropriate child from sibling to n;
} else { // merge (no sibling has a spare, so mergible)Choose an adjacent sibling s of n;Bring the appropriate item down from p into s;if (n is internal) // at this point, n has only one child
Move n’s child to be s’s child;Remove node n;if (p is now empty) fix(p);
}}
}
![Page 35: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/35.jpg)
자료구조 강의 노트 , 문병로
2-3-4 Tree
• A special case of B-trees• k-ary search tree
TT11
key1 key2 key3 … keyk-1
TT22TTkkTT33 TT44 …
TTii TTi+1i+1< key i <
![Page 36: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/36.jpg)
자료구조 강의 노트 , 문병로
• T is a 2-3-4 tree of height h ifT is empty (height 0)
orT is of the form
where T1 , T2 , T3 , and T4 are all 2-3-4 trees, each of height h-1. Here T3 and T4 can be empty if there are fewer than three keys in the node r.
TT11
rr
TT33TT22 TT44
![Page 37: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/37.jpg)
자료구조 강의 노트 , 문병로
• 2-3-4 tree (intuitive description)– Each internal node has either two, three, or four
children– All leaves are at the same level
![Page 38: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/38.jpg)
자료구조 강의 노트 , 문병로
A 2-3-4 Tree of Height 3
![Page 39: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/39.jpg)
자료구조 강의 노트 , 문병로
A 4-Node in a 2-3-4 TreeA 4-Node in a 2-3-4 Tree
![Page 40: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/40.jpg)
자료구조 강의 노트 , 문병로
Node Structure
public class Tree234Node {private Comparable smallItem; private Comparable middleItem; private Comparable largeItem;private Tree234Node firstChild; private Tree234Node secondChild;private Tree234Node thirdChild;private Tree234Node fourthChild;
}
![Page 41: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/41.jpg)
자료구조 강의 노트 , 문병로
Insertion in 2-3-4 Trees• 4-node 를 만나자마자 무조건 split 한다 (2-3 tree 와 가장 다른 점 )• 그러므로 split 시점에 parent node 는 항상 key 를 더 받아들일
여유를 갖고 있다• 2-3 tree 의 insertion 보다 간명하다
InserInserting 20
• A sequence of example insertions
a node w/ 4 links
일단 split
20 삽입
![Page 42: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/42.jpg)
자료구조 강의 노트 , 문병로
Inserting 50 and 40
![Page 43: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/43.jpg)
자료구조 강의 노트 , 문병로
Inserting 70
일단 split 70 삽입
![Page 44: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/44.jpg)
자료구조 강의 노트 , 문병로
Inserting 80 and 15
![Page 45: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/45.jpg)
자료구조 강의 노트 , 문병로
Inserting 90
일단 split90 삽입
![Page 46: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/46.jpg)
자료구조 강의 노트 , 문병로
Inserting 100
일단 split
100 삽입
![Page 47: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/47.jpg)
자료구조 강의 노트 , 문병로
Splitting a 4-Node Root During InsertionSplitting a 4-Node Root During Insertion
![Page 48: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/48.jpg)
자료구조 강의 노트 , 문병로
Splitting a 4-Node Whose Parent is a 2-Node Splitting a 4-Node Whose Parent is a 2-Node During InsertionDuring Insertion
![Page 49: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/49.jpg)
자료구조 강의 노트 , 문병로
Splitting a 4-Node Whose Parent is a 3-NodeSplitting a 4-Node Whose Parent is a 3-NodeDuring InsertionDuring Insertion
![Page 50: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/50.jpg)
자료구조 강의 노트 , 문병로
Insertion Algorithm
insertItem(theTree, key){
Find the leaf leafNode to which key should belong;(If it encounters a 4-node on the path, split the node.)Add key to leafNode;
}
이 시점에 leafNode 에는 항상 key 를 담을 여유가 있다
![Page 51: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/51.jpg)
자료구조 강의 노트 , 문병로
Deletion in 2-3-4 Trees• Basically similar to that of 2-3 trees
– Find the node that contains the item to be deleted– If the node is internal, swap the item with its inorder successor (in
a leaf)
• If the leaf is a 3-node or a 4-node, simply delete the item
• To make the deletion simple, transform each 2-node (except the root) that you encounter during the search for the item into either a 3-node or a 4-node
![Page 52: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/52.jpg)
자료구조 강의 노트 , 문병로
Transformation of 2-Nodes (Sketch)
M
![Page 53: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/53.jpg)
자료구조 강의 노트 , 문병로
![Page 54: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/54.jpg)
자료구조 강의 노트 , 문병로
![Page 55: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/55.jpg)
자료구조 강의 노트 , 문병로
Red-Black Tree
• An equivalent binary-tree representation of 2-3-4 trees
• Each reference is given a color red or black– Assign the color black to the edges from the original 2-
3-4 tree – Assign the color red to the edges generated in the
course of transforming into a binary tree
![Page 56: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/56.jpg)
자료구조 강의 노트 , 문병로
Node Structure
public class RBTreeNode {public static final int RED = 0;public static final int BLACK = 1;private Comparable item; private RBTreeNode leftChild; private RBTreeNode rightChild;private int leftColor;private int rightColor;…
}
![Page 57: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/57.jpg)
자료구조 강의 노트 , 문병로
Insertion in Red-Black Trees
• 2-3-4 tree 의 insertion 과 기본적으로 같다 • 단지 binary tree 로 2-3-4 tree 의 insertion 을
simulation 한다
![Page 58: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/58.jpg)
자료구조 강의 노트 , 문병로
InserInserting 20
2-3-4 Tree Insertions and Red-Black Equivalents
10 60
30 10 60
30
20
![Page 59: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/59.jpg)
자료구조 강의 노트 , 문병로
Inserting 50 and 40
10 50
30
20 40 60
2-3-4 Tree Red-Black Tree
![Page 60: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/60.jpg)
자료구조 강의 노트 , 문병로
15 50
30
20 40 7010
60 80
2-3-4 Tree Red-Black Tree
![Page 61: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/61.jpg)
자료구조 강의 노트 , 문병로
Red-Black Representation of a 4-NodRed-Black Representation of a 4-Nod
![Page 62: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/62.jpg)
자료구조 강의 노트 , 문병로
Red-Black Representation of a 3-NodeRed-Black Representation of a 3-Node
![Page 63: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/63.jpg)
자료구조 강의 노트 , 문병로
2-3-4 Tree
Red-Black Tree
![Page 64: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/64.jpg)
자료구조 강의 노트 , 문병로
Splitting a Red-Black Representation of a 4-Node Splitting a Red-Black Representation of a 4-Node that is the Rootthat is the Root
![Page 65: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/65.jpg)
자료구조 강의 노트 , 문병로
Splitting a Red-Black Representation of a 4-Node Splitting a Red-Black Representation of a 4-Node whose Parent is a 2-Nodewhose Parent is a 2-Node
![Page 66: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/66.jpg)
자료구조 강의 노트 , 문병로
Splitting a Red-Black Representation of a 4-Node Splitting a Red-Black Representation of a 4-Node whose Parent is a 3-Nodewhose Parent is a 3-Node
![Page 67: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/67.jpg)
자료구조 강의 노트 , 문병로
Splitting a Red-Black Representation of a 4-Node Splitting a Red-Black Representation of a 4-Node whose Parent is a 3-Nodewhose Parent is a 3-Node
![Page 68: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/68.jpg)
자료구조 강의 노트 , 문병로
Splitting a Red-Black Representation of a 4-Node Splitting a Red-Black Representation of a 4-Node whose Parent is a 3-Nodewhose Parent is a 3-Node
![Page 69: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/69.jpg)
자료구조 강의 노트 , 문병로
Observation
• In a 2-3-4 tree– Every path from the root to a leaf encounters the same
# of edges• In a red-black tree
– Every path from the root to a leaf encounters the same # of black edges
– The # of red edges encountered in a path from the root to a leaf is not greater than “the # of black edges + 1”
– So, the longest path is not greater than “2*the shortest path + 1” (approx. two times the shortest path)
![Page 70: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/70.jpg)
자료구조 강의 노트 , 문병로
AVL Tree
• Devised by Adelson-Velskii and Landis • A balanced search tree
such that the heights of the left and right subtrees of any node differ by at most 1
![Page 71: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/71.jpg)
자료구조 강의 노트 , 문병로
Node Structure
public class AVLTreeNode {private Comparable item; private AVLTreeNode leftChild; private AVLTreeNode rightChild;private int leftHeight;private int rightHeight;…
}
![Page 72: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/72.jpg)
자료구조 강의 노트 , 문병로
AVL AVL
An unbalanced binary search tree
Balanced tree after rotation
Balanced tree after insertion
![Page 73: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/73.jpg)
자료구조 강의 노트 , 문병로
AVL
2
An Unbalanced Binary Search Tree
Balanced after a Single Left Rotation
![Page 74: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/74.jpg)
자료구조 강의 노트 , 문병로
Before and after a Single Left Rotation that Decreases the Tree’s Height
Height decreased!
2 AVL
not AVL
![Page 75: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/75.jpg)
자료구조 강의 노트 , 문병로
Height not changed!
2
not AVL AVL
![Page 76: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/76.jpg)
자료구조 강의 노트 , 문병로
An Unbalanced Binary Search Tree
Balanced after a Single Left Rotation
![Page 77: No Slide Titlesoar.snu.ac.kr/course/ds/20181/notes/Cha… · PPT file · Web view · 2018-05-02Ch. 13-1 Balanced Search Trees Search time in a tree depends on the tree’s height](https://reader031.vdocument.in/reader031/viewer/2022021820/5af12e897f8b9abc788e1bb9/html5/thumbnails/77.jpg)
자료구조 강의 노트 , 문병로
a) Before; b) During; and c) After a Double Rotation
2 35
30
22
25
10
20
3022
2510
20
35
(b) (c)