C언어/문제풀다 하나씩
Binary Search Tree 구현해보기 search함수
mcdn
2020. 5. 22. 13:51
반응형
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//bingary search tree
int map[30] = { 0,15,7,19, 0,8,17,25, 0,0,0,0,16,18 };
void find(int target, int now, int num) {
//now = 1;
if (now >= 30 || map[now] == 0) {
cout << target << ":" << "없음"<<endl;
return;
}
if (map[now] == target) {
cout << target << ":" << num << "회만에찾음" << endl;
}
else if (map[now] > target) {
find(target, now * 2, num + 1);
}
else {
find(target, now * 2 + 1, num + 1);
}
}
int main() {
for (int i = 0; i < 5;i++) {
int n;
cin >> n;
find(n, 1, 1);
}
}
1차배열 코딩한채로 하는거임.
while문 쓰라고 했는데 그냥 재귀로함...
입력 예제
8 16 9 25 30
출력 결과
8:3회만에찾음 16:4회만에찾음 9:없음 25:3회만에찾음 30:없음
반응형