06 (algorithms) tree - clickseo insightalgorithms)_tree.pdf트리(1/3) 트리(tree) 트리의정의...

34
자료구조 & 알고리즘 트리 (Tree) 자료구조 & 알고리즘 트리 (Tree) Seo, Doo-Ok Clickseo.com [email protected]

Upload: others

Post on 17-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

자료구조 & 알고리즘

트리(Tree)

자료구조 & 알고리즘

트리(Tree)

Seo, Doo-Ok

[email protected]

Page 2: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

목 차

이진 트리

이진 탐색 트리

2

Page 3: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

트리 (1/3)

트리(Tree)

트리의 정의• 원소들 간에 1:多 관계를 가지는 비선형 자료구조

• 원소들 간에 계층관계를 가지는 계층형 자료구조

• 상위 원소에서 하위 원소로 내려가면서 확장되는 나무 모양의 구조

트리 구조의 예• 컴퓨터 디렉터리(Directory) 구조

• 기업 구조(Organization Chart)

• 족보(Family Tree)

• 결정 트리(Decision Tree)

3

Page 4: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

트리 (2/3)

일반 트리 구조

부모-자식 관계 : 노드(node) , 간선(edge)

4

A

C DB

GE F I JH

K L

루트 노드

Level 0

Level 1

Level 2

Level 3

형제 관계

단말 노드

Page 5: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

트리 (3/3)

일반 트리 구조 : 부분 트리

부분 트리(Subtree)

• 자식 노드들은 각각 독립하여 새로운 트리를 구성할 수 있다.

• 각 노드는 자식 노드 수만큼의 서브 트리를 갖는다.

5

A

EB

C D

F

H IG

Page 6: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리

이진 트리

이진 트리 구현

이진 트리 순회

이진 탐색 트리

6

Page 7: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 (1/4)

이진 트리(Binary Tree)

최대 두 개까지의 자식 노드를 가질 수 있는 트리• 하나의 노드는 0, 1, 혹은 2개의 서브 트리를 가질 수 있다.

• 좌 서브 트리(left subtree)

• 우 서브 트리(right subtree)

• 널 트리(null tree)

7

A

B

C D

E

F

left subtree right subtree

Page 8: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 (2/4)

포화 이진 트리(Full Binary Tree)

높이 h인 이진 트리에서 모든 단말 노드가 레벨 h에 있는 트리

8

A

CB

FD E G

H I J K L M N O

최대 노드 수 : 2h+1 - 1

Page 9: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 (3/4)

완전 이진 트리(Complete Binary Tree)

높이 (h-1) 까지는 포화 이진 트리이고,

마지막 레벨 h 에서, 왼쪽에서 오른쪽으로 단말 노드를 채운 것

9

A

CB

FD E G

H I J K L

Page 10: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 (4/4)

편향 이진 트리(Skewed Binary Tree)

이진 트리 중에서 최소 개수의 노드를 가지면서 왼쪽이나 오른쪽

서브 트리만 가지고 있는 트리

10

A

B

C

D

왼쪽 편향 이진 트리

B

C

D

A

오른쪽 편향 이진 트리

Page 11: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

11

이진 트리

이진 트리 구현

Page 12: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 구현 (1/5)

이진 트리의 순차 자료구조 표현 : 완전 이진 트리

완전 이진 트리의 배열 표현

12

ABCDEFGHIJKL

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

[9]

[10]

[11]

[12]

부모 노드의

인덱스 = 2

왼쪽 자식 노드의

인덱스 = 10

오른쪽 자식 노드의

인덱스 = 11

1

A

CB

FD E G

H I J K L

2 3

76

121110

54

98

Page 13: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 구현 (2/5)

이진 트리의 순차 자료구조 표현 : 편향 이진 트리

편향 이진 트리의 배열 표현

13

AB

C

D

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

[9]

[10]

[11]

[12]

A

B

C

D

1

2 3

7654

8 9 10 11 12 13 14 15

Page 14: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 구현 (3/5)

이진 트리의 연결 자료구조 표현

14

typedef struct _btreenode{

struct _btreenode *Llink;

int data;

struct _btreenode *Rlink;

}BTreeNode;

Llink data Rlink

왼쪽 자식 노드 오른쪽 자식 노드

Page 15: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 구현 (4/5)

이진 트리의 연결 자료구조 표현 : 완전 이진 트리

완전 이진 트리의 연결 자료 구조 형태

15

A

CB

ED

NULL H NULL NULL I NULL NULL J NULL NULL K NULL NULL L NULL

F NULL NULL G NULL

Page 16: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 구현 (5/5)

이진 트리의 연결 자료구조 표현 : 편향 이진 트리

편향 이진 트리의 연결 자료 구조 형태

16

A NULL

B NULL

C NULL

NULL D NULL

Page 17: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

17

이진 트리

이진 트리 순회

Page 18: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 순회 (1/4)

순회(traversal)

깊이 우선 순회 : 스택을 이용하여 구현

• 전위 순회(preorder traversal)

• 중위 순회(inorder traversal)

• 후위 순회(postorder traversal)

너비 우선 순회 : 큐를 이용하여 구현

• 다음 레벨의 노드들을 처리하기 전에 노드의 자식 모두를 처리

18

A

B C

Page 19: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 순회 (2/4)

깊이 우선 순회 : 전위 순회

전위 순회(preorder traversal)

19

preorder(T)

if(T ≠ NULL) then

{

visit T.data;

preorder(T.Llink);

preorder(T.Rlink);

}

end preorder()

A

B C

A B C

Page 20: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 순회 (3/4)

깊이 우선 순회 : 중위 순회

중위 순회(inorder traversal)

20

inorder(T)

if(T ≠ NULL) then

{

inorder(T.Llink)

visit T.data;

inorder(T.Rlink);

}

end inorder()

A

B C

B A C

Page 21: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 트리 순회 (4/4)

깊이 우선 순회 : 후위 순회

후위 순회(postorder traversal)

21

postorder(T)

if(T ≠ NULL) then

{

postorder(T.Llink)

postorder(T.Rlink);

visit T.data;

}

end postorder()

A

B C

B C A

Page 22: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리

이진 트리

이진 탐색 트리

이진 탐색 트리 연산

22

Page 23: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리

이진 탐색 트리(Binary Search Tree)

탐색을 위한 자료 구조

23

8

103

2 5 14

1611

root

왼쪽 서브 트리의 키 값 < 루트의 키 값 < 오른쪽 서브 트리의 키 값

Page 24: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (1/10)

이진 탐색 트리의 탐색 : 탐색 과정

24

8

103

2 5 14

1611

탐색할 데이터 : 원소 5

① 5 < 8

② 5 > 3

③ 5 == 5

Page 25: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (2/10)

이진 탐색 트리의 탐색 : 알고리즘

25

searchBST(T, data)temp ← T;

if (temp = NULL) then return NULL;

else if (data = temp.key) then return temp;

else if (data < temp.key) then return searchBST(temp.Llink, data);

else return searchBST(temp.Rlink, data);

end searchBST()searchBST(T, data)

temp ← T;while (temp ≠ NULL) do{

if (data = temp.key) then return temp;else if (data < temp.key) then temp ← temp.Llink;else temp ← temp.Rlink;

}return NULL;

end searchBST()

Page 26: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (3/10)

이진 탐색 트리의 삽입 : 삽입 과정

1. 삽입할 노드의 위치(부모 노드의 주소) 탐색

2. 노드 삽입

26

8

103

2 5 14

1611

삽입할 위치 탐색 시작 : 원소 4

① 4 < 8

② 4 > 3

③ 4 < 5

4

왼쪽 자식 노드 삽입

“탐색 실패가 결정 된 위치 ”

즉, 왼쪽 자식 노드의 위치가 삽입 할 자리가 된다.

Page 27: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (4/10)

이진 탐색 트리의 삽입 : 알고리즘

27

insertBST(T, data)temp ← T;

while (temp ≠ NULL) do

{

if (data = temp.key) then

return error;

parent ← temp;

if (data < temp.key) then temp ← temp.Llink;

else temp ← temp.Rlink;

}

new_Node ← makeNode(data);

if (T = NULL) then T ← new_Node;

else if (data < parent.key) then parent.Llink ← new_Node;

else parent.Rlink ← new_Node;

return T;

end insertBST()

Page 28: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (5/10)

이진 탐색 트리의 삭제 : 단말 노드 삭제

28

8

103

2 5 14

1611

삭제할 위치 탐색 시작 : 원소 4

① 4 < 8

② 4 > 3

③ 4 < 5

4 ④ 4 = 4

단말 노드 삭제

Page 29: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (6/10)

이진 탐색 트리의 삭제 : 하나의 자식 노드만 존재

1. 삭제할 노드의 탐색

29

8

103

2 5 14

1611

삭제할 위치 탐색 시작 : 원소 10

① 10 > 8

4

② 10 = 10

Page 30: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (7/10)

이진 탐색 트리의 삭제 : 하나의 자식 노드만 존재 (cont’d)

2. 삭제할 노드의 삭제 및 위치 조정

30

8

3

2 5 14

1611

노드 삭제

4

자식 노드 이동

Page 31: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (8/10)

이진 탐색 트리의 삭제 : 두 개의 자식 노드가 존재

1. 삭제할 노드의 탐색 및 후계자 노드 선정• 왼쪽 서브 트리에서 가장 큰 키 값을 가진 노드

• 오른쪽 서브 트리에서 가장 작은 키 값을 가진 노드

31

8

103

2 5 14

1611

노드 삭제

4

Page 32: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (9/10)

이진 탐색 트리의 삭제 : 두 개의 자식 노드가 존재

2. 트리 재구성 : 데이터 5를 가진 노드를 후계자로 선택한 경우

32

103

2 4 14

1611

① 후계자 선택

4

5

② 자식 노드 이동

Page 33: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

이진 탐색 트리 연산 (10/10)

이진 탐색 트리의 삭제 : 알고리즘

33

deleteBST(T, data)del ← 삭제할 노드;parent ← 삭제할 노드의 부모 노드;if (del = NULL) then return;if (del.Llink = NULL and del.Rlink = NULL) then {

if (parent.Llink = del) then parent.Llink ← NULL;else parent.Rlink ← NULL;

}else if (del.Llink ≠ NULL and del.Rlink ≠ NULL) {

min ← minNode(del.Rlink);del.key ← min.key;deleteBST(del.Rlink, del.key);

}else if (del.Llink = NULL or del.Rlink = NULL) then {

if (del.Llink ≠ NULL) then {if (parent.Llink = del) then parent.Llink ← del.Llink;else parent.Rlink ← del.Llink;

}else {

if (parent.Llink = del) then parent.Llink ← del.Rlink;else parent.Rlink ← del.Rlink;

}}end deleteBST()

Page 34: 06 (Algorithms) Tree - Clickseo InsightAlgorithms)_Tree.pdf트리(1/3) 트리(Tree) 트리의정의 • 원소들간에1:多관계를가지는비선형자료구조 • 원소들간에계층관계를가지는계층형자료구조

참고문헌

34

[1] 주우석, “IT CookBook, CㆍC++ 로 배우는 자료구조론”, 한빛아카데미, 2015.

[2] 문병로, “IT CookBook, 쉽게 배우는 알고리즘 - 관계 중심의 사고법”(개정판), 개정판, 한빛아카데미, 2018.

[3] 카사이 아사오, 진명조 역, “C 언어로 배우는 알고리즘 입문”, 한빛미디어, 2005.

[4] 카일 루든(Kyle Loudon), 허욱 역, “Algorithms with C : C로 구현한 알고리즘”, 한빛미디어, 2000.

[5] 이재규, “C 로 배우는 알고리즘 : 개념과 기본 알고리즘”, 도서출판 세화, 2007.

이 강의자료는 저작권법에 따라 보호받는 저작물이므로 무단 전제와 무단 복제를 금지하며,

내용의 전부 또는 일부를 이용하려면 반드시 저작권자의 서면 동의를 받아야 합니다.

Copyright © Clickseo.com. All rights reserved.