Chapter 6 Introduction to Trees Internet Computing Laboratory
Chapter 6. Introduction to Trees Internet Computing Laboratory @ KUT Youn-Hee Han
1. Basic Tree Concepts Logical structures Chap. 3~5 Chap. 6~10 Chap. 11 Linear list Tree Graph Linear structures Non-linear structures
1. Basic Tree Concepts Tree consists of n n A finite set of nodes (vertices) A finite set of branches (edges, arcs) that connects the nodes w Degree of a node: # of branches n n n 4 In-degree: # of branch toward the node (# of upward branch) Out-degree: # of branch away from the node (# of downward branch) Every non-empty tree has a root node whose in-degree is zero. In-degree of all other nodes is 1 Data Structure
1. Basic Tree Concepts Definitions n n 트리의 구성 요소에 해당하는 A, B, C, . . . , F를 노드 (Node)라 함. A node is child of its predecessor w B의 바로 아래 있는 E, F를 B의 자식노드 (Child Node)라 함. n A node is parent of its successor nodes w B는 E, F의 부모노드 (Parent Node) w A는 B, C, D의 부모노드 (Parent Node)임. n 주어진 노드의 상위에 있는 노드들을 조상노드 (Ancestor Node)라 함. w B, A는 F의 조상노드임. n 어떤 노드의 하위에 있는 노드를 후손노드 (Descendant Node)라 함. w B, E, F, C, D는 A의 후손노드임. n Path w a sequence of nodes in which each node is adjacent to the next one 5 Data Structure
1. Basic Tree Concepts Definitions n 트리의 레벨(Level) w distance from the root w 루트노드를 레벨 0으로 하고 아래로 내려오면서 증가 n 트리의 높이(Height or Depth) w level of the leaf in the longest path from the root + 1 w (트리의 최대 레벨 수 + 1) = 트리의 높이(Height) w 루트만 있는 트리의 높이는 1 w 비어있는 트리 (empty tree)의 높이는 0 (=4) 7 Data Structure
1. Basic Tree Concepts Recursive Definitions of Tree n A tree is a set of nodes that either: w 1. is Empty, or w 2. Has a designated node, called the root, from which hierarchically descend zero or more subtrees, which are also trees. User Representations General tree Indented list Parenthetical listing A (B (C D) E F (G H I) ) 9 Data Structure
2. Binary Tree Definitions n n 임의의 노드가 둘 이상의 자식노드를 가지는 트리를 일반트리 (General Tree)라 함. 임의의 노드가 최대 두 개까지의 자식노드를 가질 수 있는 트리를 이진 트리 (Binary Tree)라 함. w a tree in which no node can have more than two subtrees w the child nodes are called left and right w Left/right subtrees are also binary trees 11 Data Structure
2. Binary Tree Examples of Binary Trees 13 Data Structure
2. Binary Tree Properties of Binary Tree n 가정(Assumption): 트리 내의 노드의 수: w Maximum Hight n Ex] Given N=3 in a binary tree, what is the maximum hight? w Minimum Hight n n Ex] Given N=3 in a binary tree, what is the minimum hight? 가정(Assumption): 트리의 높이: w Maximum Nodes Ex] Given H=3 in a binary tree, what is the maximum numbers of nodes? w Minimum Nodes n n 14 Ex] Given H=3 in a binary tree, what is the minimum numbers of nodes? Data Structure
2. Binary Tree Definitions n 15 삼진트리 (Ternary Tree) Data Structure
2. Binary Tree Balance n 검색에 있어서 다음 어느 Tree 가 효율이 더 좋을까? w 높이가 3인 Tree w 높이가 2인 Tree n The shorter the tree, the easier it is to locate any desired node in the tree Balance Factor n n n : the height of the left subtree : the heighr of the right subtree Balance Factor Balanced Binary Tree n 16 A tree with |B| 1 HL HR Data Structure
2. Binary Tree Complete Binary Tree (완벽 이진트리) n 높이가 h인 이진트리에서 모든 리프노드가 레벨 h-1에 있는 트리 n 리프노드 위쪽의 모든 노드는 반드시 두개의 자식노드를 거느려야 함. n 시각적으로 볼 때 포화될 정도로 다 차 들어간 모습. n A bianary tree with the maximum # of entries for its height n A binary tree in which all leaves (vertices with zero children) are at the same depth Complete binary tree의 Height가 H 일 때 node의 갯수가 17 Data Structure
2. Binary Tree – Binary Tree Traversal n n process each node once and only once in a predetermined sequence Two general approach w Depth-first traversal (depth-first search: DFS) w Breadth-first traversal (breadth-first search: BFS, level-order) 1 1 5 2 3 4 6 depth-first traversal 19 3 2 7 4 5 6 7 breadth-first traversal Data Structure
2. Binary Tree – Binary Tree Traversal Depth-first traversal n n The processing proceeds along a path from the root through one child to the most distant descendent of that first child before processing a second child. Preorder traversal (NLR) w Root left subtree right subtree n Inorder traversal (LNR) w Left subtree root right subtree n Postorder traversal (LRN) w Left subtree right subtree root 20 Data Structure
2. Binary Tree – Binary Tree Traversal Depth-first traversal n Preorder traversal (NLR) – 1/2 void pre. Order(root) { if (root == NULL) return; printf("%s", root->data); pre. Order(root->left); pre. Order(root->right); } 21 Data Structure
2. Binary Tree – Binary Tree Traversal Depth-first traversal n Preorder traversal (NLR) – 2/2 void pre. Order(root) { if (root == NULL) return; printf("%s", root->data); pre. Order(root->left); pre. Order(root->right); } 22 Data Structure
2. Binary Tree – Binary Tree Traversal Depth-first traversal n Inorder traversal (LNR) void in. Order(root) { if (root == NULL) return; in. Order(root->left); printf("%s", root->data); in. Order(root->right); } 23 Data Structure
2. Binary Tree – Binary Tree Traversal Depth-first traversal n Postorder traversal (LRN) void post. Order(root) { if (root == NULL) return; post. Order(root->left); post. Order(root->right); printf("%s", root->data); } 24 Data Structure
2. Binary Tree – Binary Tree Traversal Breadth-first traversal (=level-order traversal) Begins at the root node and explores all the neighboring nodes n Then for each of those nearest nodes, it explores their unexplored neighbor nodes, and so on, until it finds the goal Attempts to visit the node closest to the root that it has not already visited n 25 Data Structure
2. Binary Tree – Binary Tree Traversal Breadth-first traversal (=level-order traversal) void BForder(Tree. Node *root) { QUEUE *queue = NULL; Tree. Node *node = root; if (node == NULL) return; queue = create. Queue(); while(node){ process(node->data); if(node->left) enqueue(queue, node->left); if(node->right) enqueue(queue, node->right); if(!empty. Queue(queue)) dequeue(queue, (void**)&node); else node = NULL; } destroy. Queue(queue); } 26 Data Structure
2. Binary Tree – Tree Examples Expression Tree: a binary tree in which n n Each leaf is an operand Root and internal nodes are operators Subtrees are sub-expressions with root being an operator Traversal Methods w Infix, Postfix, Prefix 27 Data Structure
2. Binary Tree – Tree Examples Infix Traversal in Expression Tree void infix(Tree. Node *root) { if(root == NULL) return; if(root->left == NULL && root->right == NULL) printf("%s", root->data); else { printf("("); infix(root->left); printf("%s", root->data); infix(root->right); printf(")"); } } 28 Data Structure
2. Binary Tree – Tree Examples Postfix Traversal in Expression Tree void postfix(Tree. Node *root) { if(root == NULL) return; postfix(root->left); postfix(root->right); printf("%s", root->data); } abc+*d+ 29 Data Structure
2. Binary Tree – Tree Examples Prefix Traversal in Expression Tree void prefix(Tree. Node *root) { if(root == NULL) return; printf("%s", root->data); prefix(root->left); prefix(root->right); } +*a+bcd 30 Data Structure
2. Binary Tree – Tree Examples Huffman Code n Representation of characters in computer w w n 7 bits/char (ASCII) 2 bytes/char (KSC Hangul) 2 bytes/char (UNICODE) Isn’t there more efficient way to store text? Data compression based on frequency Huffman Code w Variable length coding w Assign short code for characters used frequently In a text, the frequency of the character E is 15%, and the frequency of the character T is 12% 31 Data Structure
2. Binary Tree – Tree Examples Huffman Code n Building Huffman tree 1. Organize entire character node in a row, ordered by frequency 2. Repeat until all nodes are connected into “One Binary Tree” n n n Find two nodes with the lowest frequencies Merge them and make a binary sybtree Mark the merged frequency into the root of the subtree 3. End of repeat 32 Data Structure
2. Binary Tree – Tree Examples Huffman Code n Building Huffman tree Huffman Tree 33 Data Structure
2. Binary Tree – Tree Examples Huffman Code n Getting Huffman code from Huffman tree w Assign code to each character according to the path from root to the character n Properties of Huffman code w The more frequently used a character, the shorter its code is w No code is a prefix of other code 34 Data Structure
2. Binary Tree – Tree Examples Huffman Code n Data Encoding for communication w AGOODMARKET (11 char. * 7 bits = 77 bits) w 0001101001011111000000101011011100111 (42 bits) n Decoding w 0001101001011111000000101011011100111 w AGOODMARKET 35 Data Structure
2. Binary Tree – Tree Examples Huffman Code n 36 Another Example 문자 빈도 A 2 B 18 C 9 D 30 E 9 F 36 문자 빈도 원래 크기 압축된 크기 차이 A 2 7*2=14 4*2=8 6 B 18 7*18=126 2*18=36 90 C 9 7*9=63 4*9=36 27 D 30 7*30=210 2*30=60 150 E 9 7*9=63 3*9=27 36 F 36 7*36=252 2*36=72 180 계 104 728 240 488 Data Structure
3. General Tree n 37 a tree in which each node can have an unlimited out-degree Data Structure
3. General Tree Insertion in General Tree n FIFO insertion w For a given parent node, insert the new node at the end of sibling list n LIFO insertion w For a given parent node, insert the new node at the beginning of sibling list 38 Data Structure
3. General Tree Insertion in General Tree n Key-sequenced insertion w places the new node in key sequence among the sibling nodes w Most common of the insertion rules in general tree w Similar to the insertion rule in a general ordered linked list 39 Data Structure
3. General Tree Deletion in General Tree n Deletion of only leaf node w A node cannot be deleted if it has any children n 40 Other rules for deletion… Data Structure
3. General Tree to Binary Tree n A general tree can be represented by a binary tree n 변환 이유 w Ex] 일반트리에서는 자식노드의 수가 몇 개가 있는지 예측 불가 n 변환 방법 w 부모노드 (Parent Node)는 무조건 첫 자식노드 (First Child Node)를 가리킴 w 첫 자식노드로(First Child Node)부터 일렬로 자매 노드 (Sibling Node)들을 연결 41 Data Structure
3. General Tree to Binary Tree 42 Data Structure
- Slides: 42