태그 보관물: algorithm

Heap tree 만들기

Heap tree는 완전이진트리 형태로 모든 부모 노드의 값이 자식 노드 보다 크거나 (max heap) 모든 부모 노드의 값이 자식 노드 보다 작은 (min heap) tree를 말한다. 이 글에서는 max heap tree를 만드는 방법을 알아본다.

Heap tree를 생성하는 buildHeapTree()이 동작하는 절차는 다음과 같다. N은 원소의 갯수.

부모_노드 := 소숫점_버림(N / 2) - 1 부터 0까지
모든 부모_노드들에 대하여

    hepify(부모_노드):
        큰_자식 := max(왼쪽_자식, 오른쪽_자식)
        if(큰_자식 > 부모_노드)
            swap(큰_자식, 부모_노드)
            heapify(큰_자식)

재귀로 구현된 heapify()가 등장하는데, 이 함수가 하는 역할은 주어진 node를 부모로 시작해서 단말 node로 내려가면서 max heap 조건에 맞도록 변경해 주는 역할을 한다. heapify()는 큰 자식과 부모를 swap() 하는 경우 재귀하므로 시간 복잡도는 O(트리높이)가 된다.

Heap tree를 생성하는 방법을 고민해 본 적이 있다면, 부모 node들을 순회 할 때 역순으로 순회하는게 얼마나 영리한 방법인지 무릎을 쳤을 것이다. 내 얘기다;;

예제

임의의 정렬되지 않은 배열 { 2, 7, 5, 4, 1, 6, 10, 3, 9, 8}을 예로들어 보자. 10개의 node를 가지는 tree를 배열로 구현 할 때, 부모 node들의 index는 소숫점_버림(N/2) – 1로 계산할 수 있다. 따라서 10개의 node를 가지는 이 tree의 부모 node들의 위치는 [0], [1], [2], [3], [4]이다. (C/C++로 구현 할때는 N/2한 값을 그냥 int형 변수에 넣으면 된다)

각 부모 node들과 자식 node들을 subtree라고 하면 이 예제는 다음과 같이 5개의 subtree들로 구분될 수 있다.

Heapify – Subtree A

부모인 4번째 index의 값과 하나 밖에 없는 자식인 9번째 index의 값을 비교한다. 자식의 값이 더 크므로 부모와 swap()한다.

Heapify – Subtree B

부모인 3번째 index와 왼쪽 자식인 7번째, 오른쪽 자식인 8번째를 비교해서 가장 큰 오른쪽 자식과 부모 node를 swap()한다.

Heapify – Subtree C

오른쪽 자식인 index 6번과 부모인 index 2번을 교체한다.

Heapify – Subtree D

1번 index의 왼쪽 자식이 더 크므로 부모와 swap()한다.

Heapify – Subtree E

2번 index의 값이 더 커서 부모인 0번 index와 swap()하고 보니 2번 index를 부모로 하는 subtree가 더이상 max heap이 아니게 된다.

그래서 다시 한번 heapify()를 호출해서 max heap을 만든다.

Code

/*
  Name: buildHeapTree
  Desc.: Makes sure all sub-trees to be max heapified.
  Args.:
    - arr : Pointer to array to be sorted.
    - arrSize : Size of the array.
*/
static void buildHeapTree(int* arr, int arrSize)
{
    int parentIdx = (arrSize / 2) - 1;
    int arrayLastIdx = arrSize - 1;
 
    for(; parentIdx >= 0; parentIdx--)
    {
        maxHeapify(arr, arrayLastIdx, parentIdx);
    }
}
/*
  Name: swap
  Desc.: Exchanges two elements of the array 'arr' that two given indices indicates.
  Args.:
    - arr: Pointer to array to be sorted.
    - idx1, idx2 : Two indices to be swapped.
*/
static void swap(int* arr, int idx1, int idx2)
{
    int t = std::move(arr[idx1]);
    arr[idx1] = std::move(arr[idx2]);
    arr[idx2] = std::move(t);
}

 
/*
  Name: maxHeapify
  Desc.: Makes given tree and it's sub-trees to be max heap trees.
  Args:
    - arr : Pointer to array to be sorted.
    - arrayLastIdx : Last index of the given array.
    - parentIdx : Index of the parent node of the tree.
*/
static void maxHeapify(int* arr, int arrayLastIdx, int parentIdx)
{
    int lcIdx = (parentIdx * 2) + 1;  // Index for left child node.
    int rcIdx = lcIdx + 1;            // Index for right child node.
    int biggerChildIdx = lcIdx;       // Index for child that has bigger value.
 
    // Stop recursion if left child index exceeds last index of the array.
    if(lcIdx > arrayLastIdx)
        return;
 
    // Left child would be the only one if there's no right child. 
    if(rcIdx > arrayLastIdx)
        biggerChildIdx = lcIdx;
    else   // Which of left/right is bigger if both are exits?
        biggerChildIdx = (arr[lcIdx] < arr[rcIdx]) ? rcIdx : lcIdx;
 
    // Swap and heapify if child is bigger than parent.
    if(arr[parentIdx] < arr[biggerChildIdx])
    {
        swap(arr, parentIdx, biggerChildIdx);
        maxHeapify(arr, arrayLastIdx, biggerChildIdx);
    }
 
    return;
}

Heap sort (힙 정렬)

Heap sort는 root node에 항상 제일 작은 값이나 제일 큰 값이 오도록 heap tree를 구성해서 정렬하는 방법이다. Heap tree의 root에 최소 값이 오도록 구현하면 min heap tree, 최대 값이 오도록 하면 max heap tree라 한다. Heap tree를 구성하는 방법에 대해서는 별도의 포스트로 다루고 여기서는 max heap으로 오름차순 정렬하는 과정을 설명한다.

Heap sort는 한번 heap tree를 구성한 다음에는, 어떤 값이 제자리를 찾는데 소요되는 시간이 짧다는 점을 이용한다. 부모와 자식노드의 값에 따라 완전 이진트리로 구성하므로, 처음 한 번 heap tree를 구성할 때 O(N/2)만큼 소요된 이후에는, heap tree가 구성된 상태에서는 어떤 노드의 제자리를 찾는데 O(log N) 만큼이 소요된다.

Heap sort 과정을 정리하면 다음과 같다.

1. Heap tree를 만든다.
2. 제일 마지막 노드를 root와 바꿔서 heap tree른 망가뜨린다. 
   이때, root에 있던 최대값은 배열의 마지막 위치로 옮겨진다.
3. Heap tree를 구성하는 배열 크기를 하나 줄여서 옮겨진 최대값을 heap tree에서 제외시킨다.
4. 다시 heap tree가 되도록 새로운 root의 자리를 찾아 준다.
5. 위의 2, 3, 4번 과정을 배열의 크기가 1이 될때 까지 반복한다.

언뜻 잘 이해가 안 갈수도 있는데, 모종의 과정을 거치고 나면 root (배열의 0번 index)에 최대값이 오게되는 마법의 tree가 있고 이것의 root를 싹둑싹둑 자를때마다 남아있는 것들 중 제일 큰값이 root에 올라오는 것을 상상해보면 도움이 될지도 모르겠다. Tree라고 하면 일반적으로 왼쪽과 오른쪽 자식노드에 대한 pointer를 가지는 형태를 떠올리게 되니까 배열로 표현된 tree가 혼동스러울 수도 있겠는데, 완전 이진 트리인 경우에는 배열안에 원소들이 차례대로 들어 있다고 가정할 때 왼쪽/오른쪽 자식 node의 위치를 다음의 계산식으로 구할 수 있다.

왼쪽 자식 index = (2 * 부모 node index) + 1
오른쪽 자식 index = 왼쪽 자식 index + 1

그리고 하나라도 자식이 있는 부모 node들의 index도 구할 수 있다.

부모 node들 = [0 부터 floor(배열 크기 / 2) - 1 까지]

* floor() : 소숫점 버림
예) 배열크기가 10 이면 부모 node는 0, 1, 2, 3, 4가 된다.

어떤 배열이 다음의 순서일 때 이를 heap sort하는 과정의 예를 들어 살펴보자.

{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

1. “Heap tree를 만든다.”

Max heap을 가정하므로, 위의 배열로 max heap tree를 만들면 다음과 같다. 대괄호([ ]) 안에 있는 숫자는 배열상의  index를 의미한다.

예제의 경우, 이미 “모든 서브트리의 부모가 가진 값이 자식보다 크다”는 max heap tree 조건을 만족하기 때문에 배열 안에서의 순서가 변경되지 않는다. 다른 경우에 heap tree를 생성하는 절차는 나중에 설명하도록 하고 지금은 꼼수 같겠지만 일단 그냥 넘어가자. 위 그림에서 색칠된 node들은 자식들을 가지는 부모 node들인데, 앞서 설명한 부모 node들의 index를 구하는 공식에 의해서 얻을 수 있다. 이 예제에서는 잘 보이지 않지만, 부모 node들의 index를 구하는 것은 heap tree를 구성할 때 중요하다. 이 부분도 별도의 포스팅으로 따로 설명한다.

2. “제일 마지막 node를 root와 바꿔서 heap tree를 망가뜨린다.”

“아니 왜?” 하는 생각이 들 수도 있을 텐데, root에는 최대값이 들어 있으므로 제일 마지막 node와 swap하면 root에 있던 값은 오름 차순 정렬일 때의 자기 자리를 찾아가는 샘이 된다.

3. “Heap tree를 구성하는 배열의 크기를 하나 줄인다.”

이전의 root였던 10은 제자리를 찾았으니 이제 배열의 크기를 하나 줄여서 index 0 ~ 8까지가 배열이라고 가정한다. 배열 그림을 보면 정렬되어가는 모습이 보일 것이다.

4. “다시 heap tree가 되도록 새로운 root의 자리를 찾아 준다.”

얼떨결에 새로운 root가 된 node는 이제 자기 있을 곳을 찾아가야 한다. 자식 node들과의 크기를 비교해서 둘 중 큰 자식과 swap하면서 원래 있어야 할 곳을 찾아간다. 완전 이진 트리의 장점을 살려서 O(log N)의 비교 연산만에 자기 자리를 찾을 수 있다. 이 과정에서 노드들간의 swap이 일어나고 root에는 다시 최소 혹은 최대값이 오게된다.

Root였던 ‘1’은 자식들 중 더 큰 왼쪽 자식인 ‘9’와 자리를 바꾼다.

여전히 max heap tree를 만족하지 못하므로 ‘7’, ‘6’ 중 큰 자식인 ‘7’과 자리를 바꾼다.

마지막으로 ‘3’, ‘2’ 중 큰 자식인 ‘3’과 자리를 바꾸면 모든 서브트리가 max heap tree의 조건을 만족하는 상태가 된다.

5. “위의 2, 3, 4번 과정을 배열의 크기가 1이 될때 까지 반복한다.”

이제 다시 새로뽑힌 root와 마지막 node인 2를 swap하고 배열 크기를 하나 줄인 다음 새로운 node의 자리를 찾아준다. 즉, node를 swap하고 index를 하나 줄이는 과정을 O(N)번 반복 하고 각 반복 마다 O(log N)의 –정확히는 매 반복 마다 배열의 크기가 하나씩 줄어듬– 비교 연산이 필요하다.  한바퀴 더 돌아 보면.

바꾸고

크기줄이고

자리 찾아주고

이렇게 원소를 하나씩 줄여 가면서 1개가 남을때까지 반복하면 정렬된 배열을 얻게 된다.

Code로 나타내면 다음과 같다. (충분히 시험되지 않았음)

/*
  Name: heapSort
  Desc.: Sorts given array 'arr' as an increasing order.
  Args.:
    - arr : Pointer to array to be sorted.
    - arrSize : Size of the array. (Number of elements in the array)
*/
void heapSort(int* arr, int arrSize)
{
    int arrLastIdx = arrSize - 1;
    buildHeapTree(arr, arrSize);
    while(arrLastIdx > 0)
    {
        swap(arr, 0, arrLastIdx);
        arrLastIdx --;
        maxHeapify(arr, arrLastIdx, 0);
    }
}
/*
  Name: swap
  Desc.: Exchanges two elements of the array 'arr' that two given indices indicates.
  Args.:
    - arr: Pointer to array to be sorted.
    - idx1, idx2 : Two indices to be swapped.
*/
static void swap(int* arr, int idx1, int idx2)
{
    int t = arr[idx1];
    arr[idx1] = arr[idx2];
    arr[idx2] = t;
}

/*
  Name: maxHeapify
  Desc.: Makes given tree and it's sub-trees to be max heap trees.
  Args:
    - arr : Point to array to be sorted.
    - arrayLastIdx : Last index of the given array.
    - parentIdx : Index of the parent node of the tree.
*/
static void maxHeapify(int* arr, int arrayLastIdx, int parentIdx)
{
    int lcIdx = (parentIdx * 2) + 1;  // Index for left child node.
    int rcIdx = lcIdx + 1;            // Index for right child node.
    int biggerChildIdx = lcIdx;       // Index for child that has bigger value.

    // Stop recursion if left child index exceeds last index of the array.
    if(lcIdx > arrayLastIdx)
        return;

    // Left child would be the only on if there's no right child. 
    if(rcIdx > arrayLastIdx)
        biggerChildIdx = lcIdx;
    else   // Which of left/right is bigger if both are exits?
        biggerChildIdx = (arr[lcIdx] < arr[rcIdx]) ? rcIdx : lcIdx;

    // Swap and heapify if child is bigger than parent.
    if(arr[parentIdx] < arr[biggerChildIdx])
    {
        swap(arr, parentIdx, biggerChildIdx);
        maxHeapify(arr, arrayLastIdx, biggerChildIdx);
    }

    return;
}


/*
  Name: buildHeapTree
  Desc.: Makes sure all sub-trees to be max heapified.
  Args.:
    - arr : Pointer to array to be sorted.
    - arrSize : Size of the array.
*/
static void buildHeapTree(int* arr, int arrSize)
{
    int parentIdx = (arrSize / 2) - 1;
    int arrayLastIdx = arrSize - 1;

    for(; parentIdx >= 0; parentIdx--)
    {
        maxHeapify(arr, arrayLastIdx, parentIdx);
    }
}