typedef struct Tree Node int data struct Tree
- Slides: 73
링크의 구현 ¢ 노드는 구조체로 표현 ¢ 링크는 포인터로 표현 typedef struct Tree. Node { int data; struct Tree. Node *left, *right; } Tree. Node; 2016 -1학기 데이터구조 23
링크 표현법 프로그램 #include <stdio. h> #include <stdlib. h> #include <memory. h> typedef struct Tree. Node { int data; struct Tree. Node *left, *right; } Tree. Node; // n 1 // / | // n 2 n 3 void main() { Tree. Node *n 1, *n 2, *n 3; n 1= (Tree. Node *)malloc(sizeof(Tree. Node)); n 2= (Tree. Node *)malloc(sizeof(Tree. Node)); n 3= (Tree. Node *)malloc(sizeof(Tree. Node)); 2016 -1학기 데이터구조 24
전위순회 프로그램 ¢ 순환 호출을 이용한다. preorder(x) if x≠NULL then 2016 -1학기 print DATA(x); preorder(LEFT(x)); preorder(RIGHT(x)); 데이터구조 28
중위 순회 알고리즘 ¢ 순환 호출을 이용한다. inorder(x) if x≠NULL then 2016 -1학기 inorder(LEFT(x)); print DATA(x); inorder(RIGHT(x)); 데이터구조 31
후위 순회 알고리즘 ¢ 순환 호출을 이용한다. postorder(x) if x≠NULL then 2016 -1학기 postorder(LEFT(x)); postorder(RIGHT(x)); print DATA(x); 데이터구조 34
순회 프로그램 typedef struct Tree. Node { int data; struct Tree. Node *left, *right; } Tree. Node; // 15 // 4 20 // 1 16 25 Tree. Node n 1={1, NULL}; Tree. Node n 2={4, &n 1, NULL}; Tree. Node n 3={16, NULL}; Tree. Node n 4={25, NULL}; Tree. Node n 5={20, &n 3, &n 4}; Tree. Node n 6={15, &n 2, &n 5}; Tree. Node *root= &n 6; 2016 -1학기 데이터구조 36
// 중위 순회 inorder( Tree. Node *root ){ if ( root ){ inorder( root->left ); printf("%d", root->data ); inorder( root->right ); } } // 전위 순회 preorder( Tree. Node *root ){ if ( root ){ printf("%d", root->data ); preorder( root->left ); preorder( root->right ); } } 2016 -1학기 데이터구조 // 왼쪽서브트리 순회 // 노드 방문 // 오른쪽서브트리 순회 // 노드 방문 // 왼쪽서브트리 순회 // 오른쪽서브트리 순회 37
// 후위 순회 postorder( Tree. Node *root ){ if ( root ){ postorder( root->left ); postorder( root->right ); printf("%d", root->data ); } } // 왼쪽 서브 트리 순회 // 오른쪽 서브 트리 순회 // 노드 방문 void main() { inorder(root); preorder(root); postorder(root); } 2016 -1학기 데이터구조 38
레벨 순회 알고리즘 ¢ level_order(root) 1. 2. 3. 4. 5. 6. 7. 8. initialize queue; enqueue(queue, root); while is_empty(queue)≠TRUE do x← dequeue(queue); if( x≠NULL) then print DATA(x); enqueue(queue, LEFT(x)); enqueue(queue, RIGHT(x)); 2016 -1학기 데이터구조 40
수식 트리 알고리즘 evaluate(exp) 1. 2. 3. 4. 5. 6. if exp = NULL then return 0; else x←evaluate(exp->left); y←evaluate(exp->right); op←exp->data; return (x op y); 2016 -1학기 데이터구조 43
프로그램 typedef struct Tree. Node { int data; struct Tree. Node *left, *right; } Tree. Node; // + // * + // 14 16 25 Tree. Node n 1={1, NULL}; Tree. Node n 2={4, NULL}; Tree. Node n 3={'*', &n 1, &n 2}; Tree. Node n 4={16, NULL}; Tree. Node n 5={25, NULL}; Tree. Node n 6={'+', &n 4, &n 5}; Tree. Node n 7={'+', &n 3, &n 6}; Tree. Node *exp= &n 7; 2016 -1학기 데이터구조 44
int evaluate(Tree. Node *root) { if( root == NULL) return 0; if( root->left == NULL && root->right == NULL) return root->data; else { int op 1 = evaluate(root->left); int op 2 = evaluate(root->right); switch(root->data){ case '+': return op 1+op 2; case '-': return op 1 -op 2; case '*': return op 1*op 2; case '/': return op 1/op 2; } } return 0; } void main() { printf("%d", evaluate(exp)); } 2016 -1학기 데이터구조 45
디렉토리 용량 계산 프로그램 int calc_direc_size(Tree. Node *root) { int left_size, right_size; if ( root ){ left_size = calc_size( root->left ); right_size = calc_size(root->right ); return (root->data+left_size+right_size); } } void main() { Tree. Node n 4={500, NULL}; Tree. Node n 5={200, NULL}; Tree. Node n 3={100, &n 4, &n 5}; Tree. Node n 2={50, NULL}; Tree. Node n 1={0, &n 2, &n 3}; printf("디렉토리의 크기=%dn", calc_direc_size(&n 1)); } 2016 -1학기 데이터구조 47
이진 트리 연산: 높이 ¢ 서브트리에 대하여 순환 호출하고 서브 트리들의 반환값 중에서 최대값을 구하여 반환 2016 -1학기 int get_height(Tree. Node *node) { int height=0; if( node != NULL ) height = 1 + max(get_height(node->left), get_height(node->right)); return height; } 데이터구조 49
이진탐색트리에서의 탐색연산 search(x, k) if x=NULL then return NULL; if k=x->key then return x; else if k<x->key then return search(x->left, k); else return search(x->right, k); 2016 -1학기 데이터구조 56
순환적인 방법 //순환적인 탐색 함수 Tree. Node *search(Tree. Node *node, int key) { if ( node == NULL ) return NULL; if ( key == node->key ) return node; (1) else if ( key < node->key ) return search(node->left, key); (2) else return sear ch(node->right, key); (3) } 2016 -1학기 데이터구조 58
반복적인 방법 // 반복적인 탐색 함수 Tree. Node *search(Tree. Node *node, int key) { while(node != NULL){ if( key == node->key ) return node; else if( key < node->key ) node = node->left; else node = node->right; } return NULL; // 탐색에 실패했을 경우 NULL 반환 } 2016 -1학기 데이터구조 59
이진 탐색트리에서의 삽입 연산 insert_node(T, z) p←NULL; t←root; while t≠NULL do p←t; if z->key < p->key then t←p->left; else t←p->right; if p=NULL then root←z; else if z->key < p->key then p->left←z else p->right←z 2016 -1학기 // 트리가 비어있음 데이터구조 61
// 삭제 함수 void delete_node(Tree. Node **root, int key) { Tree. Node *p, *child, *succ_p, *t; // key를 갖는 노드 t를 탐색, p는 t의 부모노드 p = NULL; t = *root; // key를 갖는 노드 t를 탐색한다. while( t != NULL && t->key != key ){ p = t; t = ( key < t->key ) ? t->left : t->right; } // 탐색이 종료된 시점에 t가 NULL이면 트리안에 key가 없음 if( t == NULL ) { // 탐색트리에 없는 키 printf("key is not in the tree"); return; } 2016 -1학기 데이터구조 68
- Struct node int data struct node* next
- Typedef struct tree int info
- Typedef struct tree
- Typedef struct node
- Typedef struct node
- Typedef struct node
- Typedef struct tree int info
- Typedef struct tree int info
- Sum0
- Typedef struct node
- Typedef list
- Struct node int data
- Struct node int i float j
- What are structure
- Int max(int x int y)
- Interface calculator public int add class test
- Public void drawsquare(int x, int y, int len)
- Public int divide(int a int b)
- Typedef struct c
- Typedef struct union
- Typedef struct
- Struct listnode
- Typedef in structure
- H
- Typedef struct tag
- Typedef int
- Reference node and non reference node
- Reference node and non reference node
- Constructive interference
- Parallel spin
- C doubly linked list
- C deep copy struct
- Struct point int x y
- Struct point int x y
- Interface myinterface int foo(int x)
- Int main(int argc, char** argv)
- #include stdio.h int main() int i = 5
- 7팩토리얼
- Const arduino
- Int main int num 4
- Void swap
- Void f(int i) int j=0
- Int max
- Mainint
- Struct tree
- Tipe data struct
- Nodeleaf
- Typedef
- Typedef float
- Graphnn
- Omg uml
- C typedef enum
- Struct tag c
- Forward declaration of typedef
- Typedef
- Contoh struktur data array
- I %2
- String termasuk tipe data sederhana
- Veins of upper limb
- Median cubital vein
- Navsat transform node
- Abdominal wall lymph nodes
- Antecubital fossa lymph node
- Epitrochlear
- List adt
- Local lymph node assay
- After a node has prepared an lsp it must be disseminated to
- Lymph node palpation order
- Node in physics
- Classification of kubing instrument
- Linked list
- Edge coverage example
- Nodered https