Chapter 6. Introduction to Trees

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Binary Trees Chapter 6.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Ceng-112 Data Structures I 1 Chapter 7 Introduction to Trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Binary Search Trees Data Structures Ananda Gunawardena
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
Discrete Mathematics Chapter 5 Trees.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Foundation of Computing Systems Lecture 4 Trees: Part I.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Chapter 12 Abstract Data Type.
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Chapter 5 : Trees.
Tree.
Tree traversal from Introduction to Trees Chapter 6 Objectives
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
Find in a linked list? first last 7  4  3  8 NULL
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Trees, Binary Search Trees
Introduction to Trees Chapter 6 Objectives
Presentation transcript:

Chapter 6. Introduction to Trees Internet Computing Laboratory @ KUT Youn-Hee Han

1. Basic Tree Concepts Logical structures Chap. 3~5 Chap. 6~10 Linear list Tree Graph Linear structures Non-linear structures

1. Basic Tree Concepts 선형 자료 구조 비선형 자료 구조 리스트, 스택, 큐가 데이터 집합을 한 줄로 늘어 세운 선형 자료구조 비선형 자료 구조 트리(Tree)는 데이터 집합을 여러 갈래로 나누어 세운 비 선형 구조 비선형 구조 고안 동기 효율성 삽입, 삭제, 검색에 대해서 선형 구조보다 나은 시간적 효율을 보임 Data Structure

1. Basic Tree Concepts Tree consists of A finite set of nodes (vertices) A finite set of branches (edges, arcs) that connects the nodes Degree of a node: # of branches 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 트리의 구성 요소에 해당하는 A, B, C, ..., F를 노드 (Node)라 함. A node is child of its predecessor B의 바로 아래 있는 E, F를 B의 자식노드 (Child Node)라 함. A node is parent of its successor nodes B는 E, F의 부모노드 (Parent Node) A는 B, C, D의 부모노드 (Parent Node)임. 주어진 노드의 상위에 있는 노드들을 조상노드 (Ancestor Node)라 함. B, A는 F의 조상노드임. 어떤 노드의 하위에 있는 노드를 후손노드 (Descendant Node)라 함. B, E, F, C, D는 A의 후손노드임. Path a sequence of nodes in which each node is adjacent to the next one Data Structure

1. Basic Tree Concepts Definitions 자매노드 (Sibling Node): nodes with the same parent 같은 부모 아래 자식 사이에 서로를 자매노드 (Sibling Node)라 함. B, C, D는 서로 자매노드이며, E, F도 서로 자매노드임. 부모가 없는 노드 즉, 원조격인 노드를 루트 노드 (Root Node)라 함. C, D, E, F처럼 자식이 없는 노드를 리프노드 (Leaf Node)라 함. 리프노드를 터미널 노드 (Terminal Node)라고도 함. node with out-degree zero 리프노드 및 루트노드를 제외한 모든 노드를 내부노드 (Internal Node)라 함 Data Structure

1. Basic Tree Concepts Definitions 트리의 레벨(Level) distance from the root 루트노드를 레벨 0으로 하고 아래로 내려오면서 증가 트리의 높이(Height or Depth) level of the leaf in the longest path from the root + 1 (트리의 최대 레벨 수 + 1) = 트리의 높이(Height) 루트만 있는 트리의 높이는 1 비어있는 트리 (empty tree)의 높이는 0 (=4) Data Structure

1. Basic Tree Concepts Definitions 서브트리 (Subtree): any connected structure below the root 주어진 트리의 부분집합을 이루는 트리를 서브트리 (Subtree)라 함. 서브트리는 임의의 노드와 그 노드에 달린 모든 후손노드 (Descendant Node) 를 합한 것임. 임의의 트리에는 여러 개의 서브트리가 존재할 수 있음. B, E, F가 하나의 서브트리  Subtree B C 자체로서도 하나의 서브트리  Subtree C Subtree B can be divided into two subtrees, C and D Data Structure

1. Basic Tree Concepts Recursive Definitions of Tree A tree is a set of nodes that either: 1. is Empty, or 2. Has a designated node, called the the root, from which hierarchically descend zero or more subtrees, which are also trees. User Representations General tree Parenthetical listing Indented list A (B (C D) E F (G H I) ) Data Structure

1. Basic Tree Concepts 트리의 높이(Height)에 대한 다른 정의 1 + 루트노드에서 가장 먼 리프노드까지의 연결 링크(Link) 개수 트리의 높이(Height)에 대한 재귀적 정의 트리의 높이 = 1 + Max{왼쪽 서브트리의 높이, 오른쪽 서브트리의 높이} Height 1 Height 3 Height 4 Data Structure

2. Binary Tree Definitions 임의의 노드가 둘 이상의 자식노드를 가지는 트리를 일반트리 (General Tree)라 함. 임의의 노드가 최대 두 개까지의 자식노드를 가질 수 있는 트리를 이진트리 (Binary Tree)라 함. a tree in which no node can have more than two subtrees the child nodes are called left and right Left/right subtrees are also binary trees Data Structure

2. Binary Tree 이진트리의 재귀적 정의 (Recursive Definition) 1) 아무런 노드가 없는 트리이거나, 2) 가운데 노드를 중심으로 좌우로 나뉘되, 좌우 서브트리 (Subtree) 모두 이진트리다. 1)번 때문에, 아무런 노드가 없는 트리도 주어진 트리의 서브트리이며 동시에 이진트리가 됨. 1)번은 재귀호출의 Base-case에 해당 Data Structure

2. Binary Tree Examples of Binary Trees Data Structure

2. Binary Tree Properties of Binary Tree 가정(Assumption): 트리 내의 노드의 수: Maximum Hight Ex] Given N=3 in a binary tree, what is the maximum hight? Minimum Hight Ex] Given N=3 in a binary tree, what is the minimum hight? 가정(Assumption): 트리의 높이: Maximum Nodes Ex] Given H=3 in a binary tree, what is the maximum numbers of nodes? Minimum Nodes Ex] Given H=3 in a binary tree, what is the minimum numbers of nodes? Data Structure

2. Binary Tree Definitions 삼진트리 (Ternary Tree) Data Structure

2. Binary Tree Balance Balance Factor Balanced Binary Tree The shorter the tree, the easier it is to locate any desired node in the tree Balance Factor : the height of the left subtree : the heighr of the right subtree Balanced Binary Tree A tree with |B|  1 HL HR Data Structure

2. Binary Tree Complete Binary Tree (완벽 이진트리) 높이가 h인 이진트리에서 모든 리프노드가 레벨 h-1에 있는 트리 리프노드 위쪽의 모든 노드는 반드시 두개의 자식노드를 거느려야 함. 시각적으로 볼 때 포화될 정도로 다 차 들어간 모습. A bianary tree with the maximum # of entries for its height A binary tree in which all leaves (vertices with zero children) are at the same depth Complete binary tree의 Height가 H 일 때  node의 갯수가 Data Structure

2. Binary Tree Nearly Complete Binary Tree 레벨 (L-1)까지는 완벽 이진트리 하나라도 건너뛰고 채워지면 안됨. Complete Binary Tree 이면 Nearly Complete Binary Tree 이다. 그러나 역은 성립 안 됨. Complete binary tree의 노드의 수가 N 일 때  height는 Data Structure

2. Binary Tree – Binary Tree Traversal process each node once and only once in a predetermined sequence Two general approach Depth-first traversal (depth-first search: DFS) Breadth-first traversal (breadth-first search: BFS, level-order) 1 4 6 2 5 7 3 1 5 6 2 3 7 4 depth-first traversal breadth-first traversal Data Structure

2. Binary Tree – Binary Tree Traversal Depth-first traversal 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) Root  left subtree  right subtree Inorder traversal (LNR) Left subtree  root  right subtree Postorder traversal (LRN) Left subtree  right subtree  root Data Structure

2. Binary Tree – Binary Tree Traversal Depth-first traversal Preorder traversal (NLR) – 1/2 void preOrder(root) { if (root == NULL) return; printf("%s", root->data); preOrder(root->left); preOrder(root->right); } Data Structure

2. Binary Tree – Binary Tree Traversal Depth-first traversal Preorder traversal (NLR) – 2/2 void preOrder(root) { if (root == NULL) return; printf("%s", root->data); preOrder(root->left); preOrder(root->right); } Data Structure

2. Binary Tree – Binary Tree Traversal Depth-first traversal Inorder traversal (LNR) void inOrder(root) { if (root == NULL) return; inOrder(root->left); printf("%s", root->data); inOrder(root->right); } Data Structure

2. Binary Tree – Binary Tree Traversal Depth-first traversal Postorder traversal (LRN) void postOrder(root) { if (root == NULL) return; postOrder(root->left); postOrder(root->right); printf("%s", root->data); } 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 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 Data Structure

2. Binary Tree – Binary Tree Traversal Breadth-first traversal (=level-order traversal) void BForder(TreeNode *root) { QUEUE *queue = NULL; TreeNode *node = root; if (node == NULL) return; queue = createQueue(); while(node){ process(node->data); if(node->left) enqueue(queue, node->left); if(node->right) enqueue(queue, node->right); if(!emptyQueue(queue)) dequeue(queue, (void**)&node); else node = NULL; } destroyQueue(queue); Data Structure

2. Binary Tree – Tree Examples Expression Tree: a binary tree in which Each leaf is an operand Root and internal nodes are operators Subtrees are sub-expressions with root being an operator Traversal Methods Infix, Postfix, Prefix Data Structure

2. Binary Tree – Tree Examples Infix Traversal in Expression Tree void infix(TreeNode *root) { if(root == NULL) return; if(root->left == NULL && root->right == NULL) printf("%s", root->data); else { printf("("); infix(root->left); infix(root->right); printf(")"); } Data Structure

2. Binary Tree – Tree Examples Postfix Traversal in Expression Tree void postfix(TreeNode *root) { if(root == NULL) return; postfix(root->left); postfix(root->right); printf("%s", root->data); } abc+*d+ Data Structure

2. Binary Tree – Tree Examples Prefix Traversal in Expression Tree void prefix(TreeNode *root) { if(root == NULL) return; printf("%s", root->data); prefix(root->left); prefix(root->right); } +*a+bcd Data Structure

2. Binary Tree – Tree Examples Huffman Code Representation of characters in computer 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 Variable length coding 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% Data Structure

2. Binary Tree – Tree Examples Huffman Code Building Huffman tree Organize entire character node in a row, ordered by frequency Repeat until all nodes are connected into “One Binary Tree” Find two nodes with the lowest frequencies Merge them and make a binary sybtree Mark the merged frequency into the root of the subtree End of repeat Data Structure

2. Binary Tree – Tree Examples Huffman Code Building Huffman tree Huffman Tree Data Structure

2. Binary Tree – Tree Examples Huffman Code Getting Huffman code from Huffman tree Assign code to each character according to the path from root to the character Properties of Huffman code The more frequently used a character, the shorter its code is No code is a prefix of other code Data Structure

2. Binary Tree – Tree Examples Huffman Code Data Encoding for communication AGOODMARKET (11 char. * 7 bits = 77 bits)  00011010001001011111000000101011011100111 (42bits) Decoding 00011010001001011111000000101011011100111  AGOODMARKET Data Structure

2. Binary Tree – Tree Examples Huffman Code Another Example 문 자 빈 도 A 2 B 18 C 9 D 30 E 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 3*9=27 36 F 7*36=252 2*36=72 180 계 104 728 240 488 Data Structure

3. General Tree General Tree a tree in which each node can have an unlimited out-degree Data Structure

3. General Tree Insertion in General Tree FIFO insertion For a given parent node, insert the new node at the end of sibling list LIFO insertion For a given parent node, insert the new node at the beginning of sibling list Data Structure

3. General Tree Insertion in General Tree Key-sequenced insertion places the new node in key sequence among the sibling nodes Most common of the insertion rules in general tree Similar to the insertion rule in a general ordered linked list Data Structure

3. General Tree Deletion in General Tree Deletion of only leaf node A node cannot be deleted if it has any children Other rules for deletion… Data Structure

3. General Tree General Tree to Binary Tree A general tree can be represented by a binary tree 변환 이유 Ex] 일반트리에서는 자식노드의 수가 몇 개가 있는지 예측 불가 변환 방법 부모노드 (Parent Node)는 무조건 첫 자식노드 (First Child Node)를 가리킴 첫 자식노드로(First Child Node)부터 일렬로 자매 노드 (Sibling Node)들을 연결 Data Structure

3. General Tree General Tree to Binary Tree Data Structure