Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- recursive
- 자료구조
- algorithm
- AWS
- 스프링
- redhat
- 하둡
- hadoop
- 자바
- Amazon
- 빅데이터
- Data Structure
- storage
- rhcsa
- big data
- 아마존
- Redshift
- docker
- 스토리지
- data
- sort
- linux
- 재귀
- 리눅스
- 알고리즘
- 도커
- 레드햇
- java
- Spring
- 설치
Archives
- Today
- Total
Developer MJ
[C/C++] Binary Search Tree 구현 본문
BST(이진 탐색 트리 : Binary Search Tree)
삽입, 검색
최소값, 최대값, 노드 갯수, 트리 높이
struct Node{
int data;
Node* left;
Node* right;
};
int findMin(Node* root)
{
if(root == nullptr)
{
return -1;
}
else if (root->left == nullptr)
{
return root->data;
}
else
{
return findMin(root->left);
}
}
int findMax(Node* root)
{
if(root == nullptr)
{
return -1;
}
else if (root->right == nullptr)
{
return root->data;
}
else
{
return findMax(root->right);
}
}
int findHeight(Node* root)
{
if(root == nullptr)
{
return -1;
}
int left = findHeight(root->left);
int right = findHeight(root->right);
return (left>right ? left : right)+1;
}
int findNodes(Node* root)
{
if(root == nullptr)
{
return 0;
}
int left = findNodes(root->left);
int right = findNodes(root->right);
return left+right+1;
}
Node* insert(Node* root ,int data){
if(root == nullptr)
{
root = new Node();
root->data = data;
root->left = root->right = nullptr;
}
else if( data <= root->data)
{
root->left = insert(root->left, data);
}
else
{
root->right = insert(root->right, data);
}
return root;
}
bool search(Node* root, int data)
{
if(root == nullptr)
{
return false;
}
else if (root->data == data)
{
return true;
}
else if (data < root->data)
{
return search(root->left, data);
}
else
{
return search(root->right, data);
}
}
Node* root;
int main(int argc, const char * argv[]) {
root = nullptr;
root = insert(root, 15);
root = insert(root, 20);
root = insert(root, 4);
root = insert(root, 3);
root = insert(root, 5);
root = insert(root, 29);
if(search(root, 3) == true)
{
printf("Found\n");
}
else
{
printf("Not Found\n");
}
int max = findMax(root);
printf("max : %d\n",max);
int min = findMin(root);
printf("min : %d\n",min);
int height = findHeight(root);
printf("height : %d\n",height);
int nodeCnt = findNodes(root);
printf("node count : %d\n",nodeCnt);
return 0;
}
'Programming > Code' 카테고리의 다른 글
[Java] Double Linked List 구현 (0) | 2020.12.19 |
---|---|
[C/C++] DFS/BFS 구현 (0) | 2019.04.24 |
[Java] 퀵정렬 (Quick Sort) (0) | 2019.04.18 |
[Java] 계수정렬(Counting Sort) (0) | 2019.04.18 |
[Java] 삽입 정렬 (Insertion Sort) (0) | 2019.04.15 |