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.

Slides:



Advertisements
Similar presentations
Trees Chapter 11.
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
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.
Discrete Structures Lecture 13: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
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:
CS 171: Introduction to Computer Science II
Trees Chapter 8.
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.
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.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
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.
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.
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
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.
Searching Trees – Page 1CSCI 1900 – Discrete Structures CSCI 1900 Discrete Structures Searching Trees Reading: Kolman, Section 7.3.
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.
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
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 – Chapter 9 Slides courtesy of Dr. Michael P. Frank University of Florida Dept. of Computer & Information Science & Engineering.
Trees Dr. B. Prabhakaran Application Examples Useful for locating items in a list Used in Huffman coding algorithm Study games like.
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.
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.
Discrete Mathematics Chapter 5 Trees.
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 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)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
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
Trees Chapter 11.
Introduction to Trees Section 11.1.
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
Trees.
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
And the Final Subject is…
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Trees Chapter 11.
Presentation transcript:

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 all lectures delivered before the exam date Will consist of MCQ’s, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having page answers.

Chapter 11

Chapter Summary Introduction to Trees Applications of Trees Tree Traversal

Section 11.1

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

Trees A tree is an undirected, connected graph with no simple circuits They are called trees because they look like trees. For example, family trees are graphs that represent genealogical chart

Trees as Models: Examples Chemistry: Trees were introduced for counting the number of isomers of saturated hydrocarbons. The two isomers of butane are shown at the right Computer Science: The organization of a computer file system into directories, subdirectories, and files is naturally represented as a tree Business: Trees are used to represent the structure of organizations

Which Graphs are Trees? Theorem:An undirected graph is a tree iff there is a unique, simple path between any two of its vertices G 1 is a tree as it is a connected graphs with no simple circuits G 2 is a tree as it is a connected graphs with no simple circuits G 3 is not a tree as e, b, a, d, e is a simple circuit in this graph G 4 is not a tree as it is not connected

Which Graphs are Trees? A connected graph that contains no simple circuits is a tree Because a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops. Therefore any tree must be a simple graph What if there are no simple circuits but the graph is not connected? Each of the connected components is a tree and the collection is called a forest

Rooted Trees A rooted tree is a tree in which one vertex has been designated as the root and every edge is directed away from the root We usually draw a rooted tree with its root at the top of the graph We can change an unrooted tree into a rooted tree by choosing any vertex as the root Different choices of the root produce different rooted trees

Different Roots, Different Trees T with root aT with root c a c ge d g f Tree T gf d c c f

Rooted Trees Root a b c d e f a b c d e f a b c d e f

Rooted Trees What if a different root is chosen? 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

Parent, Child & Sibling Vertices If v is a vertex of rooted 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 The parent of a vertex is the vertex connected to it on the path to the root Every vertex except the root has a unique parent When u is the parent of v, v is called the child of u A child of a vertex v is a vertex of which v is the parent Vertices sharing a parent are called siblings

Parent, Child & Sibling Vertices a b c d e f g h i j k l m Siblings a is the parent of b, c, and d e is the child of b

Ancestor & Descendent Vertices 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

Ancestors: Example a b c d e f g h i j k l m k has 3 ancestors: f, d, a

Descendants: Example a b c d e f g h i j k l m d has 6 descendants: f, g, h, k, l, m

Leaves A vertex with no children is called a leaf a b c d e f g h i j k l m c, g, i, j, k, l and m are leaves

Internal Vertices Vertices with children are called internal vertices a b c d e f g h i j k l m a, b, d, e, f and h are internal vertices

Subtrees 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

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

m-ary and Full m-ary Trees 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

Full Binary Trees: Example What is the arity of this tree? This is a binary (i.e. 2-ary) tree Is this a full m-ary tree? Yes, this is a full binary tree since every internal vertex has exactly 2 children

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

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

3-ary Trees: Example What is the arity of this tree? Some internal nodes have 2 children, but some have 3, so this is a 3-ary tree Is this a full m-ary tree? No. 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

Ordered Rooted Tree: Left Child In an ordered binary tree, if an internal vertex has two children, then the left one is called the left child f is the left child of d a b c d e h i f g j k l m

a b c d e h i f g j k l m g is the right child of d Ordered Rooted Tree: Right Child In an ordered binary tree, if an internal vertex has two children, then the right one is called the right child

Ordered Rooted Tree: Left Subtree a b c d e h i f g j k l m Left subtree of c The subtree rooted at the left child of a vertex is called the left subtree

Ordered Rooted Tree: Right Subtree a b c d e h i f g j k l m Right subtree of c The subtree rooted at the right child of a vertex is called the right subtree

Level of a Vertex Levels a b c d e f g h i j k l m The level of a vertex in a rooted tree is the length of the unique path from the root to this vertex. The level of the root vertex is defined to be 0

Height of a Tree The height of a rooted tree is the maximum of the levels of the vertices a b c d e f g h i j k l m Height = 3 Levels

Level of a Vertex & Height of a Tree: Example a is at level 0 b, j, k are at level 1 c, e, f, l are at level 2 d, g, i, m, n are at level 3 h is at level 4 Because the largest level of any vertex is 4, this tree’s height is 4 h a

Properties of Trees 1.A tree with n vertices has n - 1 edges 2.A full m-ary tree with i internal vertices contains m · i + 1 vertices 3.There are at most m h leaves in an m-ary tree of height h

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

Unbalanced Tree: Example

Section 11.1: Exercises 1. a)Which vertex is the root? b)Which vertices are internal? c)Which vertices are leaves? d)Which vertices are children of j ? e)Which vertex is the parent of h ? f)Which vertices are siblings of o ? g)Which vertices are ancestors of m ? h)Which vertices are descendants of b ? i)What is the arity of this tree? Is it a full m -ary tree? j)What is the level of each vertex? k)What is the height of the tree? l)Draw the subtrees rooted at a and c.

Section 11.1: Exercises 17.How many edges does a tree with 200 vertices have? 18.How many edges does a full binary tree with 500 internal vertices have? 19. Draw a tree having 6 vertices. 20. Draw a graph which has 6 vertices but is not a tree. 21. Draw an unbalanced tree having 6 vertices. 22. Draw a forest having 6 vertices.

CS 103 Discrete Structures Lecture 24 Trees (2)

Second Midterm Exam 1 st Lecture in December (same time as the lecture) 75 minute duration Will cover all lectures delivered before the exam date Will consist of MCQ’s, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having page answers.

Section 11.2

Section Summary Binary Search Trees Decision Trees Prefix Codes (Huffman Coding) Game Trees

Binary Search Trees Searching for items in a list is an important task The goal of search is to efficiently find the required item in an ordered collection A binary search tree is a good tool for this purpose A binary search tree is binary tree in which each child of a vertex is designated as the left or right child, and each vertex is labeled with a key, which is one of the search items Vertices are assigned keys so that key of a vertex is both larger than the keys of all vertices on its left subtree and smaller than the keys of all vertices in the right subtree

Find a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry using alphabetical ordering

Decision Trees Rooted trees can be used to model a decision process in which a sequence of decision leads to a solution 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

Suppose there are 7 identical coins, and a counterfeit coin, that weighs less than the others. How many weighings are necessary using a balance scale to determine which of the 8 coins is the counterfeit one?

Using Bit-Strings to Encode Text Consider the problem of data compression. Suppose that we have a 100,000-character file that contains only 6 different characters, a, b, c, d, e, and f. Some of these 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, c = 010, d = 011, e = 100, f = 101 This requires 300,000 bits to store the file

Prefix Codes We can use Huffman coding which 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, i.e., no bit-string corresponds to more than one sequence of letters. This means that bit- string of one letter never occurs as the first part of the bit- string for another letter Using Huffman coding, we can store the file in 224,000 bits

= a 10 = b 110 = c 1110 = d = e11111 = f = a 101 = b 100 = c 111 = d 1101 = e 1100 = f 0 0

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

Huffman Codes are Unambiguous Since we are using a prefix code, we can simply concatenate our codewords together to produce our bit-string Example: The string abc can be represented as This is unambiguous. That is, this character-string and bit-string pairing is unique

Huffman Code Decoding 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

Game Trees Trees can be used to model and analyze certain 2-player games like tic-tack-toe In game trees, vertices represent the position that a game can be in as it progresses The edges represent legal moves between these positions The root represents the starting point When the game is at position represented by a vertex at an even level, it is the first player’s move. Otherwise, it is the second player’s move The leaves of the game tree represent the concluding positions of a game

Section 11.3

Section Summary Traversal Algorithms Infix, Prefix, and Postfix Notation

Tree Traversal Procedures for systematically visiting every vertex of an ordered tree are called traversals The three most commonly used traversals are: 1. Preorder traversal 2. Inorder traversal 3. Postorder traversal

Preorder Traversal Let T be an ordered rooted tree with root r: 1. If T consists only of r, then r is the preorder traversal of T 2. Otherwise, suppose that T 1, T 2, …, T n are the subtrees of r from left to right in T. The preorder traversal begins by visiting r, and continues by traversing T 1 in preorder, then T 2 in preorder, and so on, until T n is traversed in preorder

Preorder Traversal (contd.) procedure preorder (T: ordered rooted tree) r := root of T list r for each child c of r from left to right T(c) := subtree with c as root preorder(T(c))

Inorder Traversal Let T be an ordered rooted tree with root r: 1. If T consists only of r, then r is the inorder traversal of T 2. Otherwise, suppose that T 1, T 2, …, T n are the subtrees of r from left to right in T. The inorder traversal begins by traversing T 1 in inorder, then visiting r, and continues by traversing T 2 in inorder, and so on, until T n is traversed in inorder

Inorder Traversal (contd.) procedure inorder (T: ordered rooted tree) r := root of T if r is a leaf then list r else l := first child of r from left to right T(l) := subtree with l as its root inorder(T(l)) list(r) for each child c of r from left to right T(c) := subtree with c as root inorder(T(c))

Postorder Traversal Let T be an ordered rooted tree with root r: 1. If T consists only of r, then r is the postorder traversal of T 2. Otherwise, suppose that T 1, T 2, …, T n are the subtrees of r from left to right in T. The postorder traversal begins by traversing T 1 in postorder, then T 2 in postorder, and so on, after T n is traversed in postorder, r is visited

Postorder Traversal (contd.) Procedure postordered (T: ordered rooted tree) r := root of T for each child c of r from left to right T(c) := subtree with c as root postorder(T(c)) list r

Expression Trees Complex expressions (e.g. compound propositions, set combinations, arithmetic expressions) can be represented using ordered rooted tree Example: A binary tree representing ((x + y) ↑ 2 ) + ((x − 4)/3)

Expression Forms: Infix, Prefix, Postfix Infix expression form has operators written between operands. Parentheses are sometimes added to infix expressions to remove ambiguity. Example: x + y Prefix expression form (or Polish notation form) has operators written before the operands. Parentheses are never needed for expressions in the prefix form. Example: + x y Postfix expression form (or reverse Polish notation form) has operators written after the operands. Parentheses are never needed for expressions in the postfix form. Example: x y + Note: All three example expressions shown above lead to the exact same result, i.e. the sum of x and y

Infix Notation An inorder traversal of the tree representing an expression produces the original expression when parentheses are included Trees shown bellow all lead to the same infix expression x + y/x + 3 To make such expressions unambiguous, it is necessary to include parentheses in the inorder traversal whenever we encounter an operation. The fully parenthesized expression obtained this way is said to be in the infix form (x + (y/x)) + 3x + (y/(x + 3))(x + y)/(x + 3)

Infix Notation We obtain the infix form of an expression by traversing its binary tree in inorder x + y/x + 3 (x + (y/x)) + 3x + (y/(x + 3))(x + y)/(x + 3) Trees shown bellow all lead to the same infix expression x + y/x + 3. To make infix expressions unambiguous, it is necessary to include parentheses in the inorder traversal whenever we encounter an operation

Prefix Notation We obtain the prefix form of an expression by traversing its binary tree in preorder Prefix form of ((x + y) ↑ 2 ) + ((x − 4)/3) is + ↑ + x y 2 / − x 4 3 Prefix expressions are evaluated by working from right to left. When we encounter an operator, we perform the corresponding operation with the two operations to the right Example: We show the steps used to evaluate a particular prefix expression

Postfix Notation We obtain the postfix form of an expression by traversing its binary tree in postorder Postfix form of ((x + y) ↑ 2 ) + ((x − 4)/3) is x y + 2 ↑ x 4 − 3 / + A binary operator follows its two operands. So, to evaluate an expression one works from left to right, carrying out an operation represented by an operator on its preceding operands Example: We show the steps used to evaluate a particular postfix expression