제 5 장 트리

22
제 5 제 제제

description

제 5 장 트리. 5.4 추가연산 ( 트리 복사 ). tree_pointer copy(tree_pointer original) { tree_pointer temp; if(original){ temp = (tree_pointer) malloc(sizeof(node)); if(IS_FULL(temp)){ fprintf(stderr, ” The memory is full\n ” ); exit(1); } - PowerPoint PPT Presentation

Transcript of 제 5 장 트리

제 5 장 트리

5.4 추가연산 ( 트리 복사 )tree_pointer copy(tree_pointer original){

tree_pointer temp;if(original){temp = (tree_pointer) malloc(sizeof(node));if(IS_FULL(temp)){fprintf(stderr,”The memory is full\n”);exit(1);}temp->left_child = copy(original->left_child);temp->right_child = copy(original->right_child);temp->data = original->data;return temp;}return NULL;

}

5.4 추가연산 ( 트리 복사 )tree_pointer copy(tree_pointer original){

tree_pointer temp;if(original){temp = (tree_pointer) malloc(sizeof(node));if(IS_FULL(temp)){fprintf(stderr,”The memory is full\n”);exit(1);}temp->left_child = copy(original->left_child);temp->right_child = copy(original->right_child);temp->data = original->data;return temp;}return NULL;

}

*

+

E

* D

/ C

A B

original

5.4 추가연산 ( 동질성검사 )

int equal(tree_pointer first, tree_pointer second){

return((!first && !second)|| (first && second && (first->data == second->data) && equal(first->left_child, second->left_child) && equal(first->right_child, second->right_child));}

5.4 추가연산

만족성 문제– 변수 x1, x2, …, xn 과 연산자 ^(and), V(or), !

(not) 으로 이루어진 식– x1V(x2^!x3)

– 명제식의 만족성 문제 ?: 식의 값이 참이 되도록 변수에 값을 지정할

수 있는 방법이 있는가를 묻는 문제

5.4 추가연산

규칙– 변수 자체도 하나의 식이다– x,y 가 식일때 !x, x^y, xVy 도 식이다 .– 연산 순서는 !, ^, V 의 순 , 괄호 사용

연산– 중위순회 : x1V(x2^!x3)– 계산방법 : 전체식이 하나의 값이 될때까지

서브트리를 계산하면서 트리를 후위 순회– O(g2n), g 는 각 변수에 값을 대입하고 식을 계산

5.4 추가연산

x1^!x2V!x1^!x3V!x3

^

x1 !

x2

^

x3!

x1

V ^

x3

V

5.4 추가연산

노드구조 typedef enum {not, and, or, true, false} logical;typedef struct node *tree_pointer;typedef struct node{

tree_pointer left_child;logical data;short int value;tree_pointer right_child;

}리프노드의 node->data 는 이 노드가 나타내는 변수의 현재 값

5.4 추가연산

for(all 2n possible combinations){generate the next combination;replace the variables by their values;if (root->value){

printf(<combination>);return;

}}printf(“No satisfiable combination\n”);

void post_order_eval(tree_pointer node){

if(node){post_order_eval(node->left_child);post_order_eval(node->right_child);switch(node->data){case not: node->value=!node->right_child->value;

break;case and: node->value=node->right_child->value &&

node->left_child->value;break;

case or: node->value=node->right_child->value || node->left_child->value;

break;case true: node->value = TRUE;

break;case false: node->value=FALSE;}

}}

void post_order_eval(tree_pointer node){

if(node){post_order_eval(node->left_child);post_order_eval(node->right_child);switch(node->data){case not: node->value=!node->right_child->value;

break;case and: node->value=node->right_child->value &&

node->left_child->value;break;

case or: node->value=node->right_child->value || node->left_child->value;

break;case true: node->value = TRUE;

break;case false: node->value=FALSE;}

}}

^

x1 !

x2

^

x3!

x1

V ^

x3

V

5.5 스레드 이진트리

이진트리– 실제 포인터보다 더 많은 널 링크 존재– 2n 개의 링크중 n+1 개의 널링크 존재

이런 널 링크를 활용 <= 스레드 (thread) ( 널링크 => 다른링크를 가리키는 포인터 )

^

x1 !

x2

5.5 스레드 이진트리

스레드 (thread)– 널 링크 필드에 삽입된 포인터(1) ptr->left_child = NULL => ptr->left_child <= 중위순회시 P 의 prede

cessor 에 대한 포인터(2) ptr->right_child = NULL =>

ptr->right_child <= 중위순회시 P 의 successor 에 대한 포인터

5.5 스레드 이진트리

중위순회:HDIBEAFCG

A

B

D E

C

F G

H I

5.5 스레드 이진트리

스레드 이진 트리의 기억장소 표현

- left_thread = T left_child : normal pointer F left_child : thread- right_thread = T right_child : normal pointer F right_child : thread

left_thread left_child DATA right_child right_thread

5.5 스레드 이진트리

typedef struct threaded_tree *thread_pointer;typedef struct threaded_tree{

short int left_thread;threaded_pointer left_child;char data;threaded_pointer right_child;short int right_thread;

}

5.5 스레드 이진트리

제일왼쪽 제일오른쪽

A

B

D E

C

F G

H I

t . H . t t . I . t

f . D . f t . E . t

f . B . f

t . F . t t . G . t

f . C . f

f . A . f

f . -- . fptr

5.5 스레드 이진트리

스레드 이진트리의 중위순회void tinorder(threaded_pointer tree){

threaded_pointer temp=tree;for (;;){

temp=insucc(temp);if(temp=tree) break;printf(“%3c”, temp->data);

}}

5.5 스레드 이진트리

threaded_pointer insucc(threaded_pointer tree){

threaded_pointer temp;temp=tree->right_child;if(!tree->right_thread)

while(!temp->left_thread)temp=temp->left_child;

return temp;}

5.5 스레드 이진트리

If ptr->right_thread = FALSE: ptr 의 오른쪽 자식부터 시작하여 왼쪽

자식 링크를 따라 left_child=TRUE 인 노드를 찾음

과제물

스레드 이진 트리를 이용한 중위순회 프로그램 작성