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

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

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

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

March 20/2003 Jayakanth Srinivasan,

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

Introduction to Computers and Programming. Concept Question

An undirected graph is a tree if and only of there is a unique simple path between any 2 of its vertices.

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

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

EE 368. Weeks 5 (Notes)

TREES. Trees - Introduction

CS204 Discrete Mathematics. 11 Trees. 11 Trees

Trees Algorhyme by Radia Perlman

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

Binary Trees, Binary Search Trees

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

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

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

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

CE 221 Data Structures and Algorithms

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

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

Hamilton paths & circuits. Gray codes. Hamilton Circuits. Planar Graphs. Hamilton circuits. 10 Nov 2015

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

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.


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

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

Trees. Eric McCreath

Data Structures and Algorithms for Engineers

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

Binary Search Tree (2A) Young Won Lim 5/17/18

Trees. Trees. CSE 2011 Winter 2007

Binary Trees

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

Foundations of Discrete Mathematics

Graph and Digraph Glossary

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

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

v V Question: How many edges are there in a graph with 10 vertices each of degree 6?

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

CSI33 Data Structures

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

Garbage Collection: recycling unused memory

Analysis of Algorithms

Trees. Truong Tuan Anh CSE-HCMUT

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

CSI33 Data Structures

CS 151. Binary Trees. Friday, October 5, 12

6. Finding Efficient Compressions; Huffman and Hu-Tucker

CS301 - Data Structures Glossary By

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

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

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

CMSC th Lecture: Graph Theory: Trees.

1. Why Study Trees? Trees and Graphs. 2. Binary Trees. CITS2200 Data Structures and Algorithms. Wood... Topic 10. Trees are ubiquitous. Examples...

12 Abstract Data Types

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

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

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Data Structures and Algorithms

Associate Professor Dr. Raed Ibraheem Hamed

Algorithms and Data Structures

Formal Languages and Automata Theory, SS Project (due Week 14)

Trees, Binary Trees, and Binary Search Trees

CSE 214 Computer Science II Introduction to Tree

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

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

Algorithms. Deleting from Red-Black Trees B-Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

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

Data Structures Question Bank Multiple Choice

Programming II (CS300)

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

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

Binary Trees Fall 2018 Margaret Reid-Miller

CHAPTER 10 GRAPHS AND TREES. Alessandro Artale UniBZ - artale/

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

We ve all seen trees. No, not that sprightly spruce in your garden, but your family tree. Rachael

Trees. CSE 373 Data Structures

CSC Intro to Intelligent Robotics, Spring Graphs

An Introduction to Trees

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

Stacks, Queues and Hierarchical Collections

Crossing bridges. Crossing bridges Great Ideas in Theoretical Computer Science. Lecture 12: Graphs I: The Basics. Königsberg (Prussia)

Priority Queues and Binary Heaps

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

Properties of red-black trees

6. Finding Efficient Compressions; Huffman and Hu-Tucker Algorithms

Postfix (and prefix) notation

CSE 100 Advanced Data Structures

Laboratory Module Trees

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

Recursive Data Structures and Grammars

Data Structure - Binary Tree 1 -

TREES Lecture 10 CS2110 Spring2014

CIS265/ Trees Red-Black Trees. Some of the following material is from:

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

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

Transcription:

Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: o There is a unique simple path between any 2 of its vertices. o No loops. o No multiple edges.

Example 1 a b c e d f G1 : This graph is a Tree because it is a connected graph with no simple circuits

Example 2 a c e b d f G2: is not a tree because there is a cycle a, b, e, d, a

Example 3 a c e f b d G3: is not a tree because it s not connected. In this case it s called forest in which each connected component is a tree. Component 1: a, f Component 2: c, e, b, d

Forest An undirected graph without simple circuits is called a forest. You can think of it as a set of trees having disjoint sets of nodes.

Rooted (Directed) Trees A rooted tree is a tree in which one node has been designated the root and every edge is directed away from the root. You should know the following terms about rooted trees: Root, Parent, Child, Siblings, Ancestors, Descendents, Leaf, Internal node, Subtree.

Definitions a b c d y f g e Root: Vertex with in-degree 0 [Node a is the root]

Definitions Parent: Vertex u is a parent, such that there is directed edge from u to v. [b is parent of g and f ] Child: If u is parent of v, then v is child of u. [g and f are children of b ] Siblings: Vertices with the same parents. [f and g ] Ancestors: Vertices in path from the root to vertex v, excluding v itself, including the root. [Ancestors of g : b, a ]

Definitions Descendents: All vertices that have v as ancestors. [Descendents of b : f, g, y ] Leaf: Vertex with no children. [y, g, e, d ] Internal vertices: Vertices that have children. [a, b, c, f ] Subtree: Subgraphs consisting of v and its descendents and their incident edges. b Subtree rooted at b : f g y

Definitions Level (of v ) is length of unique path from root to v. [level of root = 0, level of b = 1, level of g = 2] Height is maximum of vertices levels. [ Height = 3]

m-ary Trees A rooted tree is called m-ary if every internal vertex has no more than m children. It is called full m-array if every internal vertex has exactly m children. A 2-ary tree is called a binary tree.

Example a b c d e f Full binary tree g Full 3-ary tree

Ordered Rooted Tree A rooted tree where the children of each internal node are ordered. In ordered binary trees, we can define: left child, right child left subtree, right subtree For m-ary trees with m > 2, we can use terms like leftmost, rightmost, etc.

Examples a b g Left subtree of g h Right subtree of g j c e h j k n m d f k n m Left child of c is d, Right child of c is f

Trees as Models Saturated Hydrocarbons and Trees. Graphs can be used to represent molecules, where atoms are represented by vertices and bonds between them by edges.

Representing Organizations The structure of a large organization can be modeled using a rooted tree. Each vertex in this tree represents a position in the organization. An edge from one vertex to another indicates that the person represented by the initial vertex is the (direct) boss of the person represented by the terminal vertex.

Computer File Systems Files in computer memory can be organized into directories. A directory can contain both files and subdirectories. The root directory contains the entire file system. Thus, a file system may be represented by a rooted tree, where the root represents the root directory, internal vertices represent subdirectories, and leaves represent ordinary files or empty directories.

Properties of Trees 1- A tree with n vertices has n - 1 edges. e.g. The tree in the figure has 14 vertices and 13 edges

Properties of Trees 2- A full m-ary tree with I internal vertices and L leaves contains: n = m I + 1 vertices n = I + L vertices e.g. The full binary tree in the figure has: Internal vertices I = 6 Leaves L = 7 Vertices 13 = (2)(6) + 1

Summary For a full m-ary tree: (i) Given n vertices, I = (n 1 ) / m internal vertices and L = n I = [(m 1) n + 1] / m leaves. (ii) Given I internal vertices, n = m I + 1 vertices and L = n I = (m 1) I + 1 leaves. (iii) Given L leaves, n = (m L 1) / (m 1) vertices and I = n L = (L 1) / (m 1) internal vertices. In the previous example: m = 2, n = 13, I = 6 and L = 7 (i) I = (13 1) / 2 = 6 and L = 13 6 = 7 (ii) n = 2 6 + 1 = 13 and L = 13 6 = 7 (iii) n = (2 7 1 )/ (2 1) = 13 and I = 13 7 = 6

Properties of Trees 3- The level of a vertex in a rooted tree is the length of the path from the root to the vertex (level of the root is 0) 4- The height of the rooted tree is the maximum of the levels of vertices (length of the longest path from the root to any vertex)

Balanced Trees Balanced Tree A rooted m-ary tree of height h is balanced if all leaves are at levels h or h - 1. Balanced Balanced Not Balanced

10.2 Applications of Trees Binary Search Trees Searching for items in a list is one of the most important tasks that arises in computer science. Our primary goal is to implement a searching algorithm that finds items efficiently when the items are totally ordered. This can be accomplished through the use of a binary search tree.

Binary Search Trees A binary search tree is a binary tree in which each child of a vertex is designated as a right or left child, no vertex has more than one right child or left child, and each vertex is labeled with a key, which is one of the items. Furthermore, vertices are assigned keys so that the key of a vertex is both larger than the keys of all vertices in its left subtree and smaller than the keys of all vertices in its right subtree.

Example Form a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry (using alphabetical order).

Mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry

Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solution. For instance, a binary search tree can be used to locate items based on a series of comparisons, where each comparison tells us whether we have located the item, or whether we should go right or left in a subtree. A rooted tree in which each internal vertex corresponds to a decision, with a subtree at these vertices for each possible outcome of the decision, is called a decision tree. The possible solutions of the problem correspond to the paths to the leaves of this rooted tree.

Prefix Codes Consider using bit strings of different lengths to encode letters. When letters are encoded using varying numbers of bits, some method must be used to determine where the bits for each character start and end. One way to ensure that no bit string corresponds to more than one sequence of letters is to encode letters so that the bit string for a letter never occurs as the first part of the bit string for another letter. Codes with this property are called prefix codes.

Example The tree in the figure represents the encoding of e by 0, a by 10, t by 110, n by 1110, s by 1111.

Huffman Codes On the left tree the word rate is encoded 001 000 011 100 On the right tree, the same word rate is encoded 11 000 001 10

10.3 Tree Traversal Traversal algorithms o Pre-order traversal o In-order traversal o Post-order traversal Prefix / Infix / Postfix notation

Traversal Algorithms Is visiting every vertex of ordered rooted tree. Pre-order: Root, Left, Right. In-order: Left, Root, Right. Post-order: Left, Right, Root.

Tree Traversals Pre-order traversal In-order traversal Post-order traversal

Tree Traversals Pre-order: Root, Left, Right. In-order: Left, Root, Right. Post-order: Left, Right, Root. Pre-order: a b e j k n o p f c d g l m h i In-order: j e n k o p b f a c l g m d h i Post-order: j n o p k e f b c l m g h i d a

Infix, Prefix, and Postfix Notation A tree can be used to represents mathematical expressions. Example: (( x + y )^2) + (( x 4) / 3) Prefix: + ^ + x y 2 / x 4 3 7/4/2013 Postfix: x y + 2 ^ x 4 3 / +

Prefix Codes Infix: In-order traversal of tree must be fully parenthesized to remove ambiguity. Example: x + 5 / 3 : (x + 5) / 3, x + (5 / 3) Prefix (polish): Pre-order traversal of tree (no parenthesis needed) Example: From the above tree + * + x y 2 / x 4 3 Postfix: Post-order traversal (no parenthesis needed) Example: From the above tree x y + 2 * x 4 3 / +

1. Evaluating a Prefix Expression: (Pre-order: Right to left)

2. Evaluating a Postfix Expression: (Post-order: Left to right)