7 3 2 struct node struct node leftchild

  • Slides: 81
Download presentation

7. 3. 2 연속 리스트를 이용하는 방법 ※ 연결 리스트 노드 구조 struct node

7. 3. 2 연속 리스트를 이용하는 방법 ※ 연결 리스트 노드 구조 struct node { struct node *left_child; char data; struct node *right_child; }

7. 4 이진트리의 운행 ■ 전위 운행(pre-order)의 예(반복문 이용) 1. voiditerinorder (tree. Pointer ptr)

7. 4 이진트리의 운행 ■ 전위 운행(pre-order)의 예(반복문 이용) 1. voiditerinorder (tree. Pointer ptr) 2. { 3. int top = -1; 4. tree. Pointer stack[MAX_SIZE]; 5. for (; ; ) 6. { 7. for (; ptr=ptr->left. Child) 8. add(&top, ptr); 9. ptr = delete(&top); 10. if (!ptr) break; 11. printf("%c", ptr->data); 12. ptr= ptr->right. Child; 13. } 14. }

7. 4 이진트리의 운행 ■ 비 순환적 중위 운행 방법의 정의 1. #define maxsize

7. 4 이진트리의 운행 ■ 비 순환적 중위 운행 방법의 정의 1. #define maxsize 10 2. void nonrecursive_inorder(current_node) 3. struct node { 4. struct node *lchild; 5. char data; 6. struct node *rchild; }; 7. struct node *courrent_node; 8. { 9. /* 스택의 top 포인터 초기화 */ 10. int top = 0; 11. /* 부모 노드로 되돌아가기 위하여 사용되는 포이터용 스택 배열 정의 */ 12. struct node *stack[maxsize]; 13. enum boolean {FALSE, TRUE} done; 14. /* 반복 조건의 초기화 */ 15. done = TRUE; 16. do{ 17. /* 현재의 노드 주소를 스택에 저장한 후 왼쪽 서브트리로 이동 */ 18. while(current_node != NULL) { 19. top=top+1; 20. if(top>maxsize){

7. 4 이진트리의 운행 21. 22. 23. 24. 25. 26. 27. 28. 29. 30.

7. 4 이진트리의 운행 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. printf("stack overflows n"); exit(1); }; /* end if */ /* 스택에 push() */ stack[top]=current_node; /* 실제 왼쪽 서브트리로 이동 */ current_node=current_node -> lchild; }/* end while */ /* 근노드 출력 및 오른쪽 서브트리로 이동 */ if(top!=0) { current_node=stack[top]; /* 스택에서 pop() */ top=top-1; printf("%c", current_node -> data); current_node=current_node -> rchild; }; else done=FALSE; }/* end if */ }while(done); /* end do while */ }/* end nonrecursive_inorder */

7. 6. 1 삽입 조작의 예 ■ 이진트리와 연결 표현 DATA LCHILD RCHILD 1

7. 6. 1 삽입 조작의 예 ■ 이진트리와 연결 표현 DATA LCHILD RCHILD 1 + 2 5 2 / (10) 4 3 a NULL 4 b NULL 5 * 6 7 6 - 8 9 7 e NULL 8 c NULL 9 d NULL 10 (*) (3) (11)

7. 6. 1 삽입 조작의 예 ■ 이진트리와 연결 표현 DATA LCHILD RCHILD 1

7. 6. 1 삽입 조작의 예 ■ 이진트리와 연결 표현 DATA LCHILD RCHILD 1 + 2 (6) 2 / 10 4 3 a NULL 4 b NULL - 8 9 8 c NULL 9 d NULL 10 * 3 11 11 f (NULL) 5 6 7

7. 7 스레드 이진트리(Threaded Binary Tree)

7. 7 스레드 이진트리(Threaded Binary Tree)

7. 7. 1 스레드 이진트리의 표현법 ■ 스레드 이진트리의 노드 구조 ※ 각각의 노드

7. 7. 1 스레드 이진트리의 표현법 ■ 스레드 이진트리의 노드 구조 ※ 각각의 노드 구조의 필드 의미 ① left. Thread=FALSE이면, leftchild=정상적인 연결 필드 ② left. Thread=TRUE이면, leftchild=스레드 연결 필드 ③ right. Thread=FALSE이면, rightchild=정상적인 연결 필드 ④ right. Thread=TRUE이면, rightchild=스레드 연결 필드 1. 2. 3. 4. 5. 6. 7. struct Thread. Node { int info ; Node left ; Node righ. Thread; Node left. Thread; }

7. 7. 1 스레드 이진트리 ■ right. Thread(알고리즘) 1. struct Thread. Bin. Tree {

7. 7. 1 스레드 이진트리 ■ right. Thread(알고리즘) 1. struct Thread. Bin. Tree { 2. public void inorder (Node firstin) { 3. Node p; 4. p = firstin ; 5. while (p != null) { 6. System. out. println(p. info) ; 7. p = p. right. Thread; 8. } 9. } 10. } ■ 단노드의 포인터를 이용한 전위 순회 스레드 이진트리(알고리즘) 1. 2. 3. 4. 5. 6. 7. type nodeptr = ↑nodetype; nodetype = record left : nodeptr; threadflag: 0. . 1; info: integer; right: nodeptr end;

7. 7. 2 스레드 이진트리의 중위 운행 ■ 스레드 이진트리의 중위 운행 표현 예(알고리즘)

7. 7. 2 스레드 이진트리의 중위 운행 ■ 스레드 이진트리의 중위 운행 표현 예(알고리즘) 1. threaded. Poiner insucc(threaded. Pointer tree) 2. { 3. threaded. Pointer temp; 4. temp = tree->right. Child; 5. if (!tree->right. Thread) 6. while (!temp->left. Thread) 7. temp = temp->left. Child; 8. return temp; 9. } 11. void tinorder (threaded. Pointer tree) 12. { 13. threaded. Pointer temp = tree; 14. for (; ; ) 14. { 15. temp = insucc(temp); 16. if (temp == tree) break; 17. printf("%c", temp->data); 18. } 19. }

7. 7. 3 스레드 트리 노드의 삽입․삭제 ■ 스레드 이진트리의 알고리즘 1. struct Thread.

7. 7. 3 스레드 트리 노드의 삽입․삭제 ■ 스레드 이진트리의 알고리즘 1. struct Thread. Bin. Tree { 2. . . 3. public void insert(TNode X, TNode Y) { 4. Y. left = null; 5. Y. right = X. right; 6. Y. leftthread = X; 7. Y. rightthread = X. rightthread; 8. X. right = Y; 9. X. rightthread = Y; 10. } 11. };

7. 7. 3 스레드 트리 노드의 삽입․삭제 ■ 오른쪽 부트리가 없을 경우(알고리즘) 1. void

7. 7. 3 스레드 트리 노드의 삽입․삭제 ■ 오른쪽 부트리가 없을 경우(알고리즘) 1. void insert. Right (threaded. Pointer parent, threaded. Pointer child) 2. { 3. threaded. Pointer temp; 4. child->right. Child = parent->right. Child; 5. child->right. Thread = parent->right. Thread; 6. child->left. Child = parent; 7. child->left. Thread = true; 8. parent->right. Child = child; 9. parent->right. Thread = false; 10. if (!child->right. Thread) { 11. temp = insucc(child); 12. temp->left. Child = child; 13. } 14. } ■ 오른쪽 부트리가 있을 경우 오른쪽 노드 삽입 스레드 이진트리

7. 7. 3 스레드 트리 노드의 삽입․삭제 ■ 오른쪽 부트리가 있을 경우(알고리즘) 1. 2.

7. 7. 3 스레드 트리 노드의 삽입․삭제 ■ 오른쪽 부트리가 있을 경우(알고리즘) 1. 2. 3. 4. 5. 6. 7. 8. 9. type nodeptr = &nodetype; nodetype = record left : nodeptr; info : integer; right : nodeptr; leftthread : nodeptr; rightthread : nodeptr; parent : nodeptr end;