Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Similar documents
Data Structure - Binary Tree 1 -

Binary Trees, Binary Search Trees

Trees. (Trees) Data Structures and Programming Spring / 28

CHAPTER 5. Trees CHAPTER 5 1/70

Tree Data Structures CSC 221

IX. Binary Trees (Chapter 10)

TREES. Trees - Introduction

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

CSCI2100B Data Structures Trees

Analysis of Algorithms

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Binary Search Trees. What is a Binary Search Tree?

Binary Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Introduction to Computers and Programming. Concept Question

A set of nodes (or vertices) with a single starting point

March 20/2003 Jayakanth Srinivasan,


CSI33 Data Structures

CS301 - Data Structures Glossary By

Todays Lecture. Assignment 2 deadline: You have 5 Calendar days to complete.

Binary Trees. Height 1

DATA STRUCTURES USING C

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Trees, Binary Trees, and Binary Search Trees

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Data Structures Question Bank Multiple Choice

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

CSI33 Data Structures

Data Structures and Algorithms for Engineers

CE 221 Data Structures and Algorithms

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Trees and Tree Traversals. Binary Trees. COMP 210: Object-Oriented Programming Lecture Notes 8. Based on notes by Logan Mayfield

9. Heap : Priority Queue

Trees. A tree is a directed graph with the property

Trees. CSE 373 Data Structures

Data and File Structures Laboratory

Trees. Trees. CSE 2011 Winter 2007

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

CSE 230 Intermediate Programming in C and C++ Binary Tree

EE 368. Weeks 5 (Notes)

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Programming II (CS300)

CSC148 Week 6. Larry Zhang

Advanced Tree Data Structures

Algorithms. Deleting from Red-Black Trees B-Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Programming II (CS300)

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

INF2220: algorithms and data structures Series 1

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Data Structure. IBPS SO (IT- Officer) Exam 2017

Garbage Collection: recycling unused memory

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Tree Structures. A hierarchical data structure whose point of entry is the root node

Chapter 20: Binary Trees

Graphs V={A,B,C,D,E} E={ (A,D),(A,E),(B,D), (B,E),(C,D),(C,E)}

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Binary Trees and Binary Search Trees

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

Advanced Set Representation Methods

COMP : Trees. COMP20012 Trees 219

Trees. Truong Tuan Anh CSE-HCMUT

Binary Trees and Huffman Encoding Binary Search Trees

Data Structures and Algorithms

CS 171: Introduction to Computer Science II. Binary Search Trees

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Why Do We Need Trees?

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Algorithms and Data Structures

Algorithms. AVL Tree

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

COSC 2011 Section N. Trees: Terminology and Basic Properties

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

Course Review. Cpt S 223 Fall 2009

Data Structures And Algorithms

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Transcription:

Bioinformatics Programming EE, NCKU Tien-Hao Chang (Darby Chang) 1

Tree 2

A Tree Structure A tree structure means that the data are organized so that items of information are related by branches 3

Definition of a Tree Structure (recursive definition) A tree is a finite set of one or more nodes such that there is a specially designated node called root the remaining nodes are partitioned into n 0 disjoint set T 1,, T n, where each of these sets is a tree T 1,, T n are called the sub-trees of the root Every node in the tree is the root of some subtree 4

Tree Some Terminology node: the item of information plus the branches to each node degree: the number of sub-trees of a node degree of a tree: the maximum of the degree of the nodes in the tree terminal nodes (or leaf): nodes that have degree zero non-terminal nodes: nodes that don t belong to terminal nodes children: the roots of the sub-trees of a node X are X s children parent: X is the parent of its children siblings: children of the same parent are said to be siblings ancestors: all the nodes along the path from the root to that node level (of a node): defined by letting the root be at level one (if a node is at level l, then its children are at level l+1) height (or depth): the maximum level of any node in the tree 5

An example A is the root node B is the parent of D and E C is the sibling of B A B C D E F G D and E are the children of B D, E, F, G, I are external nodes, or leaves A, B, C, H are internal nodes The level of E is 3 The height (depth) of the tree is 4 The degree of node B is 2 The degree of the tree is 3 The ancestors of node I is A, C, H The descendants of node C is F, G, H, I H I Level 1 2 3 4 6

Representation of Trees List Representation we can write of Figure 5.2 as a list in which each of the sub-trees is also a list ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) the root comes first, followed by a list of sub-trees 7

8

Left child-right sibling representation 9

10

Binary Trees Binary trees are characterized by the fact that any node can have at most two branches Definition (recursive): A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left sub-tree and the right sub-tree Question Answer A A Thus the left sub-tree and the right sub-tree are distinguished Any tree can be transformed into a binary tree by left child-right sibling representation B B 11

The abstract data type of binary tree 12

Skewed and complete binary trees 13

Properties of Binary Trees Lemma 5.1 [Maximum number of nodes]: The maximum number of nodes on level i of a binary tree is 2 i-1, i 1 The maximum number of nodes in a binary tree of depth k is 2 k -1, k 1 Lemma 5.2 [Relation between number of leaf nodes and degree-2 nodes]: For any nonempty binary tree, T, if n 0 is the number of leaf nodes and n 2 is the number of nodes of degree 2, then n 0 = n 2 + 1 These lemmas allow us to define full and complete binary trees 14

Full/Complete Binary Tree A full binary tree of depth k is a binary tree of death k having 2 k -1 nodes, k 0 A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k From Lemma 5.1, the height of a complete binary tree with n nodes is log 2 (n+1) 15

16

Binary Tree Representations Using Array Lemma 5.3: If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1 i n, we have parent(i) is at i/2 if i 1 if i = 1, i is at the root and has no parent left_child(i) is at 2i if 2i n if 2i > n, then i has no left child right_child(i) is at 2i+1 if 2i+1 n if 2i +1 > n, then i has no left child 17

[1] [2] [3] [4] [5] [6] [7] A B C D E Level 2 Level 3 Level 1 A [1] B [2] C [3] D H [4] [5] [6] [7] 18

Binary Tree Representations using Array Drawbacks Waste spaces in the worst case, a skewed tree of depth k requires 2 k-1 spaces of these, only k spaces will be occupied Insertion or deletion of nodes from the middle of a tree requires the movement of potentially many nodes to reflect the change in the level of these nodes 19

20

Binary Tree Representations Using Link 21

22

Binary Tree Traversals How to traverse a tree or visit each node in the tree exactly once? There are 6 possible combinations of traversal LVR, LRV, VLR, VRL, RVL, RLV Adopt convention that we traverse left before right, only 3 traversals remain LVR (inorder) LRV (postorder) VLR (preorder) left_child data right_child L: moving left V: visiting node R: moving right 23

Binary Tree Arithmetic Expression Arithmetic Expression using binary tree inorder traversal (infix expression) A / B * C * D + E preorder traversal (prefix expression) + * * / A B C D E postorder traversal (postfix expression) A B / C * D * E + level order traversal + * E Answer * D / C A B 24

25

26

Level-order traversal, which requires a queue to implement 27

Copying binary trees, similar to postorder traversal 28

Testing equality: binary trees are equivalent if they have the same data and topology 29

Any Questions? 30

What is The time complexity of iter_inorder()? 31

Analysis of iter_inorder Non-recursive inorder traversal Let n be the number of nodes in the tree Time complexity: O(n) every node of the tree is placed on and removed from the stack exactly once Space complexity: O(n) equal to the depth of the tree which (skewed tree is the worst case) 32

Heap 33

Heap A max/min tree is a tree in which the key value in each node is no smaller (larger) than the key values in its children A max/min heap is a complete binary tree that is also a max/min tree Basic operations: creation of an empty heap insertion of a new element into a heap deletion of the largest/smallest element from the heap 34

35

36

Priority Queues Heaps are frequently used to implement priority queues Delete the element with highest (lowest) priority Insert the element with arbitrary priority Heaps is the only way to implement priority queue An example: Huffman coding 37

38

Deletion from a max heap 39

Any Questions? 40

Can We Use Array (ordered or unordered), list (ordered or unordered) to implement priority queues? What s A further complexities? question 41

42

Binary Search Trees 43

Binary Search Trees Heap is not suited for applications in which arbitrary elements are to be deleted from the element list deletion of the max/min element O(log 2 n) deletion of an arbitrary element O(n) search for an arbitrary element O(n) Definition of binary search tree: every element has a unique key the keys in a nonempty left/right sub-tree are smaller/larger than the key in the root of sub-tree the left and right sub-trees are also binary search trees 44

Heap maintains the orders vertically, while binary search tree maintains them horizontally 45

Search(25) Search(76) 44 17 88 32 65 97 28 54 82 29 76 80 46

47

O(height) 48

49

Binary Search Tree Deletion Three cases should be considered leaf delete one child delete and change the pointer to this child two child either the smallest element in the right sub-tree or the largest element in the left sub-tree 50

51

Height of a Binary Search Tree The height of a binary search tree with n elements can become as large as n It can be shown that when insertions and deletions are made at random, the height of the binary search tree is O(log 2 n) on the average Search trees with a worst-case height of O(log 2 n) are called balance search trees 52

Binary Search Trees Time Complexity Searching, insertion, deletion O(h), where h is the height of the tree Worst case skewed binary tree O(n), where n is the number of internal nodes Prevent worst case rebalancing scheme AVL, 2-3, and red-black tree 53

Complete Link In Out a symmetric matrix the tree Requirement - complete link algorithm - teamwork is encouraged - a report of how the work is split and why - time/space analyses - using C would be the best Bonus -output n clusters given n - single/average link 54

Deadline 2010/4/13 23:59 Zip your code, a step-by-step README of how to execute the code and anything worthy extra credit. Email to darby@ee.ncku.edu.tw. 55

Hierarchical Clustering Hierarchical clustering takes as input a set of points Produces a set of nested clusters organized as a hierarchical tree Can be visualized as a dendrogram a tree-like diagram that records the 4 sequences of merges 1 2 5 0.4 0.35 5 2 0.3 0.25 0.2 0.15 0.1 0.05 3 4 3 1 6 0 3 6 4 1 2 5 56

The method is summarized below: place all points into their own clusters while there is more than one cluster, do merge the closest pair of clusters The behavior of the algorithm depends on how closest pair of clusters is defined 57

Complete Link Distance between two clusters C i and C j is the maximum distance between any object in C i and any object in C j The distance is defined by the two most dissimilar objects D C, C ) = max d a C, b C ( i j a, b { } ab i j 1 2 3 4 5 1 0.00 0.10 0.90 0.35 0.80 2 0.10 0.00 0.30 0.40 0.50 3 0.90 0.30 0.00 0.60 0.70 4 0.35 0.40 0.60 0.00 0.20 5 0.80 0.50 0.70 0.20 0.00 1 2 3 4 5 58