template<typename T, typename T2>
class MyTemplateClass
{
T m_Data1;
T2 m_Data2;
};
MyTemplateClass<int, float> obj;
#include <set>
#include <map>
using std::set;
using std::map;
using std::make_pair; // 1
int main()
{
map<int, int> intmap;
intmap.insert(make_pair(100, 1)); // 2
intmap.insert(make_pair(150, 2));
intmap.insert(make_pair(170, 3));
intmap.insert(make_pair(125, 4));
intmap.insert(make_pair(80, 5));
intmap.insert(make_pair(90, 6));
intmap.insert(make_pair(50, 7));
return 0;
}
100-1
/ \
80-5 150-2
/ \ / \
50-7 90-6 125-4 170-3
template<typename T1, typename T2>
struct BSTNode
{
T1 first;
T2 second;
BSTNode* pParent;
BSTNode* pLeftChild;
BSTNode* pRightChild;
};
template<typename T1, typename T2>
struct BSTPair
{
T1 first;
T2 second;
};
template<typename T1, typename T2>
struct BSTNode
{
BSTPair<T1, T2> pair;
BSTNode* pParent;
BSTNode* pLeftChild;
BSTNode* pRightChild;
};
map<int, int>::iterator mapiter = intmap.find(50);
if (mapiter == intmap.end());
{
//*mapiter; // 1
(*mapiter).first;
(*mapiter).second;
}
.
을 눌러서 pair에 접근 가능 first, second 나온다.template <typename T1, typename T2>
class BST
{
private:
BSTNode<T1, T2>* m_Root; // 1
int m_CurCount; // 2
public:
void insert();
};
template<typename T1, typename T2>
BSTPair<T1, T2> make_bstpair(const T1& _first, const T2& _second)
{
// 1
BSTPair<T1, T2> pair;
pair.first = _first;
pair.second = _second;
// 2
BSTPair<T1, T2> pair = { _first ,_second }
return pair;
// 3
return BSTPair<T1, T2>(_first, _second);
}
template<typename T1, typename T2>
struct BSTPair
{
T1 first;
T2 second;
// 1
BSTPair(const T1& _first, const T2& _second)
: first(_first)
, second(_second)
{
}
};
class BST
{
private:
BSTNode<T1, T2>* m_Root;
int m_CurCount;
public:
//void insert();
void insert(const BSTPair<T1, T2>& _pair);
template<typename T1, typename T2>
void BST<T1, T2>::insert(const BSTPair<T1, T2>& _pair)
{
BSTNode<T1, T2>* pNewNode = new BSTNode<T1, T2>(_pair);
// 2.1
if (nullptr == m_Root)
{
this->m_Root = pNewNode;
}
else
{
BSTNode<T1, T2>* pNode = m_Root;
while (true)
{
if (pNewNode->pair.first < pNode->pair.first)
{
if (nullptr == pNode->pLeftChild)
{
pNode->pLeftChild = pNewNode;
pNewNode->pParent = pNode;
break;
}
pNode = pNode->pLeftChild;
}
else if (pNode->pair.first < pNewNode->pair.first)
{
if (nullptr == pNode->pRightChild)
{
pNode->pRightChild = pNewNode;
pNewNode->pParent = pNode;
break;
}
pNode = pNode->pRightChild;
}
}
}
++m_CurCount;
}
pair.first;
pair.second;
if (nullptr == m_Root)
{
}
else
BST()
: m_Root(nullptr)
, m_CurCount(0)
{}
~BST()
{
// 모든 노도들 삭제
}
BSTNode<T1, T2>* pNewNode = new BSTNode<T1, T2>(_pair,nullptr,nullptr,nullptr);
template<typename T1, typename T2>
struct BSTNode
{
BSTPair<T1, T2> pair;
BSTNode* pParent;
BSTNode* pLeftChild;
BSTNode* pRightChild;
// 1
BSTNode()
//: pair(_pair)
: pParent(nullptr)
, pLeftChild(nullptr)
, pRightChild(nullptr)
{}
// 2
BSTNode(const BSTPair<T1, T2>& _pair, BSTNode* _Parent, BSTNode* _LeftChild, BSTNode* _RightChild)
: pair(_pair)
, pParent(_Parent)
, pLeftChild(_LeftChild)
, pRightChild(_RightChild)
{}
};
BSTNode(const BSTPair<T1, T2>& _pair, BSTNode* _Parent = nullptr, BSTNode* _LeftChild = nullptr, BSTNode* _RightChild = nullptr)
: pair(_pair)
, pParent(_Parent)
, pLeftChild(_LeftChild)
, pRightChild(_RightChild)
{}
BSTNode<T1, T2>* pNewNode = new BSTNode<T1, T2>(_pair);
if (nullptr == m_Root)
{
this->m_Root = pNewNode;
}
++m_CurCount;
else
{
BSTNode<T1, T2>* pNode = m_Root; // 1
while (true) // 6
{
if (pNewNode->pair.first < pNode->pair.first) // 2
{
if (nullptr == pNode->pLeftChild) // 7
{
pNode->pLeftChild = pNewNode; // 8
pNewNode->pParent = pNode; // 9
break; // 10
}
pNode = pNode->pLeftChild; // 3
}
else if (pNode->pair.first < pNewNode->pair.first) // 4
{
if (nullptr == pNode->pRightChild) // 11
{
pNode->pRightChild = pNewNode; // 12
pNewNode->pParent = pNode; // 13
break; // 14
}
pNode = pNode->pRightChild; // 5
}
}
}
++m_CurCount; // 15
문제는 최초일떄말고 두번째 데이터 넣을때부터의 코드다.
1번은 m_Root
부터 시작해서 지역변수 pNode
에 m_Root
를 대입한다.
2번은 입력된 first
값이 현재 노드의 first
값보다 작은 경우
4번은 입력된 first
값이 현재 노드의 first
값보다 큰 경우
저 2~5번 작업을 반복해야되니 6번처럼 while문
으로 조건문을 둘른다.
7번은 왼쪽 방향으로 이미 정해졌으므로 내려갈 되가 nullptr
이라면 멈추는 조건문
while
문을 탈출한다.11번은 오른쪽 방향으로 이미 정해졌으므로 내려갈 되가 nullptr
이라면 멈추는 조건문
while
문을 탈출한다.15 하나를 채워넣으면 현재값을 증가.
BST<int, float> bst;
bst.insert(make_bstpair<int, float>(100, 1.1f));
bst.insert(make_bstpair<int, float>(150, 2.2f));
bst.insert(make_bstpair<int, float>(50, 2.2f));
template <typename T1, typename T2>
class BST
{
private:
BSTNode<T1, T2>* m_Root;
int m_CurCount;
public:
void insert(const BSTPair<T1, T2>& _pair);
// 1
class iterator;
iterator find(const T1& _ket);
public:
BST()
: m_Root(nullptr)
, m_CurCount(0)
{}
// 3
~BST()
{
// 모든 노도들 삭제
}
// 2
class iterator
{
};
};
// 2
public:
void Circit()
{
Circit(m_Root);
}
// 1
private:
void Circit(BSTNode<T1, T2>* _Node);
template<typename T1, typename T2>
inline void BST<T1, T2>::Circit(BSTNode<T1, T2>* _Node)
{
// 3
if (nullptr == _Node)
{
return;
}
std::cout << _Node->pair.first << std::endl;
Circit(_Node->pLeftChild); // 1
Circit(_Node->pRightChild); // 2
}
BST<int, float> bst;
bst.insert(make_bstpair<int, float>(100, 1.1f));
bst.insert(make_bstpair<int, float>(150, 2.2f));
bst.insert(make_bstpair<int, float>(50, 2.2f));
bst.Circit();
100
50 150
if (nullptr == _Node)
{
return;
}
std::cout << _Node->pair.first << std::endl;
Circit(_Node->pLeftChild);
Circit(_Node->pRightChild);
서킷이라는 함수가 호출되면 루트노드(100)이다.
첫번째 호출스택에서 왼쪽을 호출한다.
다시 방문할 왼쪽을 함수를 호출하면 3번째 호출스택은 50의 왼쪽은 없으니까 입력으로 nullptr반환되서 호출 종료
2번째 코드로 호출스택이 돌아와서 오른쪽 호출스택을 확인하고 거기도 없으니 nullptr반환해서 호출 종료
100에 오른쪽을 확인 150(출력)
150에서 왼쪽 호출하면 없으니까 nullptr하고 오른쪽도 호출해서 없으니까 nullptr하고 재귀함수가 종료된다.
제공하신 코드는 이진 탐색 트리(Binary Search Tree, BST)의 전위 순회(pre-order traversal)를 위한 Circit
함수입니다. 전위 순회에서는 루트 노드를 먼저 처리한 후, 왼쪽 서브트리와 오른쪽 서브트리를 순차적으로 방문합니다. 함수는 재귀적으로 구현되어 있습니다.
gpt
Circit
함수의 첫 번째 호출 (루트 = 100):
100
)를 처리합니다 (출력).50
)으로 재귀 호출을 합니다.150
)으로 재귀 호출을 합니다.Circit
함수의 두 번째 호출 (루트 = 50):
50
)를 처리합니다 (출력).50
의 왼쪽 자식은 없으므로, 왼쪽 자식에 대한 호출은 무시됩니다.50
의 오른쪽 자식도 없으므로, 오른쪽 자식에 대한 호출도 무시됩니다.첫 번째 호출로 돌아옴 (루트 = 100), 오른쪽 자식 처리:
150
)으로 재귀 호출을 합니다.Circit
함수의 세 번째 호출 (루트 = 150):
150
)를 처리합니다 (출력).150
의 왼쪽 자식은 없으므로, 왼쪽 자식에 대한 호출은 무시됩니다.150
의 오른쪽 자식도 없으므로, 오른쪽 자식에 대한 호출도 무시됩니다.전위 순회의 결과로서, 노드들은 다음 순서로 처리됩니다: 100
, 50
, 150
. 이 순서는 루트를 먼저 방문하고, 그 다음으로 왼쪽 및 오른쪽 서브트리를 방문하는 전위 순회의 특징을 반영합니다.
BST<int, float> bst;
bst.insert(make_bstpair<int, float>(100, 1.1f));
bst.insert(make_bstpair<int, float>(150, 2.2f));
bst.insert(make_bstpair<int, float>(50, 2.2f));
bst.insert(make_bstpair<int, float>(170, 1.1f));
bst.insert(make_bstpair<int, float>(125, 2.2f));
bst.insert(make_bstpair<int, float>(25, 2.2f));
bst.insert(make_bstpair<int, float>(75, 2.2f));
bst.Circit();
100
50 150
25 75 125 170
if (nullptr == _Node)
{
return;
}
Circit(_Node->pLeftChild);
std::cout << _Node->pair.first << std::endl;
Circit(_Node->pRightChild);
if (nullptr == _Node)
{
return;
}
Circit(_Node->pLeftChild);
Circit(_Node->pRightChild);
std::cout << _Node->pair.first << std::endl;
#include <iostream>
#include "BST.h"
template<typename T1, typename T2>
class MyTemplateClass
{
T1 m_Data1;
T2 m_Data2;
};
MyTemplateClass<int, float> obj;
#include <set>
#include <map>
using std::set;
using std::map;
using std::make_pair;
int main()
{
set<int> intset;
intset.insert(100);
intset.insert(150);
intset.insert(170);
intset.insert(125);
intset.insert(80);
intset.insert(90);
intset.insert(50);
// 100
// / \
// 80 150
// /\ /\
// 50 90 125 170
set<int>::iterator iter = intset.find(125);
iter = intset.find(124);
if (iter != intset.end())
{
}
else
{
}
// map 사용
map<int, int> intmap;
intmap.insert(make_pair(100, 1));
intmap.insert(make_pair(150, 2));
intmap.insert(make_pair(170, 3));
intmap.insert(make_pair(125, 4));
intmap.insert(make_pair(80, 5));
intmap.insert(make_pair(90, 6));
intmap.insert(make_pair(50, 7));
// 100-1
// / \
// 80-5 150-2
// / \ / \
// 50-7 90-6 125-4 170-3
map<int, int>::iterator mapiter = intmap.find(50);
if (mapiter != intmap.end())
{
(*mapiter).first;
(*mapiter).second;
}
// BST insert 테스트
BST<int, float> bst;
bst.insert(make_bstpair(100, 1.1f));
bst.insert(make_bstpair(150, 2.2f));
bst.insert(make_bstpair(50, 2.2f));
bst.insert(make_bstpair(170, 1.1f));
bst.insert(make_bstpair(125, 2.2f));
bst.insert(make_bstpair(25, 2.2f));
bst.insert(make_bstpair(75, 2.2f));
// 100
// 50 150
// 25 75 125 170
bst.Circit();
return 0;
}
#pragma once
#include <iostream>
// Binary Search Tree(BST)
template<typename T1, typename T2>
struct BSTPair
{
T1 first;
T2 second;
BSTPair()
{}
BSTPair(const T1& _first, const T2& _second)
: first(_first)
, second(_second)
{
}
};
template<typename T1, typename T2>
struct BSTNode
{
BSTPair<T1, T2> pair;
BSTNode* pParent;
BSTNode* pLeftChild;
BSTNode* pRightChild;
BSTNode()
: pParent(nullptr)
, pLeftChild(nullptr)
, pRightChild(nullptr)
{}
BSTNode(const BSTPair<T1, T2>& _pair, BSTNode* _Parent = nullptr, BSTNode* _LeftChild = nullptr, BSTNode* _RightChild = nullptr)
: pair(_pair)
, pParent(_Parent)
, pLeftChild(_LeftChild)
, pRightChild(_RightChild)
{}
};
template<typename T1, typename T2>
class BST
{
private:
BSTNode<T1, T2>* m_Root;
int m_CurCount;
public:
void insert(const BSTPair<T1, T2>& _pair);
public:
void Circit()
{
Circit(m_Root);
}
private:
void Circit(BSTNode<T1, T2>* _Node);
//class iterator;
//iterator find(const T1& _key);
public:
BST()
: m_Root(nullptr)
, m_CurCount(0)
{}
~BST()
{}
/*class iterator
{
};*/
};
template<typename T1, typename T2>
BSTPair<T1, T2> make_bstpair(const T1& _first, const T2& _second)
{
return BSTPair<T1, T2>(_first, _second);
}
template<typename T1, typename T2>
void BST<T1, T2>::insert(const BSTPair<T1, T2>& _pair)
{
BSTNode<T1, T2>* pNewNode = new BSTNode<T1, T2>(_pair);
if (nullptr == m_Root)
{
m_Root = pNewNode;
}
else
{
BSTNode<T1, T2>* pNode = m_Root;
while (true)
{
if (pNewNode->pair.first < pNode->pair.first)
{
if (nullptr == pNode->pLeftChild)
{
pNode->pLeftChild = pNewNode;
pNewNode->pParent = pNode;
break;
}
pNode = pNode->pLeftChild;
}
else if (pNode->pair.first < pNewNode->pair.first)
{
if (nullptr == pNode->pRightChild)
{
pNode->pRightChild = pNewNode;
pNewNode->pParent = pNode;
break;
}
pNode = pNode->pRightChild;
}
}
}
++m_CurCount;
}
template<typename T1, typename T2>
inline void BST<T1, T2>::Circit(BSTNode<T1, T2>* _Node)
{
if (nullptr == _Node)
{
return;
}
std::cout << _Node->pair.first << std::endl;
Circit(_Node->pLeftChild);
Circit(_Node->pRightChild);
}
1차 24.01.04
2차 24.01.05
3차 24.01.09
4차 24.01.10
5차 24.01.11