no slide titlesoar.snu.ac.kr/course/ds/20181/notes/cha… · ppt file · web view ·...
TRANSCRIPT
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
Worst balanced
Best balanced
자료구조 강의 노트 , 문병로
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 <
자료구조 강의 노트 , 문병로
• 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
자료구조 강의 노트 , 문병로
• 2-3 tree (intuitive description)– Each internal node has either two or three
children– All leaves are at the same level
자료구조 강의 노트 , 문병로
A 2-3 Tree of Height 3
자료구조 강의 노트 , 문병로
A 2-Node A 3-Node
Nodes in a 2-3 Tree3
자료구조 강의 노트 , 문병로
An Example 2-3 Tree
자료구조 강의 노트 , 문병로
A Balanced Binary Search Tree
A 2-3 Tree w/ the Same Elements
자료구조 강의 노트 , 문병로
Continued… Continued… After a Sequence of InsertionsAfter a Sequence of Insertions
자료구조 강의 노트 , 문병로
Node Structure
public class Tree23Node {private Comparable smallItem; private Comparable largeItem;private Tree23Node leftChild;private Tree23Node midChild;private Tree23Node rightChild;
}
자료구조 강의 노트 , 문병로
A Sequence of Example Insertions in 2-3 Trees
Starting 2-3 Tree
40
자료구조 강의 노트 , 문병로
inserting 39
40
자료구조 강의 노트 , 문병로
inserting 38
자료구조 강의 노트 , 문병로
inserting 37
38
자료구조 강의 노트 , 문병로
inserting 36
자료구조 강의 노트 , 문병로
inserting 35, 34, and 33
자료구조 강의 노트 , 문병로
Inserting 32
32
30
50
35
34
33
37
자료구조 강의 노트 , 문병로
Splitting a Leaf in a 2-3 TreeSplitting a Leaf in a 2-3 Tree
자료구조 강의 노트 , 문병로
Splitting an Internal Node in a 2-3 TreeSplitting an Internal Node in a 2-3 Tree
자료구조 강의 노트 , 문병로
Splitting the Root of a 2-3 TreeSplitting the Root of a 2-3 Tree
자료구조 강의 노트 , 문병로
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);}
}
자료구조 강의 노트 , 문병로
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);}
}
자료구조 강의 노트 , 문병로
Deletion in 2-3 Trees
• A sequence of example deletions
Starting 2-3 tree
40
자료구조 강의 노트 , 문병로
Deleting 70
Binary search tree 에서의 삭제와 연관성을 생각해 볼 것
자료구조 강의 노트 , 문병로
자료구조 강의 노트 , 문병로
Deleting 100
자료구조 강의 노트 , 문병로
DDeleting 80
자료구조 강의 노트 , 문병로
자료구조 강의 노트 , 문병로
b) Merging a leaf
a) Redistributing values
자료구조 강의 노트 , 문병로
c) Redistributing values and children
d) Merging internal nodes
자료구조 강의 노트 , 문병로
e) Deleting the root
자료구조 강의 노트 , 문병로
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;
}}
자료구조 강의 노트 , 문병로
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);
}}
}
자료구조 강의 노트 , 문병로
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 <
자료구조 강의 노트 , 문병로
• 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
자료구조 강의 노트 , 문병로
• 2-3-4 tree (intuitive description)– Each internal node has either two, three, or four
children– All leaves are at the same level
자료구조 강의 노트 , 문병로
A 2-3-4 Tree of Height 3
자료구조 강의 노트 , 문병로
A 4-Node in a 2-3-4 TreeA 4-Node in a 2-3-4 Tree
자료구조 강의 노트 , 문병로
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;
}
자료구조 강의 노트 , 문병로
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 삽입
자료구조 강의 노트 , 문병로
Inserting 50 and 40
자료구조 강의 노트 , 문병로
Inserting 70
일단 split 70 삽입
자료구조 강의 노트 , 문병로
Inserting 80 and 15
자료구조 강의 노트 , 문병로
Inserting 90
일단 split90 삽입
자료구조 강의 노트 , 문병로
Inserting 100
일단 split
100 삽입
자료구조 강의 노트 , 문병로
Splitting a 4-Node Root During InsertionSplitting a 4-Node Root During Insertion
자료구조 강의 노트 , 문병로
Splitting a 4-Node Whose Parent is a 2-Node Splitting a 4-Node Whose Parent is a 2-Node During InsertionDuring Insertion
자료구조 강의 노트 , 문병로
Splitting a 4-Node Whose Parent is a 3-NodeSplitting a 4-Node Whose Parent is a 3-NodeDuring InsertionDuring Insertion
자료구조 강의 노트 , 문병로
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 를 담을 여유가 있다
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
Transformation of 2-Nodes (Sketch)
M
자료구조 강의 노트 , 문병로
자료구조 강의 노트 , 문병로
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
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;…
}
자료구조 강의 노트 , 문병로
Insertion in Red-Black Trees
• 2-3-4 tree 의 insertion 과 기본적으로 같다 • 단지 binary tree 로 2-3-4 tree 의 insertion 을
simulation 한다
자료구조 강의 노트 , 문병로
InserInserting 20
2-3-4 Tree Insertions and Red-Black Equivalents
10 60
30 10 60
30
20
자료구조 강의 노트 , 문병로
Inserting 50 and 40
10 50
30
20 40 60
2-3-4 Tree Red-Black Tree
자료구조 강의 노트 , 문병로
15 50
30
20 40 7010
60 80
2-3-4 Tree Red-Black Tree
자료구조 강의 노트 , 문병로
Red-Black Representation of a 4-NodRed-Black Representation of a 4-Nod
자료구조 강의 노트 , 문병로
Red-Black Representation of a 3-NodeRed-Black Representation of a 3-Node
자료구조 강의 노트 , 문병로
2-3-4 Tree
Red-Black Tree
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
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)
자료구조 강의 노트 , 문병로
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
자료구조 강의 노트 , 문병로
Node Structure
public class AVLTreeNode {private Comparable item; private AVLTreeNode leftChild; private AVLTreeNode rightChild;private int leftHeight;private int rightHeight;…
}
자료구조 강의 노트 , 문병로
AVL AVL
An unbalanced binary search tree
Balanced tree after rotation
Balanced tree after insertion
자료구조 강의 노트 , 문병로
AVL
2
An Unbalanced Binary Search Tree
Balanced after a Single Left Rotation
자료구조 강의 노트 , 문병로
Before and after a Single Left Rotation that Decreases the Tree’s Height
Height decreased!
2 AVL
not AVL
자료구조 강의 노트 , 문병로
Height not changed!
2
not AVL AVL
자료구조 강의 노트 , 문병로
An Unbalanced Binary Search Tree
Balanced after a Single Left Rotation
자료구조 강의 노트 , 문병로
a) Before; b) During; and c) After a Double Rotation
2 35
30
22
25
10
20
3022
2510
20
35
(b) (c)