Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.

Slides:



Advertisements
Similar presentations
Chapter 10, Section 10.3 Tree Traversal
Advertisements

Trees Chapter 11.
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
1 Tree Traversal Section 9.3 Longin Jan Latecki Temple University Based on slides by Paul Tymann, Andrew Watkins, and J. van Helden.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Chapter 10 Trees. Tree Definition 1. A tree is a connected undirected graph with no simple circuits. Theorem 1. An undirected graph is a tree if and.
1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Definition: A tree is a connected undirected graph with no simple circuits. Since a tree cannot have a.
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
Discrete Mathematics Transparency No. 8-1 Chapter 8 Trees.
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Let G be a pseudograph with vertex set V, edge set E, and incidence mapping f. Let n be a positive integer. A path of length n between vertex v and vertex.
May 5, 2015Applied Discrete Mathematics Week 13: Boolean Algebra 1 Dijkstra’s Algorithm procedure Dijkstra(G: weighted connected simple graph with vertices.
ساختمانهای گسسته دانشگاه صنعتی شاهرود – اردیبهشت 1392.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
May 1, 2002Applied Discrete Mathematics Week 13: Graphs and Trees 1News CSEMS Scholarships for CS and Math students (US citizens only) $3,125 per year.
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.
Foundations of Discrete Mathematics
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees – Chapter 9 Slides courtesy of Dr. Michael P. Frank University of Florida Dept. of Computer & Information Science & Engineering.
5.5.3 Rooted tree and binary tree  Definition 25: A directed graph is a directed tree if the graph is a tree in the underlying undirected graph.  Definition.
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.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
5.5.2 M inimum spanning trees  Definition 24: A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible.
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.
CS 103 Discrete Structures Lecture 23 Trees (1). Second Midterm Exam 1 st Lecture in December (same time as the lecture) 75 minute duration Will cover.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 11 TREES INTRODUCTION TO TREES ► A tree is a connected undirected graph with no simple circuit. ► An undirected graph is a tree if and only.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
Discrete Structures – CNS 2300 Text Discrete Mathematics and Its Applications (5 th Edition) Kenneth H. Rosen Chapter 9 Trees.
Discrete Mathematics Chapter 10 Trees. Outline 10.1 Introduction to Trees 10.2 Applications of Trees 10.3 Tree Traversal 10.4 Spanning Trees 10.5 Minimal.
Discrete Mathematics Chapter 10 Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Section10.1: Introduction to Trees
Discrete Mathematics Trees.
Applied Discrete Mathematics Week 15: Trees
CSCE 210 Data Structures and Algorithms
Chapter 5 : Trees.
Trees Chapter 11.
Paul Tymann and Andrew Watkins
Introduction to Trees Section 11.1.
Binary Trees, Binary Search Trees
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
And the Final Subject is…
Trees Chapter 11.
Binary Trees, Binary Search Trees
Presentation transcript:

Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth H. Rosen, published by McGraw Hill, Boston, MA, They are intended for classroom use only and are not a substitute for reading the textbook. Chapter 10

Tree A tree is a connected undirected graph with –No simple circuits –No multiple edges –No loops An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.

Which graphs are trees? a b c d e f a b c d e f a b c d e f a b c d e f YES YES NO NO

Forest What if there are no simple circuits but the graph is not connected? Each of the connected components is a tree The collection is called a forest.

Rooted Tree Specify a vertex as root, then direct each edge away from the root. The resulting tree is called a rooted tree. Root

Example Root a b c d e f a b c d e f a b c d e f

What if a different root is chosen? A different rooted tree results. Root a b c d e f a b c d e f a b c d e f a b c d e f

Tree Terminology If v is a vertex of tree T other than the root, the parent of v is the unique vertex u such that there is a directed edge from u to v. When u is the parent of v, v is called the child of u. If two vertices share the same parent, then they are called siblings.

Example a b c d e f g h i j k l m Root

Example a b c d e f g h i j k l m Siblings

Tree Terminology (Cont.) The ancestors of a vertex other than the root are the vertices in the path from the root to this vertex, excluding the vertex itself and including the root. The descendants of a vertex v are those vertices that have v as an ancestor.

Example a b c d e f g h i j k l m Ancestors of k

a b c d e f g h i j k l m Example Descendants of d

Tree Terminology (Cont.) A vertex with no children is called a leaf. Vertices with children are called internal vertices.

Example a b c d e f g h i j k l m Leaves

Example a b c d e f g h i j k l m Internal vertices

Tree Terminology (Cont.) If a is a vertex in a tree, the subtree with a as its root is: – the subgraph of the tree consisting of a and its descendants, and –all edges incident to these descendants.

Example a b c d e f g h i j k l m Subtree at dSubtree at b

Tree Terminology (Cont.) A rooted tree is called an m-ary tree if every internal vertex has no more than m children. A tree is called a full m-ary tree if every internal vertex has exactly m children. An m-ary tree with m  2 is called a binary tree.

Example What is the arity of this tree? Is this a full m-ary tree? This is a 2-ary, or binary, tree. Yes, this is a full binary tree, since every internal vertex has exactly 2 children.

Example What is the arity of this tree? Is this a full m-ary tree? This is a 3-ary tree. Yes, this is a full 3-ary tree, since every internal vertex has exactly 3 children.

Example What is the arity of this tree? Is this a full m-ary tree? This is a full 5-ary tree.

Example What is the arity of this tree? Is this a full m-ary tree? Some internal nodes have 2 children, but some have 3, so this is a 3-ary tree. It is not a full-3-ary tree, since one internal node has only 2 children.

Ordered Rooted Tree An ordered rooted tree is one where the children of each internal vertex are ordered. In an ordered binary tree, if an internal vertex has two children, then they are called left child and right child. The subtree rooted at the left child of a vertex is called the left subtree and subtree rooted at the right child of a vertex is called the right subtree.

Example Left child of d a b c d e h i f g j k l m

Example Right child of d a b c d e h i f g j k l m

Example a b c d e h i f g j k l m Left subtree of c

Example a b c d e h i f g j k l m Right subtree of c

Tree Terminology (Cont.) The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. What is the level of the root? 0 The height of a rooted tree is the maximum of the levels of the vertices.

Example a b c d e f g h i j k l m Levels Height = 3

Properties of Trees A tree with n vertices has n  1 edges. An full m-ary tree with i internal vertices contains n = mi + 1 vertices. A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h–1.

Example Is this tree balanced?

Example Is this tree balanced?

Example Is this tree balanced?

Tree Properties (Cont.) There are at most m h leaves in an m-ary tree of height h If an m-ary tree with l leaves is full and balanced, then its height is h =  log m l 

Homework Exercises Do Section 10.1 exercises # 1, 3, 5, 7, 9, 17, 19, 20, 27, 28

CSE 2813 Discrete Structures Chapter 10, Section 10.2 Applications of Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth H. Rosen, published by McGraw Hill, Boston, MA, They are intended for classroom use only and are not a substitute for reading the textbook.

Applications of Trees Binary Search Trees Decision Trees Prefix Codes (Huffman Coding) Game Trees

Full and Complete Binary Trees A full binary tree is a binary tree in which each node is either a leaf node or has degree 2 (i.e., has exactly 2 children). A complete binary tree is a full binary tree in which all leaves have the same depth. A nearly complete binary tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point.

Examples Full binary tree: Complete binary tree:

Binary Trees What is the smallest height possible in a binary tree of 7 nodes? How many leaf nodes does it have? height = 2 num. leaves = 4

Binary Trees What is the smallest height possible in a binary tree of 15 nodes? How many leaf nodes does it have? height = 3 num. leaves = 8

Binary Trees What is the smallest height possible in a binary tree of 31 nodes? How many leaf nodes does it have? height = 4 num. leaves = 16

Binary Trees What is the smallest height possible in a binary tree of (2 n ) - 1 nodes? The smallest height possible in a binary tree of (2 n ) - 1 nodes is n – 1. Example: a tree with 31 nodes has 2 5 – 1 nodes, so n = 5, and its height = (n – 1) = (5 – 1) = 4.

Binary Trees How many leaf nodes does a binary tree of (2 n ) - 1 nodes have? A tree with (2 n ) - 1 nodes has 2 n-1 leaves Example: A tree with 31 nodes has 2 5 – 1 nodes, so n = 5, and this tree has 2 n-1 = = 2 4 = 16 leaves.

Binary Trees Note the pattern here: In a completely filled binary tree with (2 n ) – 1 nodes, half of the nodes (rounding up) will be leaves. That is, (2 n ) / 2 nodes will be leaf nodes. And we can rewrite (2 n ) / 2 as 2 n-1.

Binary Trees Lemma: For any h  1, a binary tree which has more than 2 h-1 leaf nodes must have a height greater than h – 1. Example: If a binary tree has 17 leaf nodes, can it have a height of 4? No; a complete binary tree of height 4 has only 16 leaf nodes. A binary tree with 17 leaves must have a height greater than 4.

Binary Search Trees

Decision Trees

Prefix Codes

Huffman Codes Consider the problem of data compression. Suppose that we have a 100,000-character file that contains only 6 different characters, a - f. Some characters occur more frequently than others. Currently, the file is stored using a fixed-length code of 3 bits per character, where a = 000, b = 001,..., f = 101. This requires 300,000 bits to store the file. This slide and the next 53 are adapted from: Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 2nd edition, The MIT Press, McGraw-Hill, 2001.

Huffman Codes Huffman coding uses a variable-length code to compress the file. We can use a 0 to represent the most frequently- occurring letter in the file, which will save us two bits per occurrence. Huffman codes are prefix codes, which means that all bit patterns are unambiguous; this requires that the bit-patterns for our other letters be 3 or 4 bits long. Using Huffman coding, we can store the file in 2240,00 bits.

Huffman Codes abcdef Frequency of occurrence (in thousands) Codeword (fixed- length) Codeword (variable length)

Huffman Codes Since we are using a prefix code, we can simply concatenate our codewords together to produce our bitstring. For example, the string abc can be represented as This is unambiguous. Why?

Huffman Codes Only one character can begin with 0; that is a. So a must be the first character in our string. This leaves The next bit is a 1; five characters can begin with 1, so we look at the second bit. Two characters can begin with 10, so we look at the third bit. Only one character can begin with 101; that is b. This leaves 100. Again, looking at all three bits, we see that this character must be c.

Huffman Codes

Huffman Coding, using a “Greedy Algorithm”

Game Trees

CSE 2813 Discrete Structures Chapter 10, Section 10.3 Tree Traversal These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth H. Rosen, published by McGraw Hill, Boston, MA, They are intended for classroom use only and are not a substitute for reading the textbook.

Universal Address Systems To totally order the vertices of on orered rooted tree: 1.Label the root with the integer 0. 2.Label its k children (at level 1) from left to right with 1, 2, 3, …, k. 3.For each vertex v at level n with label A, label its k v children, from left to right, with A.1, A.2, A.3, …, A.k v. This labeling is called the universal address system of the ordered rooted tree.

Universal Address Systems

Traversal Algorithms A traversal algorithm is a procedure for systematically visiting every vertex of an ordered rooted tree –An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered The three common traversals are: –Preorder traversal –Inorder traversal –Postorder traversal

Traversal Let T be an ordered rooted tree with root r. Suppose T 1, T 2, …,T n are the subtrees at r from left to right in T. r T1T1 T2T2 TnTn

Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder

Preorder Traversal

Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:

The Preorder Traversal of T In which order does a preorder traversal visit the vertices in the ordered rooted tree T shown to the left? Preorder: Visit root, then visit subtrees left to right.

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved Preorder: Visit root, then visit subtrees left to right.

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

The Preorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn

Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:

The Inorder Traversal of T In which order does an inorder traversal visit the vertices in the ordered rooted tree T shown to the left? Inorder: Visit leftmost tree, visit root, visit other subtrees left to right.

The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved Inorder: Visit leftmost tree, visit root, visit other subtrees left to right.

The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

The Inorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Postorder Traversal Step 1: Visit T 1 in postorder Step 2: Visit T 2 in postorder. Step n: Visit T n in postorder Step n+1: Visit r r T1T1 T2T2 TnTn

Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:

The Postorder Traversal of T In which order does a postorder traversal visit the vertices in the ordered rooted tree T shown to the left? Postorder: Visit subtrees left to right, then visit root.

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved Postorder: Visit subtrees left to right, then visit root.

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

The Postorder Traversal of T © The McGraw-Hill Companies, Inc. all rights reserved

Representing Arithmetic Expressions Complicated arithmetic expressions can be represented by an ordered rooted tree –Internal vertices represent operators –Leaves represent operands Build the tree bottom-up –Construct smaller subtrees –Incorporate the smaller subtrees as part of larger subtrees

Example (x+y) 2 + (x-3)/(y+2) + x y 2  – x 3 + y 2 / +

Infix Notation +  – + / + 2 x y x 3 y 2 Traverse in inorder adding parentheses for each operation x + y ()  2 () + x – 3() / y + 2() () ()

Prefix Notation (Polish Notation) Traverse in preorder: x + y  2 + x – 3 / y  – + / + 2 x y x 3 y 2

Evaluating Prefix Notation In an prefix expression, a binary operator precedes its two operands The expression is evaluated right-left Look for the first operator from the right Evaluate the operator with the two operands immediately to its right

Example + / / – / / – / / / /

Postfix Notation (Reverse Polish) Traverse in postorder x + y  2 + x – 3 / y  – + / + 2 x y x 3 y 2

In an postfix expression, a binary operator follows its two operands The expression is evaluated left-right Look for the first operator from the left Evaluate the operator with the two operands immediately to its left Evaluating Postfix Notation

Example / 3 2 – / / 3 2 – / – / / /

Homework Exercises Do questions : 7, 9, 10, 12, 13, 15, 16, 17, 18, 23, 24

Conclusion In this chapter we have studied: –Trees –Applications of Trees –Tree Traversal