Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)

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.
Greedy Algorithms Greed is good. (Some of the time)
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
1 Section 9.4 Spanning Trees. 2 Let G be a simple graph. A spanning subtree of G is a subgraph of G containing every vertex of G –must be connected; contains.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
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.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Rooted Trees. More definitions parent of d child of c sibling of d ancestor of d descendants of g leaf internal vertex subtree root.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Discrete Mathematics Lecture 9 Alexander Bukharovich New York University.
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.
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.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
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.
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.
Foundations of Discrete Mathematics
Week 11 - Wednesday.  What did we talk about last time?  Graphs  Euler paths and tours.
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.
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.
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.
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
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.
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.
Discrete Structures – CNS 2300 Text Discrete Mathematics and Its Applications (5 th Edition) Kenneth H. Rosen Chapter 9 Trees.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
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 Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
Section10.1: Introduction to Trees
Discrete Mathematics Trees.
Applied Discrete Mathematics Week 15: Trees
Trees Chapter 15.
Chapter 5 : Trees.
Trees Chapter 11.
12. Graphs and Trees 2 Summary
Introduction to Trees Section 11.1.
Advanced Algorithms Analysis and Design
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.
Trees.
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
And the Final Subject is…
Trees Chapter 11.
Presentation transcript:

Chapter 11

Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)

Section 11.1

Section Summary  Trees  Rooted trees  Trees as models  Properties of trees

Graph  Definition 1: A graph G=(V, E) consists of V, a nonempty set of vertices (or nodes) and E, a set of edges. Each edge has either one or two vertices associated with it, it called its endpoints. An edge is said to connect its endpoint. Undirected graph directed graph

Connectedness  Path: a path is a sequence of edges that begins at a vertex of a graph and travels from vertex along of the graph. As the path travels along its edge, it visits the vertices along this path, that is, the endpoints of these edges.  Definition 2 : An undirected graph is called connected if there is a path between every pair of distinct vertices of the graph. An undirected graph that is not connected is called disconnected. disconnected connected

Tree  A tree is a connected undirected graph with no simple circuits.  Theorem 1: An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices.

Rooted Tree  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.  A rooted tree is a directed graph

Rooted Tree  Suppose that T is a rooted tree.  Parent and child: If v is a vertex in 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.  Siblings: vertices with the same parent are called siblings.  Ancestor: 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 that is, its parent, its parent’s parent and so on, until the root is reached).

Rooted Tree  Suppose that T is a rooted tree.  Descendant: the descendants of a vertex v are those vertices that have v as an ancestor.  Leaf: a vertex of a rooted tree is called a leaf if it has no children.  Internal vertices: vertices that have children are called internal vertices The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf.  Subtree: 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.

Rooted Tree  Example: A rooted tree T, find the parent of c, the children of g, siblings of h, all ancestors of e, all descendants of b, all internal vertices, and all leaves. What is the subtree rooted at g?  Solution  The parent of c is b.  The children of g are h, i, and j.  The siblings of h are i and j.  The ancestors of e are c, b, and a.  The descendants of b are c, d, and e.  The internal vertices are a, b, c, g, h, and j.  The leaves are d, e, f, i, k, l, and m.  The subtree rooted at g is shown

Rooted Tree  Definition 3: A rooted tree is called an m-ary tree if every internal vertex has no more than m children. The 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: Are the rooted trees are full m-ary trees for some positive integer m?  Solution:  T1 is a full binary.  T2 is a full 3-ary tree.  T3 is a full 5-ary tree.  T4 is not a full m-ary tree.

Ordered Rooted Tree  ordered rooted tree: An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.  left child and right child : In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children, the first child is called the left child and the second child is called the right child.  the left subtree and right subtree: The tree rooted at the left child of a vertex is called the left subtree of this vertex, and the tree rooted at the right child of a vertex is called the right subtree of the vertex.

Tree as models

Properties of Trees  THEOREM 2: A tree with n vertices has n − 1 edges.  THEOREM 3: A full m-ary tree with i internal vertices contains n = mi + 1 vertices.  THEOREM 4 : A full m-ary tree with  (i ) n vertices has i=(n−1)/m internal vertices and l= [(m−1)n+1]/m leaves.  (ii ) i internal vertices has n = mi + 1 vertices and l=(m − 1)i+1 leaves,  (iii ) l leaves has n = (ml −1)/(m−1) vertices and i = (l−1)/(m−1) internal vertices.

Properties of Trees  Level: The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. The level of the root is defined to be zero.  Height: The height of a rooted tree is the maximum of the levels of vertices. In other words, the height of a rooted tree is the length of the longest path from the root to any vertex.  Balanced: A rooted m-ary tree of height h is balanced if all leaves are at levels h or h − 1. balanced unbalanced

Properties of Trees  THEOREM 5: There are at most m h leaves in an m-ary tree of height h.  COROLLARY 1: If an m-ary tree of height h has l leaves, then h ≥ ceiling(log m l). If the m-ary tree is full and balanced, then h = log m l.

Section 11.2

Section summary  How should items in a list be stored so that an item can be easily located?  What series of decisions should be made with a certain property in a collection of objects of a certain type?  How should a set of characters be efficiently coded by bit strings?

Binary search trees  Binary search tree  A binary tree  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

Binary search trees  Construction of Binary search tree  recursive procedure:  Start with a tree containing just one vertex, namely, the root. The first item in the list is assigned as the key of the root.  To add a new item, first compare it with the keys of vertices already in the tree, starting at the root and moving to the left if the item is less than the key of the respective vertex if this vertex has a left child, or moving to the right if the item is greater than the key of the respective vertex if this vertex has a right child. When the item is less than the respective vertex and this vertex has no left child, then a new vertex with this item as its key is inserted as a new left child. Similarly, when the item is greater than the respective vertex and this vertex has no right child, then a new vertex with this item as its key is inserted as a new right child.

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

Binary search trees  Locate items in the binary search tree  recursive procedure:  v is the vertex currently under examination and label(v) represents the key of this vertex.  The algorithm begins by examining the root. If x equals the key of v, then the algorithm has found the location of x and terminates; if x is less than the key of v, we move to the left child of v and repeat the procedure; and if x is greater than the key of v, we move to the right child of v and repeat the procedure.  If at any step we attempt to move to a child that is not present, we know that x is notpresent in the tree

Decision trees  Decision tree: 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.  Example :Comparison-based sorting algorithm

Decision trees  THEOREM 1: A sorting algorithm based on binary comparisons requires at least ceiling(logn!) comparisons.  COROLLARY 1: The number of comparisons used by a sorting algorithm to sort n elements based on binary comparisons is Ω(n log n).  THEOREM 2: The average number of comparisons used by a sorting algorithm to sort n elements based on binary comparisons is Ω(n log n).

Prefix codes  Prefix codes : Codes with this property encode letters so that the bit string for a letter never occurs as the first part of the bit string for another letter are called prefix codes.  E.g. the encoding of e as 0, a as 10, and t as 11 is a prefix code.  Huffman coding: an algorithm that takes as input the frequencies (which are the probabilities of occurrences) of symbols in a string and produces as output a prefix code that encodes the string using the fewest possible bits, among all possible binary prefix codes for these symbols.  Huffman coding, is a fundamental algorithm in data compression, the subject devoted to reducing the number of bits required to represent information.

Prefix codes  Construction of Huffman coding:  begins with a forest of trees each consisting of one vertex, where each vertex has a symbol as its label and where the weight of this vertex equals the frequency of the symbol that is its label.  At each step, combine two trees having the least total weight into a singletree by introducing a new root and placing the tree with larger weight as its left subtree and the tree with smaller weight as its right subtree. Furthermore, assign the sum of the weights of the two subtrees of this tree as the total weight of the tree.  The algorithm is finished when theforest is reduced to a single tree.

Prefix codes

Section 11.3

Section Summary  Universal address systems  Traversal algorithms  Preorder traversal  inorder traversal  postorder traversal

Universal Address Systems  Universal address systems  1. Label the root with the integer 0. Then label its k children (at level 1) from left to right with 1, 2, 3,..., k.  2. For each vertex v at level n with label A, label its k v children, as they are drawn from left to right, with A.1,A.2,..., A.k v.

Traversal algorithms  Procedures for systematically visiting every vertex of an ordered rooted tree are called traversal algorithms.  Definition 1: Let T be an ordered rooted tree with root r. If T consists only of r, then r is the preorder traversal of T. Otherwise, suppose that T 1, T 2,..., T n are the subtrees at r from left to right in T. The preorder traversal begins by visiting r. It continues by traversing T 1 in preorder, then T 2 in preorder, and so on, until T n is traversed in preorder.

Traversal algorithms  EXAMPLE: In which order does a preorder traversal visit the vertices in the ordered rooted tree T ?  Solution: the preorder traversal of T is a, b, e, j, k, n, o, p, f, c, d, g, l, m, h, i.

Traversal algorithms  Definition 2: Let T be an ordered rooted tree with root r. If T consists only of r, then r is the inorder traversal of T. Otherwise, suppose that T 1, T 2,..., T n are the subtrees at r from left to right. The inorder traversal begins by traversing T 1 in inorder, then visiting r. It continues by traversing T 2 in inorder, then T 3 in inorder,..., and finally T n in inorder.

Traversal algorithms  EXAMPLE: In which order does an inorder traversal visit the vertices of the ordered rooted tree T  Solution: the inorder traversal of T is j, e, n, k, o, p, b, f, a, c, l, g, m, d, h, i.

Traversal algorithms  Definition 3: Let T be an ordered rooted tree with root r. If T consists only of r, then r is the postorder traversal of T. Otherwise, suppose that T 1, T 2,..., T n are the subtrees at r from left to right. The postorder traversal begins by traversing T 1 in postorder, then T 2 in postorder,..., then T n in postorder, and ends by visiting r.

Traversal algorithms  EXAMPLE: In which order does an inorder traversal visit the vertices of the ordered rooted tree T  Solution: the postorder traversal of T is j, n, o, p, k, e, f, b, c, l, m, g, h, i, d, a.

Section 11.4

Section summary  Spanning tree  Depth-first search  Breadth-first search

Spanning Trees  Definition 1: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G.  Construct spanning tree by removing edges

Spanning Trees  Spanning tree are important in data networking.  IP Multicasting:

Depth-first search  Construct a spanning tree using depth-first search.  Arbitrarily choose a vertex of the graph as the root.  Form a path starting at this vertex by successively adding vertices and edges, where each new edge is incident with the last vertex in the path and a vertex not already in the path.  Continue adding vertices and edges to this path as long as possible. If the path goes through all vertices of the graph, the tree consisting of this path is a spanning tree.  if the path does not go through all vertices, move back to the next to last vertex in the path, and, if possible, form a new path starting at this vertex passing through vertices that were not already visited. If this cannot be done, move back another vertex in the path, that is, two vertices back in the path, and try again.  Repeat

Depth-first search  Example:

breadth-first search  Construct a spanning tree using depth-first search.  Arbitrarily choose a root from the vertices of the graph. Then add all edges incident to this vertex. The new vertices added at this stage become the vertices at level 1 in the spanning tree. Arbitrarily order them.  Next, for each vertex at level 1, visited in order, add each edge incident to this vertex to the tree as long as it does not produce a simple circuit.  Arbitrarily order the children of each vertex at level 1. This produces the vertices at level 2 in the tree.  Follow the same procedure until all the vertices in the tree have been added.

breadth-first search  Example: