#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MoveToFront(int Dataset[], int length,int target);
int MoveToFront(int Dataset[], int length,int target)
{
int index = 0;
for (int i = 0; i < length; i++)
{
if (Dataset[i] == target)
{
index = 1;
memmove(&Dataset[1], &Dataset[0], sizeof(Dataset[0]) * i);
Dataset[0] = target;
}
}
return index;
}
int main(void) {
int arr[] = { 5,6,1,41,515,2 };
int length = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
int TF = MoveToFront(arr, length, 6);
printf("\n=================\n\n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
printf("\n=================\n\n");
if (TF)
{
printf("Find!!!\n\n");
}
else
{
printf("Fail!!!\n\n");
}
}
5 6 1 41 515 2
=================
6 5 1 41 515 2
=================
Find!!!
int TransPose(int Dataset[], int length, int target)
{
int isFind = 0;
for (int i = 0; i < length; i++)
{
if (Dataset[i]==target)
{
if (i > 0)
{
Swap(&Dataset[i - 1], &Dataset[i]);
}
isFind = 1;
}
}
return isFind;
}
5 6 1 41 515 2
=================
5 6 1 515 41 2
=================
Find!!!
Node* SLL_FrequencyCount(Node** head, int target) // 계수법 LinkedList
{
Node* Current = (*head);
Node* Match = NULL;
while (Current != NULL)
{
if (Current->data == target)
{
(Current->frequency)++;
Match = Current;
break;
}
else
{
Current = Current->nextNode;
}
}
if (Match != NULL)
{
SLL_SortByFrequency(head); // 찾았다면 변경된 계수에 따라 데이터 재배치
return Match;
}
}
void SLL_SortByFrequency(Node** head) // 계수에 따라 데이터 재배치
{
Node* Current = *head;
Node* before = NULL;
Node* bbfore = NULL;
Node* after = NULL;
int length = SLL_GetNodeCount(*head);
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-i-1; j++)
{
if (SLL_GetNodeAt(*head,j)->frequency < SLL_GetNodeAt(*head,j+1)->frequency)
{
before = SLL_GetNodeAt(*head, j);
after = SLL_GetNodeAt(*head, j+1);
if (before == *head)
{
after->count = length;
before->nextNode = after->nextNode;
after->nextNode = before;
*head = after;
}
else
{
bbfore = SLL_GetNodeAt(*head, j - 1);
bbfore->nextNode = after;
before->nextNode = after->nextNode;
after->nextNode = before;
}
}
}
}
}
List[0] : -2
List[1] : -1
List[2] : 0
List[3] : 1
List[4] : 2
List[5] : 3
List[6] : 4
After FrequencyCount...
2x2 , 0x2 , 3x1
List[0] : 2
List[1] : 0
List[2] : 3
List[3] : -2
List[4] : -1
List[5] : 1
List[6] : 4
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define BOUND 7; // 수의 범위
int FrequencyCount(int Dataset[],int Count[], int length, int target);
void _Swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int arr[] = { 1,2,3,4,5,6,7 };
int count[] = { 0,0,0,0,0,0,0 };
int length = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
FrequencyCount(arr, count, length, 4);
FrequencyCount(arr, count, length, 7);
FrequencyCount(arr, count, length, 7);
FrequencyCount(arr, count, length, 7);
printf("\nAfter Count...\n\n");
for (int i = 0; i < length; i++)
{
printf("%d ", arr[i]);
}
}
int FrequencyCount(int Dataset[],int Count[], int length, int target) // 계수법 배열
{
int isFind = 0;
for (int i = 0; i < length; i++)
{
if (Dataset[i] == target)
{
Count[target - 1]++;
isFind = 1;
break;
}
}
if (isFind)
{
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-1-i; j++)
{
if (Count[Dataset[j]-1] < Count[Dataset[j+1] - 1])
{
_Swap(&Dataset[j], &Dataset[j + 1]);
}
}
}
}
return isFind;
}
1 2 3 4 5 6 7
After Count...
7 4 1 2 3 5 6
잎 노드까지 블랙노드 갯수는 동일 + 레드노드의 자식은 반드시 블랙
최대 경로는 최소경로의 2배 이하이므로
트리가 한쪽방향으로 기형적으로 자랄 수 없다.