Download presentation
Presentation is loading. Please wait.
Published byJack Wilcox Modified over 8 years ago
1
CS 103 Discrete Structures Lecture 23 Trees (1)
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 0.5-1 page answers.
3
Chapter 11
4
Chapter Summary Introduction to Trees Applications of Trees Tree Traversal
5
Section 11.1
6
Section Summary Introduction to Trees Rooted Trees Trees as Models Properties of Trees
7
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
8
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
9
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
10
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
11
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
12
Different Roots, Different Trees T with root aT with root c a c ge d g f Tree T gf d c c f
13
Rooted Trees Root a b c d e f a b c d e f a b c d e f
14
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
15
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
16
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
17
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
18
Ancestors: Example a b c d e f g h i j k l m k has 3 ancestors: f, d, a
19
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
20
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
21
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
22
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
23
Subtrees: Example a b c d e f g h i j k l m Subtree at b Subtree at d
24
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
25
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
26
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
27
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
28
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
29
Ordered Rooted Tree An ordered rooted tree is one where the children of each internal vertex are ordered
30
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
31
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
32
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
33
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
34
Level of a Vertex Levels a b c d e f g h i j k l m 0 1 2 3 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
35
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 0 1 2 3 Height = 3 Levels
36
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
37
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
38
Balanced Trees A rooted m-ary tree of height h is balanced if all leaves are at levels h or h - 1
39
Unbalanced Tree: Example
40
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.
41
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.
42
CS 103 Discrete Structures Lecture 24 Trees (2)
43
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 0.5-1 page answers.
44
Section 11.2
45
Section Summary Binary Search Trees Decision Trees Prefix Codes (Huffman Coding) Game Trees
46
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
47
Find a binary search tree for the words mathematics, physics, geography, zoology, meteorology, geology, psychology, and chemistry using alphabetical ordering
48
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
49
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?
50
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
51
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
52
10 1 1 1 1 0 0 0 0 0 = a 10 = b 110 = c 1110 = d 11110 = e11111 = f 10 1 1 0 1 1 0 0 = a 101 = b 100 = c 111 = d 1101 = e 1100 = f 0 0
53
Huffman Codes: Codewords abcdef Frequency of occurrence (in thousands) 4513121695 Codeword (fixed- length) 000001010011110101 Codeword (variable length) 010110011111011100
54
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 0101100 This is unambiguous. That is, this character-string and bit-string pairing is unique
55
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 101100. 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
56
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
58
Section 11.3
59
Section Summary Traversal Algorithms Infix, Prefix, and Postfix Notation
60
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
61
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
62
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))
63
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
64
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))
65
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
66
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
67
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)
68
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
69
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)
70
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
71
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
72
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.