|자료구조| 3 | BinSearchTree | 이진 탐색 트리

2022. 3. 13. 19:14·PS/자료구조
no title

이진 탐색 트리 구현

이진 탐색트리 구현은 다음 깃허브를 참고했습니다
https://gist.github.com/harish-r/a7df7ce576dda35c9660

Visual Studio 솔루션 구성


구현부

노드 - 헤더 : Node.h

#pragma once
class Node
{
	friend class BinSearchTree;
public:
	int data;
	Node* lch;
	Node* rch;
};

이진 탐색 트리 - 헤더 : BinSearchTree.h

#pragma once
#include "Node.h"
class BinSearchTree
{
	friend class Node;
private:
	Node* mRoot;
	Node* mMakeEmpty(Node *_t);
	Node* mInsert(Node* _t, int _x);
	Node* mFindMin(Node *t);
	Node* mFindMax(Node *t);
	Node* mFind(Node *t, int _x);
	Node* mRemove(Node *t,Node *par, int _x);
	void mPreorder(Node* _t);
	void mInorder(Node* _t);
	void mPostorder(Node* _t);

public:
	BinSearchTree();
	~BinSearchTree();
	void insert(int _x);
	void remove(int _x);
	void search(int _x);
	void preorder();
	void inorder();
	void postorder();
};

이진 탐색 트리 - 메소드 : BinSearchTree.cpp

#include <bits/stdc++.h>
#include "BinSearchTree.h"
#include "Node.h"
using namespace std;

Node* BinSearchTree::mMakeEmpty(Node* _t) {
	//인풋은 트리 전체이다
	//재귀적으로 노드를 L R "Del" 하면서 삭제한다
		//삭제는 동적할당, 동적삭제 사용
	if (_t == NULL)
		return NULL;
	else {
		mMakeEmpty(_t->lch);
		mMakeEmpty(_t->rch);
		Node* curNode = _t;
		delete curNode;
		return NULL;
	}
}

Node* BinSearchTree::mInsert(Node* _t, int _x) {
	//인풋은 트리 전체이다.
	if (_t == NULL) {
		// 재귀적으로 왔는데 현재 위치에 Node가 없다면,또는 트리가 없다면
			//노드(트리) 생성
		_t = new Node;
		_t->data = _x;
		_t->lch = _t->rch = NULL;
	}
	// 트리가 존재한다면 
	//현재 노드와 비교하면서 재귀적으로
	//노드 들어간다.
	else if (_x < _t->data) {
		_t->lch = mInsert(_t->lch, _x);
	}
	else if (_t->data < _x) {
		_t->rch = mInsert(_t->rch, _x);
	}
	return _t;
}
Node* BinSearchTree::mFindMin(Node* _t){
	//이진탐색트리는 늘 죄측이 작은 숫자이다.
	if (_t == NULL)
		return NULL;
	else if (_t->lch == NULL)
		return _t;
	return mFindMin(_t->lch);
}
Node* BinSearchTree::mFindMax(Node* _t){
	//이진탐색트리는 늘 우측이 큰 숫자이다.
	if (_t == NULL)
		return NULL;
	else if (_t->rch == NULL)
		return _t;
	return mFindMax(_t->rch);
}
Node* BinSearchTree::mFind(Node* _t, int _x) {
	//현재 노드의 대소비교를 하며 수를 찾아간다.
	if (_t == NULL)
		return NULL;
	else if (_t->data == _x)
		return _t;
	else if (_x < _t->data) {
		return mFind(_t->lch, _x);
	}
	else if (_t->data < _x){
		return mFind(_t->rch, _x);
	}
}
Node* BinSearchTree::mRemove(Node* _t, Node *_par, int _x) {
	Node* temp;
	if (_t == NULL)
		return NULL;
	else if (_x < _t->data)
		_t->lch = mRemove(_t->lch, _par,_x);
	else if (_x > _t->data)
		_t->rch = mRemove(_t->rch, _par,_x);
	else if (_t->lch && _t->rch)
	{
		temp = mFindMin(_t->rch);
		_t->data = temp->data;
		_t->rch = mRemove(_t->rch, _par, _t->data);
	}
	else
	{
		temp = _t;
		if (_t->lch == NULL)
			_t = _t->rch;
		else if (_t->rch == NULL)
			_t = _t->lch;
		delete temp;
	}

	return _t;
}

BinSearchTree::BinSearchTree() {
	mRoot = NULL;
}
BinSearchTree::~BinSearchTree() {
	mRoot = mMakeEmpty(mRoot);
}
void BinSearchTree::insert(int _x) {
	mRoot = mInsert(mRoot, _x);
}
void BinSearchTree::remove(int _x) {
	mRoot = mRemove(mRoot, NULL, _x);
}
void BinSearchTree::search(int _x) {
	mRoot = mFind(mRoot ,_x);
}

void BinSearchTree::mPreorder(Node *_t) {
	if (_t == NULL)
		return;
	cout << _t->data << ' ';
	mPreorder(_t->lch);
	mPreorder(_t->rch);
}
void BinSearchTree::mInorder(Node* _t) {
	if (_t == NULL)
		return;
	mInorder(_t->lch);
	cout << _t->data << ' ';
	mInorder(_t->rch);
}
void BinSearchTree::mPostorder(Node* _t) {
	if (_t == NULL)
		return;
	mPostorder(_t->lch);
	mPostorder(_t->rch);
	cout << _t->data << ' ';
}
void BinSearchTree::preorder() {
	mPreorder(mRoot);
	cout << '\n';
};
void BinSearchTree::inorder() {
	mInorder(mRoot);
	cout << '\n';
}
void BinSearchTree::postorder() {
	mPostorder(mRoot);
	cout << '\n';
}

적용부

main.cpp

#include <bits/stdc++.h>
#include "BinSearchTree.h"
using namespace std;
/*
** Binary Search Tree implementation in C++
** Harish R
** Edit By DogGuyMan
*/

int main() {
    BinSearchTree t;
    t.insert(20);
    t.insert(25);
    t.insert(15);
    t.insert(10);
    t.insert(30);
    t.inorder();
    t.preorder();
    t.postorder();
    cout << '\n';
    t.remove(20);
    t.inorder();
    t.preorder();
    t.postorder();
    cout << '\n';
    t.remove(25);
    t.inorder();
    t.preorder();
    t.postorder();
    t.remove(30);
    t.inorder();
    t.preorder();
    t.postorder();
    cout << '\n';
    return 0;
}
저작자표시 (새창열림)

'PS > 자료구조' 카테고리의 다른 글

| 니앙팽이 - 자료구조 | 4 | 해시 테이블 노트  (0) 2022.03.13
| 니앙팽이 - 자료구조| 3 | 트리, 이진트리 노트  (0) 2022.03.13
| 니앙팽이 - 자료구조| 2 | 스택,큐 노트  (0) 2022.03.13
| 니앙팽이 - 자료구조| 1 | 연결 리스트 노트  (0) 2022.03.13
|자료구조| 1 | Double LinkedList_3 | 이중 연결 리스트  (0) 2022.03.13
'PS/자료구조' 카테고리의 다른 글
  • | 니앙팽이 - 자료구조 | 4 | 해시 테이블 노트
  • | 니앙팽이 - 자료구조| 3 | 트리, 이진트리 노트
  • | 니앙팽이 - 자료구조| 2 | 스택,큐 노트
  • | 니앙팽이 - 자료구조| 1 | 연결 리스트 노트
니앙팽이
니앙팽이
  • 니앙팽이
    니앙팽이 블로그
    니앙팽이
  • 전체
    오늘
    어제
    • 분류 전체보기 (126)
      • 그림그리기 (7)
      • 음악 (4)
        • FL Studio & MIDI (2)
        • 자작곡 (2)
      • 게임 (7)
        • 모바일 (0)
        • 스팀 (0)
        • 닌텐도 (0)
        • 개발 (7)
      • CS (44)
        • SW 공학 (27)
        • DB (7)
        • OS (9)
        • 네트워크 (1)
      • 팁 (9)
      • Language (21)
        • C# (8)
        • C&C++ (3)
        • 파이썬 메모 (3)
        • Javascript (7)
      • PS (0)
        • 알고리즘 (24)
        • 자료구조 (8)
        • 수학 (1)
        • 선형대수 (0)
        • 오토마타 (1)
        • 이산수학 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    파이썬
    unity
    가비지 콜렉터
    KAKAO
    유니티
    Stack
    디자인패턴
    알고리즘
    clip studio paint
    자료구조
    프로세스
    프로그래머스
    따라그리기
    연결리스트
    Javascript
    객체지향개발
    클립 스튜디오
    그림 연습
    노마드 코더
    c#
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
니앙팽이
|자료구조| 3 | BinSearchTree | 이진 탐색 트리
상단으로

티스토리툴바