int seq_search(int key, int low, int high){
int i;
for( i = low; i <= high; i++ ){
if( list[i] == key )
return i;
return -1;
}
반복문의 리스트 끝 테스트 배제
int seq_search2(int key, int low, int high){
int i;
list[high+1] = key;
for(i = low; list[i] != key; i++){
if( i = low; list[i] != key; i++ )
return -1;
else
return i;
}
}
int search_binary(int key, int low, int high){
int middle;
if( low <= high ){
middle = (low + high) / 2;
if( key == list[middle] )
return middle;
else if(key < list[middle] )
return search_binary(key, low, middle-1);
else
return search_binary(key, middle+1, high);
}
return -1;
}
int search_binary2(int key, int low, int high){
int middle;
while( low <= high ){
middle = (low + high) / 2;
if( key == list[middle] )
return middle;
else if( key > list[middle] )
low = middle + 1;
else
high = middle - 1;
}
return -1;
}
인덱스(index) 테이블을 사용하여 탐색의 효율 증대이진 탐색과 이진 탐색 트리의 차이점
모든 노드의 왼쪽과 오른쪽 서브트리의 높이 차가 1이하인 이진탐색트리
트리가 비균형 상태로 되면 스스로 노드들을 재배치하여 균형 상태 유지
균형 인수
= (왼쪽 서브 트리의 높이 - 오른쪽 서브 트리의 높이)
모든 노드의 균형 인수가 +-1 이하면 AVL 트리

typedet struct AVLNode{
int key;
struct AVLNode *left;
struct AVLNode *right;
} AVLNode;
AVLNode *rotate_right(AVLNode *parent){
AVLNode *child = parent->left;
parent->left = child->right;
child->right = parent;
return child;
}
AVLNode *rotate_left(AVLNode *parent){
AVLNode *child = parent->right;
parent->right = child->left;
child->left = parent;
return child;
}
AVLNode *rotate_left_right(AVLNode *parent){
parent->left = rotate_left(parent->left);
return rotate_right(parent);
}
AVLNode *rotate_right_left(AVLNode *parent){
parent->right = rotate_right(parent->right);
return rotate_left(parent);
}