License. Discrete Mathematics. Tree. Topics. Definition tree: connected graph with no cycle. examples. c T. Uyar, A. Yayımlı, E.

Similar documents
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

March 20/2003 Jayakanth Srinivasan,

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

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

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees Algorhyme by Radia Perlman

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Introduction to Computers and Programming. Concept Question

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

TREES. Trees - Introduction

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

Chapter 20: Binary Trees

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

CMSC th Lecture: Graph Theory: Trees.

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

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

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

Binary Trees

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

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Binary Trees, Binary Search Trees

Programming II (CS300)

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

CSI33 Data Structures

Trees. Truong Tuan Anh CSE-HCMUT

CSI33 Data Structures

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

Warm Up. Use Kruskal s algorithm to find the minimum spanning tree and it s weight.

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

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

Foundations of Discrete Mathematics

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

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

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

Trees. CSE 373 Data Structures

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

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

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

Algorithms and Data Structures

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

BBM 201 Data structures

CHAPTER 11 Trees. 294 Chapter 11 Trees. f) This is a tree since it is connected and has no simple circuits.

Associate Professor Dr. Raed Ibraheem Hamed

INF2220: algorithms and data structures Series 1

12/5/17. trees. CS 220: Discrete Structures and their Applications. Trees Chapter 11 in zybooks. rooted trees. rooted trees

Graph Algorithms Using Depth First Search

CSE 21 Mathematics for Algorithm and System Analysis

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

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

CE 221 Data Structures and Algorithms

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

Trees. Tree Structure Binary Tree Tree Traversals

Major Themes. Trees. Types of Graphs. Which of the following are trees? G 3 G 1 G 2 G 4. Dr Papalaskari 1

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

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

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

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

Search Trees - 1 Venkatanatha Sarma Y

CSC148 Week 6. Larry Zhang

Cpt S 122 Data Structures. Data Structures Trees

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Discrete Mathematics. Chapter 7. trees Sanguk Noh

COMP 250 Fall binary trees Oct. 27, 2017

Successor/Predecessor Rules in Binary Trees

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Trees Rooted Trees Spanning trees and Shortest Paths. 12. Graphs and Trees 2. Aaron Tan November 2017

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

CS 441 Discrete Mathematics for CS Lecture 26. Graphs. CS 441 Discrete mathematics for CS. Final exam

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

Course Review for Finals. Cpt S 223 Fall 2008

Data Structures and Algorithms

Trees. Trees. CSE 2011 Winter 2007

Garbage Collection: recycling unused memory

Stacks, Queues and Hierarchical Collections

Advanced Tree Data Structures

Trees. Arash Rafiey. 20 October, 2015

NANYANG TECHNOLOGICAL UNIVERSITY SEMESTER II EXAMINATION MH 1301 DISCRETE MATHEMATICS TIME ALLOWED: 2 HOURS

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

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

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

EECS 281 Homework 3 KEY Fall 2004

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

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

CS350: Data Structures B-Trees

MIDTERM Preparation. CSS 343: Data Structures Algorithms, and Discrete Math II

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

Chapter 3 Trees. Theorem A graph T is a tree if, and only if, every two distinct vertices of T are joined by a unique path.

Notation Index. Probability notation. (there exists) (such that) Fn-4 B n (Bell numbers) CL-27 s t (equivalence relation) GT-5.

INF2220: algorithms and data structures Series 1

Data Structures Question Bank Multiple Choice

Trees. CSC 1300 Discrete Structures Villanova University

Transcription:

License c 2001-2016 T. Uyar, A. Yayımlı, E. Harmancı Discrete Mathematics Trees H. Turgut Uyar Ayşegül Gençata Yayımlı Emre Harmancı 2001-2016 You are free to: Share copy and redistribute the material in any medium or format Adapt remix, transform, and build upon the material Under the following terms: Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. NonCommercial You may not use the material for commercial purposes. ShareAlike If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. For more information: https://creativecommons.org/licenses/by-nc-sa/4.0/ Read the full license: https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode 1 / 55 2 / 55 Topics Tree Trees Introduction Rooted Trees Binary Trees Decision Trees Definition tree: connected graph with no cycle examples Tree Problems Minimum Spanning Tree 3 / 55 4 / 55

Tree Theorems Tree Theorems Theorem T is a tree (T is connected and contains no cycle). There is one and only one path between any two distinct nodes in T. T is connected, but if any edge is removed it will no longer be connected. T contains no cycle, but if an edge is added between any pair of nodes one and only one cycle will be formed. Theorem E = V 1 proof method: induction on the number of edges 5 / 55 6 / 55 Tree Theorems Tree Theorems Proof: Induction step. Proof: Base step. E = k + 1 E = 0 V = 1 E = 1 V = 2 E = 2 V = 3 assume that E = V 1 for E k remove edge (y, z): T 1 = (V 1, E 1 ), T 2 = (V 2, E 2 ) V = V 1 + V 2 = E 1 + 1 + E 2 + 1 = ( E 1 + E 2 + 1) + 1 = E + 1 7 / 55 8 / 55

Tree Theorems Tree Theorems Theorem T is a tree (T is connected and contains no cycle). T is connected and E = V 1. T contains no cycle and E = V 1. Theorem In a tree, there are at least two nodes with degree 1. Proof. 2 E = v V d v assume: only 1 node with degree 1: 2 E 2( V 1) + 1 2 E 2 V 1 E V 1 2 > V 1 9 / 55 10 / 55 Rooted Tree Node Level hierarchy between nodes creates implicit direction on edges: in and out degrees in-degree 0: root (only 1 such node) out-degree 0: leaf not a leaf: internal node level of node: distance from root parent: adjacent node closer to root (only 1 such node) child: adjacent nodes further from root sibling: nodes with same parent depth of tree: maximum level in tree 11 / 55 12 / 55

Rooted Tree Example Rooted Tree Example root: r leaves: x y z u v internal nodes: r p n t s q w parent of y: w children of w: y and z y and z are siblings Book C1 C2 C3 S1.1 S1.2 S3.1 S3.2 S3.2.1 S3.2.2 S3.3 13 / 55 14 / 55 Ordered Rooted Tree Lexicographic Order siblings ordered from left to right universal address system root: 0 children of root: 1, 2, 3,... v: internal node with address a children of v: a.1, a.2, a.3,... address A comes before address B if one of: A = x 1 x 2... x i x j... B = x 1 x 2... x i x k... x j comes before x k A = x 1 x 2... x i B = x 1 x 2... x i x k... 15 / 55 16 / 55

Lexicographic Order Example Binary Trees 0-1 - 1.1-1.2-1.2.1-1.2.2-1.2.3-1.2.3.1-1.2.3.2-1.3-1.4-2 - 2.1-2.2-2.2.1-3 - 3.1-3.2 T = (V, E) is a binary tree: v V [d v o {0, 1, 2}] T = (V, E) is a complete binary tree: v V [d v o {0, 2}] 17 / 55 18 / 55 Expression Tree Expression Tree Examples binary operations can be represented as binary trees root: operator, children: operands 7 a a + b mathematical expression can be represented as trees internal nodes: operators, leaves: variables and values 19 / 55 20 / 55

Expression Tree Examples Expression Tree Examples 7 a 5 (a + b) 3 7 a 5 (a + b) 3 21 / 55 22 / 55 Expression Tree Examples Expression Tree Traversals 1. inorder traversal: traverse left subtree, visit root, traverse right subtree t + u v w + x y z 2. preorder traversal: visit root, traverse left subtree, traverse right subtree 3. postorder traversal (reverse Polish notation): traverse left subtree, traverse right subtree, visit root 23 / 55 24 / 55

Inorder Traversal Example Preorder Traversal Example t + u v / w + x y z + t / u v + w x y z 25 / 55 26 / 55 Postorder Traversal Example Expression Tree Evaluation t u v w x y z + / + inorder traversal requires parantheses for precedence preorder and postorder traversals do not require parantheses 27 / 55 28 / 55

Postorder Evaluation Example Regular Trees t u v w x y z + / + 4 2 3 1 9 2 3 + / + 4 2 3 4 6 1 9 2 3 4 6 1 9 8 4 6 1 1 + 4 6 2 / 4 3 + 7 T = (V, E) is an m-ary tree: v V [d v o m] T = (V, E) is a complete m-ary tree: v V [d v o {0, m}] 29 / 55 30 / 55 Regular Tree Theorem Regular Tree Examples Theorem T = (V, E): complete m-ary tree n: number of nodes l: number of leaves i: number of internal nodes n = m i + 1 l = n i = m i + 1 i = (m 1) i + 1 how many matches are played in a tennis tournament with 27 players? every player is a leaf: l = 27 every match is an internal node: m = 2 number of matches: i = l 1 m 1 = 27 1 2 1 = 26 i = l 1 m 1 31 / 55 32 / 55

Regular Tree Examples Decision Trees how many extension cords with 4 outlets are required to connect 25 computers to a wall socket? every computer is a leaf: l = 25 every extension cord is an internal node: m = 4 number of cords: i = l 1 m 1 = 25 1 4 1 = 8 one of 8 coins is counterfeit (heavier) find counterfeit coin using a beam balance depth of tree: number of weighings 33 / 55 34 / 55 Decision Trees Decision Trees 35 / 55 36 / 55

Spanning Tree Kruskal s Algorithm T = (V, E ) is a spanning tree of G(V, E): T is a subgraph of G T is a tree V = V minimum spanning tree: total weight of edges in E is minimal 1. G = (V, E ), V =, E = 2. select e = (v 1, v 2 ) E E such that: E {e} contains no cycle, and wt(e) is minimal 3. E = E {e}, V = V {v 1, v 2 } 4. if E = V 1: result is G 5. go to step 2 37 / 55 38 / 55 Kruskal s Algorithm Example Kruskal s Algorithm Example minimum weight: 1 (e, g) E = {(e, g)} E = 1 minimum weight: 2 (d, e), (d, f ), (f, g) E = {(e, g), (d, f )} E = 2 39 / 55 40 / 55

Kruskal s Algorithm Example Kruskal s Algorithm Example minimum weight: 2 (d, e), (f, g) E = {(e, g), (d, f ), (d, e)} E = 3 minimum weight: 2 (f, g) forms a cycle minimum weight: 3 (c, e), (c, g), (d, g) (d, g) forms a cycle E = {(e, g), (d, f ), (d, e), (c, e)} E = 4 41 / 55 42 / 55 Kruskal s Algorithm Example Kruskal s Algorithm Example E = { (e, g), (d, f ), (d, e), (c, e), (b, e) } E = 5 E = { (e, g), (d, f ), (d, e), (c, e), (b, e), (a, b) } E = 6 43 / 55 44 / 55

Kruskal s Algorithm Example Prim s Algorithm total weight: 17 1. T = (V, E ), E =, v 0 V, V = {v 0 } 2. select v V V such that for a node x V e = (x, v), E {e} contains no cycle, and wt(e) is minimal 3. E = E {e}, V = V {x} 4. if V = V : result is T 5. go to step 2 45 / 55 46 / 55 Prim s Algorithm Example Prim s Algorithm Example E = V = {a} V = 1 E = {(a, b)} V = {a, b} V = 2 47 / 55 48 / 55

Prim s Algorithm Example Prim s Algorithm Example E = {(a, b), (b, e)} V = {a, b, e} V = 3 E = {(a, b), (b, e), (e, g)} V = {a, b, e, g} V = 4 49 / 55 50 / 55 Prim s Algorithm Example Prim s Algorithm Example E = {(a, b), (b, e), (e, g), (d, e)} V = {a, b, e, g, d} V = 5 E = { (a, b), (b, e), (e, g), (d, e), (f, g) } V = {a, b, e, g, d, f } V = 6 51 / 55 52 / 55

Prim s Algorithm Example Prim s Algorithm Example E = { (a, b), (b, e), (e, g), (d, e), (f, g), (c, g) } V = {a, b, e, g, d, f, c} V = 7 total weight: 17 53 / 55 54 / 55 References Required Reading: Grimaldi Chapter 12: Trees 12.1. Definitions and Examples 12.2. Rooted Trees Chapter 13: Optimization and Matching 13.2. Minimal Spanning Trees: The Algorithms of Kruskal and Prim 55 / 55