1 / 27

양승혁 Yang_ka@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.05. 02

자료 구조. Chap 7. 트 리. 양승혁 Yang_ka@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.05. 02. 목차. 트리 이진 트리 스레드 이진 트리 이진 탐색 트리. 트리. 트리 의 개념 트리 의 용어. 트리 (1/2). 개념 계층적인 구조를 나타내는 자료구조 트리는 부모 - 자식 관계의 노드들로 이룸 응용분야 계층적 조직 표현 파일 시스템 결정 트리. A. B. C. D. I. J. E. F. G. H.

yaphet
Télécharger la présentation

양승혁 Yang_ka@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.05. 02

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 자료 구조 Chap 7. 트 리 양승혁 Yang_ka@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.05. 02

  2. 목차 • 트리 • 이진 트리 • 스레드 이진 트리 • 이진 탐색 트리 IST (Information Sciences & Technology) Laboratory

  3. 트리 • 트리의개념 • 트리의용어 IST (Information Sciences & Technology) Laboratory

  4. 트리(1/2) • 개념 • 계층적인 구조를 나타내는 자료구조 • 트리는 부모-자식 관계의 노드들로 이룸 • 응용분야 • 계층적 조직 표현 • 파일 시스템 • 결정 트리 IST (Information Sciences & Technology) Laboratory

  5. A B C D I J E F G H 트리(2/2) Ex) 루트 : A Ex) B의 서브트리 : E, F, G Ex) 단말 노드: E, F, G, H, I, J Ex) 비단말 노드: A, B, C, D Ex) B레벨 : 2 Ex) 높이 : 3 Ex) B차수 : 3 Ex) 노드: A, B, C, D, E, F, G, H, I, J • 높이(height) • 트리의 최대 레벨 • 차수(degree) • 노드가 가지고 있는 자식 노드의 개수 • 트리의 용어 • 노드(node) • 트리의 구성요소 • 루트(root) • 부모가 없는 노드(A) • 서브트리(subtree) • 노드의 자손들로 이루어진 트리 • 단말노드(terminal node) • 자식이 없는 노드 • 비단말노드(nonterminal node) • 적어도 하나의 자식을 가지는 노드 • 레벨(level) • 트리의 각층의 번호 IST (Information Sciences & Technology) Laboratory

  6. 이진트리 • 이진 트리 • 성질 • 분류 • 표현 • 순회 • 연산 IST (Information Sciences & Technology) Laboratory

  7. 이진 트리(1/1) • 이진 트리 • 공집합 이거나 • 루트와 왼쪽 서브트리 오른쪽 서브트리로 구성된 노드들의 유한 집합 • 이진트리의 서브 트리디들은 모두 이진트리 IST (Information Sciences & Technology) Laboratory

  8. 이진 트리의성질(1/2) h-1 • 이진 트리의 성질 • 노드의 개수가 n개이면 간선의 개수는 n-1 • 높이가 h인 이진트리의 경우, 최소 h개의 노드를 가지며 최대 2개의 노드를 가진다. IST (Information Sciences & Technology) Laboratory

  9. 이진 트리의성질(2/2) • n개의 노드를 가지는 이진트리의 높이는 최대 n이거나 최소 IST (Information Sciences & Technology) Laboratory

  10. 이진 트리의분류(1/1) k • 이진 트리의 분류 • 포화 이진 트리(full binary tree) • 높이 k인 포화 이진 트리는 2 -1 개의 노드를 가짐 • 완전 이진 트리(complete binary tree) • 높이 k인이진 트리에k-1레벨까지 채워져 있고, k레벨에서는 왼쪽에서 오른쪽으로 순서대로 채워져 있는 이진 트리 • 기타 이진 트리 IST (Information Sciences & Technology) Laboratory

  11. 이진 트리의표현(1/1) structTreeNode{ int data; structTreeNode *left, *reght; } • 이진 트리의 표현 • 배열 표현법 • 모든 이진트리를포화이진트리라고 가정하고 각 노드에 번호를 붙여서 그 번호를 배열의 인덱스로 삼아 노드의 데이터를 배열에 저장하는 방법 • 링크 표현법 • 포인터를 이용하여 부모노드가 자식노드를 가리키게 하는 방법 IST (Information Sciences & Technology) Laboratory

  12. 이진 트리의순회(1/5) • 이진 트리의 순회 • 모든 노드들을 순차적으로 한 번씩 반문하여 자료를 목적에 맞게 처리하는 것 • 전위순회(preorder traversal) • 자손노드보다루트노드 먼저 방문 방법 • VLR • 중위순회(inorder traversal) • 왼쪽 자손, 루트, 오른쪽 자손 순으로 방문 방법 • LVR • 후위순회(postorder traversal) • 루트노트보다 자손을 먼저 방문 하는 방법 • LRV • 레벨순회 • 래렐이 낮은 것부터 방문 • VLR IST (Information Sciences & Technology) Laboratory

  13. 이진 트리의순회(2/5) preorder( TreeNode *root ){     if ( root ){ printf("%d", root->data );  preorder( root->left ); preorder( root->right ); } } 1 1 2 5 2 5 7 3 4 6 3 4 6 7 • 전위순회(preorder traversal) • 루트 노드를 방문 • 왼쪽 서브트리를 방문 • 오른 서브트리를 방문 IST (Information Sciences & Technology) Laboratory

  14. 이진 트리의순회(3/5) inorder( TreeNode*root ){     if ( root ){ inorder( root->left ); printf("%d", root->data );  inorder( root->right); } } 4 1 2 6 2 5 7 1 3 5 3 4 6 7 • 중위순회(inorder traversal) • 왼쪽 서브 트리를 방문 • 루트 노드를 방문 • 오른쪽 서브 트리를 방문 IST (Information Sciences & Technology) Laboratory

  15. 이진 트리의순회(4/5) postorder( TreeNode *root ){ if ( root ){ postorder( root->left) postorder( root->right) printf("%d", root->data ); }} 7 1 3 6 2 5 5 1 2 4 3 4 6 7 • 후위순회(postorder traversal) • 왼쪽 서브 트리의 모든 노드를 방문 • 오른쪽 서브 트리의 모든 노드를 방문 • 루트 노드를 방문 IST (Information Sciences & Technology) Laboratory

  16. 이진 트리의순회(5/5) Level_order( TreeNode *root ){ QueueType q; init(&q); if(!root) return; enqueue(&q,root); while(is_empty(&q){ root = dequeue(&q); printf(“%d”, root->data); if( root->left) enqueue(&q, root->data); if( root->right) enqueue(&q, root->data); } } 1 1 2 3 2 5 7 4 5 6 3 4 6 7 • 레벨 순회(level order) • 레벨 순으로 검사하는 순회 방법 • 동일한 레벨의 경우 좌에서 우로 방문 • 큐를 사용 IST (Information Sciences & Technology) Laboratory

  17. 이진 트리의연산(1/1) 노드의 개수 구하기 높이 구하기 intget_node_count(TreeNode *node) { int count=0; if( node != NULL ) count = 1 + get_node_count(node->left)+ get_node_count(node->right); return count; } 3 6 3 2 2 2 intget_height(TreeNode *node) { int height=0; if( node != NULL ) height = 1 + max(get_height(node->left), get_height(node->right)); return height; } • 각각의 서브 트리에대하여순환 호출한 다음, 반환되는 값에 1을 더하여 반환 • 서브트리에 대하여 순환호출하고 서브 트리들의반환값 중에서 최대값을 구하여 반환 1 1 1 1 1 1 • 이진 트리의연산 • 노드의개수 구하기 • 높이 구하기 IST (Information Sciences & Technology) Laboratory

  18. 스레드 이진 트리(1/2) typedefstructTreeNode { int data; structTreeNode *left, *right; intis_thread; //만약오른쪽링크가스레드이면 TRUE } TreeNode; 1 2 5 3 4 6 • 스레드 이진 트리 (threaded binary tree) • 이진트리의NULL 링크를  이용하여 순환호출 없이도 트리의노드들을 순회 • NULL 링크에 중위 순회시에 후속 노드인 중위 후속자(inorder successor)를 저장시킴 • 단말노드와비단말노드의 구별을 위히여is_thread필드 필요 IST (Information Sciences & Technology) Laboratory

  19. 스레드 이진 트리(2/2) TreeNode *find_successor(TreeNode *p){ // q 는 p의 오른쪽 포인터 TreeNode *q = p->right; // 만약 오른쪽 포인터가 NULL이거나 스레드이면 오른쪽 포인터를 반환 if(q==NULL || p->is_thread==TRUE) return q; // 만약 오른쪽 자식이면 다시 가장 왼쪽 노드로 이동 while(q->left != NULL) q=q->left; return q; } 4 1 2 6 2 5 1 3 5 Void thread_inorder(TreeNode *t){ TreeNode *q; q = t; while(q->left) q = q->left; do { printf(“%c”, q->data); q = find_successor(q); } while(q); } 3 4 6 • 스레드 이진 트리 • 후속자 찾기 • 중위 순회 IST (Information Sciences & Technology) Laboratory

  20. 이진 탐색 트리 • 이진 탐색 트리 • 탐색 연산 • 추가 연산 • 삭제 연산 • 분석 IST (Information Sciences & Technology) Laboratory

  21. 이진 탐색 트리(1/6) • 이진 탐색 트리 • 인진 탐색 트리의 성질을 만족하는 이진 트리 • 모든 원소의 키는 유일 • 왼쪽 서브 트리의 키들은 루트의 키보다 작음 • 오른쪽 서브 트리의 키들은 루트의 키보다 크음 • 왼쪽과 오른쪽 서브 트리도 이진 택색 트리 • 중위 순회 방법으로 순회 • 3, 7, 12, 18, 26, 27, 31 IST (Information Sciences & Technology) Laboratory

  22. 이진 탐색 트리(2/6) TreeNode *search(TreeNode *node, int key){ if( node =NULL) return NULL; if(key == node data) return node; else if ( key < node -> data) return search( node->left, key) else return search( node->right, key) } • 이진 탐색 트리 • 탐색 연산 • 특정 키 값을 찾기 위해 주언진 탐색키 값과 루트 노드의키값을 비교 • 비교한 결과가 같으면 탐색이 성공 • 키 값이 작으면 루트 노드의 왼쪽 자식 부터 시작 • 키 값이 크면 루트 노드의 오른쪽 자식 부터 시작 IST (Information Sciences & Technology) Laboratory

  23. 이진 탐색 트리(3/6) Void insert_node(TreeNode, int key){ TreeNode *p,*t; TreeNode *n; t=*root; p=NULL; while (t != NULL){ if( key < t->data) return; p = t; if( key < t-> data) t= t->left; else t = t->right; } n= (TreeNode *) malloc(sizeof(TreeNode)); if(n = NULL) return; n->data = key; n->left = n->right = NULL; if( p != NULL) if(key < p->data) p->left = n; else p->right = n; else *root = n; } • 이진 탐색 트리 • 삽입 연산 • 삽일할key값을 탐색 • 탐색이 실패하면 타색이 끝난 지점에 노드z를 삽입 IST (Information Sciences & Technology) Laboratory

  24. 이진 탐색 트리(4/6) • 이진 탐색 트리 • 삭제 연산 • 단말 노드 • 하나의 서브 트리를 가지고 있는 노드 • 두개의 서브 트리를 가지고 있는 노드 IST (Information Sciences & Technology) Laboratory

  25. 이진 탐색 트리(5/6) 단말 노드 if((t->left==NULL)&&(t->right==NULL)){ if( p!=NULL){ if( p->left ==t) p->left =NULL; else p->right =NULL; } else root = NULL; } void delete_node(TreeNode **root, int key){ TreeNode*p, *chile, *succ, *succ_p, *t; p = NULL; t = *root; //t를 탐색 while( t! = NULL && t != key) { p = t; t = ( key < t->data ) ? t->left : t->right; } //key 있는지 확인 if(( t->data==NULL){ printf("key is not in the tree"); return; } 두개의 서브 트리를 가지고 있는 노드 succ_p= t; succ= t->right; while(succ->left != NULL){ succ_p= succ; succ= succ -> left; } if( succ_p -> left == succ) succ_p-> left == succ->right; else succ_p->right = succ->right; t->data = succ->data; t = succ; } free(t); } 하나의 서브 트리를 가지고 있는 노드 else if((t->left==NULL)|| (t->right==NULL)){ child=(t->left!=NULL) ? t->left : t->right; if( p != NULL){ if(p->left == t) p->left = child; else p->right = child; } else *root = child; } • 삭제 연산 소스 IST (Information Sciences & Technology) Laboratory

  26. 이진 탐색 트리(6/6) • 이진 탐색 트리 분석 • 최선의 경우 • 이진트리가 균형적으로 생성되어 있는 경우 • h=log2n • 최악의 경우 • 한쪽으로 치우친 경사이진트리의 경우 • h=n • 순차탐색과 시간복잡도가 같다 IST (Information Sciences & Technology) Laboratory

  27. 감사합니다 Yang_ka@kunsan.ac.kr 군산대학교 정보통계학과 정보과학기술 연구실 2012.01.03 양승혁 Yang_ka@kunsan.ac.kr 군산대학교 통계컴퓨터과학과 정보과학기술 연구실 2012.05.02

More Related