π T(n) = T(1) + log n * C = C1 + C * log n → Θ(log n) μ€λͺ !
μ΄ μμμ BST(Binary Search Tree)μμ νμ(κ²μ, μ½μ
, μμ ) μ°μ°μ μκ° λ³΅μ‘λλ₯Ό ꡬνλ κ³Όμ μ λνλΈ κ±°μΌ.
μλ λ¨κ³λ³λ‘ λΆμν΄λ³Όκ². π
1οΈβ£ μ νμ λ€μ 보기
BSTμμ νμμ ν λ, κ° λ¨κ³λ§λ€ λ¬Έμ ν¬κΈ°κ° μ λ°μΌλ‘ μ€μ΄λ¦.
μ΄κ±Έ μ νμμΌλ‘ νννλ©΄:
μ¬κΈ°μ:
- T(n/2): μ λ° ν¬κΈ°μ νΈλ¦¬μμ λ€μ νμ
- C: νμ¬ λ Έλμμ λΉκ΅νλ λΉμ© (μμ μκ°, O(1))
2οΈβ£ μ νμ νμ₯νκΈ° (λ°λ³΅ μ μ©)
μ νμμ ν λ¨κ³ λ νμ₯ν΄λ³΄μ.
T(n)=T(n/2)+CT(n/2)=T(n/4)+CT(n/4)=T(n/8)+C...
μ΄κ±Έ κ³μ νμ₯νλ©΄:
T(n/2k)=T(1)+kC3οΈβ£ k ꡬνκΈ°
μ νμμ΄ μ’
λ£λλ 쑰건μ νμν λ
Έλκ° 1κ° λ¨μμ λ (T(1)).
μ¦, n/2^k = 1μ λ§μ‘±νλ k κ°μ μ°Ύμ.
μλ³μ 2^kλ₯Ό κ³±νλ©΄:
n=2kμλ³μ log_2λ₯Ό μ·¨νλ©΄:
logβ‘2n=kμ¦, BSTμ νμ κΉμ΄(μ΅λ λΉκ΅ νμ)λ k = log_2 n.
4οΈβ£ μ νμμ k λμ νκΈ°
μ κ²°κ³Όλ₯Ό μ νμμ λ£μΌλ©΄:
T(n)=T(1)+kCT(n)=T(1)+logβ‘2n⋅Cμ¬κΈ°μ:
- T(1) = C1 (Base Case, μ¦ 1κ°μ λ Έλλ₯Ό νμνλ μκ°)
- Cλ κ° λ¨κ³μμ λΉκ΅νλ λΉμ© (μμ)
λ°λΌμ:
T(n)=C1+C⋅logβ‘2n5οΈβ£ Θ(log n)μ΄ λμ€λ μ΄μ
Big-Theta(Θ) νκΈ°λ²μμ κ°μ₯ ν° νλ§ κ³ λ €νκ³ , μμ κ³μλ₯Ό 무μνλ€.
T(n)=C1+Clogβ‘2n- C1μ μμμ΄λ―λ‘ λ¬΄μ κ°λ₯
- C \log_2 nμμ Cλ μμμ΄λ―λ‘ λ¬΄μ κ°λ₯
- μ΅μ’ μ μΌλ‘ Θ(log n)
μ¦, BST νμ(κ²μ, μ½μ , μμ )μ μκ° λ³΅μ‘λλ Θ(log n)μ΄λ€! π
π 6οΈβ£ μ€μ μ½λμμ νμΈν΄λ³΄κΈ°
β
BSTμμ νμμ μννλ©΄ μ΅λ log_2 n λ² λΉκ΅νλ―λ‘ Θ(log n) μκ°
β
νμν λλ§λ€ μ λ°μ© μ€μ΄λλ κ²μ νμΈ κ°λ₯!
π μ΅μ’ μ 리
1οΈβ£ μ νμ νμ΄
- BSTμμ νμνλ κ³Όμ μ λ¬Έμ λ₯Ό μ λ°μ© μ€μ → T(n) = T(n/2) + C
- λ°λ³΅ μ μ©νλ©΄ T(n/2^k) = T(1) + kC
- n/2^k = 1μ΄ λλ k μ°ΎκΈ° → k = log_2 n
- μ νμ λμ → T(n) = C1 + C log_2 n
- Big-Theta νκΈ°λ²μΌλ‘ νν → Θ(log n)
2οΈβ£ BST νμμ μκ° λ³΅μ‘λ
- μ΅μ μ κ²½μ°: O(1) (νμν κ°μ΄ 루νΈμ μμ λ)
- νκ· μ μΈ κ²½μ°: O(log n)
- μ΅μ μ κ²½μ°: O(n) (νΈν₯ νΈλ¦¬μμ νμν λ)
- **κ· ν BST (AVL, Red-Black Tree λ±)**μμλ νμ Θ(log n)
π μ΄μ μ νμκ³Ό Θ(log n)μ΄ λμ€λ μ리 μλ²½νκ² μ΄ν΄λμ§? ππ₯
π ν¨μ¨μ μΈ κ±°λμ κ³± κ³μ° (A Better Power Function)
μ£Όμ΄μ§ ν¨μλ κ±°λμ κ³±(power) κ³μ°μ ν¨μ¨μ μΌλ‘ μννλ μ¬κ· ν¨μμ
λλ€.
μΌλ°μ μΈ κ±°λμ κ³± κ³μ°λ³΄λ€ ν¨μ¬ λΉ λ₯΄κ² κ³μ°ν μ μμ΄μ. π
1οΈβ£ κΈ°λ³Έμ μΈ power(x, n) κ³μ° λ°©λ²
μΌλ°μ μΌλ‘ κ±°λμ κ³±μ κ³μ°νλ κ°μ₯ μ§κ΄μ μΈ λ°©λ²μ xλ₯Ό nλ² κ³±νλ κ²:
νμ§λ§ μ΄ λ°©μμ μκ° λ³΅μ‘λλ O(n) μ΄λ―λ‘, nμ΄ μ»€μ§λ©΄ λ§€μ° λΉν¨μ¨μ !
2οΈβ£ ν¨μ¨μ μΈ λ°©λ²: λΆν μ 볡 (Divide & Conquer)
μ£Όμ΄μ§ μ½λλ λΆν μ 볡(Divide & Conquer)μ νμ©ν κ±°λμ κ³± κ³μ°μ΄μμ.
β μ§μ μ§μ(nμ΄ μ§μμΌ λ):
xn=(x2)n/2
μ¦, ν λ² μ κ³±ν΄μ μ§μλ₯Ό λ°μΌλ‘ μ€μ¬μ κ³μ°!
β νμ μ§μ(nμ΄ νμμΌ λ):
xn=(x2)n/2×x
μ§μμ κ²½μ°μ²λΌ μ€μ΄μ§λ§, λ§μ§λ§μ xλ₯Ό ν λ² λ κ³±ν΄μ€.
3οΈβ£ μ½λ μ€ν κ³Όμ λΆμ
π μμ 1: power(2, 10)
μ°λ¦¬λ 2^10μ κ³μ°νκ³ μΆμ.
- n = 10 (μ§μ) → power(2 * 2, 10 / 2) νΈμΆ → power(4, 5)
- n = 5 (νμ) → power(4 * 4, 5 / 2) * 4 νΈμΆ → power(16, 2) * 4
- n = 2 (μ§μ) → power(16 * 16, 2 / 2) νΈμΆ → power(256, 1)
- n = 1 (Base Case) → return 256
- κ° λ°ν:
- power(256, 1) = 256
- power(16, 2) = 256
- power(4, 5) = 256 * 4 = 1024
- power(2, 10) = 1024
β
κ²°κ³Ό: 2^10 = 1024
β
μ°μ° νμ: O(log n) λ²λ§ μ¬κ· νΈμΆλ¨!
4οΈβ£ μκ° λ³΅μ‘λ λΆμ
μ΄ λ°©λ²μ 맀 λ¨κ³μμ μ§μλ₯Ό μ λ°μ© μ€μ΄λ―λ‘ μκ° λ³΅μ‘λκ° O(log n) μ΄λ€.
π λΉκ΅
- μΌλ°μ μΈ λ°©λ²: O(n)
- λΆν μ 볡 λ°©λ²: O(log n)
μμ: power(2, 1024)
- μΌλ°μ μΈ λ°©λ² → 1024λ² κ³±νκΈ° μν
- λΆν μ 볡 λ°©λ² → log_2(1024) = 10λ² κ³±νκΈ° μν (ν¨μ¬ λΉ λ¦!)
public void deleteIterative(int elem) {
if (root == null) { // nothing to delete
return;
}
BSTNode prev = null;
BSTNode current = root;
// Find current that contains the elem value
// Keep track of prev (parent)
while (current != null) {
if (elem == current.data)
break;
else if (elem > current.data) {
prev = current;
current = current.right;
}
else {
prev = current;
current = current.left;
}
}
//System.out.println(current.data);
if (current == null)
return; // element not in the tree, nothing to delete
// Removing a node with one child
// The root also needs to change if we delete the root
if (current.left == null) { // no left child
if (prev == null) {
// removing the root with one right child
root = root.right;
return;
}
// Removing a node without the left child, but it is not a root
if (prev.left == current)
prev.left = current.right; // since current has not left child
else { //current is the right child of prev
prev.right = current.right;
}
}
else if (current.right == null){ // removing a node that does not have the right child
if (prev == null) {
// removing the root with one left child
root = root.left;
return;
}
// Removing a node without the right child, but it is not a root
if (prev.left == current)
prev.left = current.left;
else { //current is the right child of prev
prev.right = current.left;
}
}
else { // has both children
// Find the smallest value in the right subtree
BSTNode rightRoot = current.right;
BSTNode prevBeforeRightRoot = current;
while (rightRoot.left != null ) {
prevBeforeRightRoot = rightRoot;
rightRoot = rightRoot.left;
}
System.out.println("smallest in the right subtree " + rightRoot.data);
int smallest = rightRoot.data;
current.data = smallest;
// Remove smallest
if (prevBeforeRightRoot.right == rightRoot) {
prevBeforeRightRoot.right = rightRoot.right;
}
else
prevBeforeRightRoot.left = rightRoot.right;
}
}
'λ―Έκ΅μ ν > Data Structure and Algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
2.20 λͺ©μμΌ DSA (0) | 2025.02.21 |
---|---|
2.14 FRI DSA (0) | 2025.02.15 |
2.11 DSA (0) | 2025.02.12 |
linkedlist - reverse (0) | 2025.02.11 |
Iterable, Linkedlist, Queues, Stack, Trees (0) | 2025.02.09 |