Download presentation

Presentation is loading. Please wait.

Published byNaomi Cobb Modified over 4 years ago

1
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 simple circuit, a tree cannot contain multiple edges or loops. Therefore, any tree must be a simple graph. Computer-Based Applications? –Build data structures –Perform encoding –Study formal computer languages –Searching and sorting

2
2 Copyright M.R.K. Krishna Rao 2003Trees Example: Are the following graphs trees? No. Yes. Yes. No.

3
3 Copyright M.R.K. Krishna Rao 2003 Trees Theorem: An undirected graph is a tree if and only if there is a unique simple path between any of its vertices. Proof: Proof: To prove necessary condition: Suppose T is a tree. Since T is connected, there is a simple path between two vertices. If there are two paths between two vertices, then there is a simple circuit in the graph, contradicting that T is a tree. So there is a unique simple path between any two of its vertices.

4
4 Copyright M.R.K. Krishna Rao 2003 Trees Proof (cont.): Proof (cont.): To prove sufficient condition: Suppose there is a unique simple path between any two vertices of an undirected graph T. First, obviously, T is connected. Second, T cannot have any simple circuit, for otherwise there exist two paths between two vertices on a simple circuit, which is a contradiction. So T is connected and does not have any simple circuits. Hence T is a tree.

5
5 Copyright M.R.K. Krishna Rao 2003 Trees Definition: An undirected graph that does not contain simple circuits and is not necessarily connected is called a forest. In general, we use trees to represent hierarchical structures. We often designate a particular vertex of a tree as the root. Since there is a unique path from the root to each vertex of the graph, we direct each edge away from the root. Thus, a tree together with its root produces a directed graph called a rooted tree.

6
6 Copyright M.R.K. Krishna Rao 2003 Tree Terminology If v is a vertex in a rooted tree other than the root, the parent of v is the unique vertex u such that there is an edge from u to v. When u is the parent of v, v is called the child of u. Vertices with the same parent are called siblings. 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.

7
7 Copyright M.R.K. Krishna Rao 2003 Tree Terminology The descendants of a vertex v are those vertices that have v as an ancestor. A vertex of a tree is called a leaf if it has no children. Vertices that have children are called internal vertices. If a is a vertex in a tree, then 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.

8
8 Copyright M.R.K. Krishna Rao 2003 Tree Terminology The level of 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. The height of a rooted tree is the maximum of the levels of vertices. Theorem: Every tree is a bipartite graph. Theorem: A tree with n vertices has n - 1 edges. Theorem: The minimum number of nodes of a tree of height h is h+1.

9
9 Copyright M.R.K. Krishna Rao 2003 Trees as models Example: Family tree - Bernoulli Nikolaus Johann I Jacob I Nikolaus II Johann II

10
10 Copyright M.R.K. Krishna Rao 2003 Trees as models Example: File system /usrtemp binspoolls bin

11
11 Copyright M.R.K. Krishna Rao 2003 Trees as models Example: Arithmetic expressions +- yzxy This tree represents the expression (y + z) (x - y).

12
12 Copyright M.R.K. Krishna Rao 2003 Trees Definition: 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. Theorem: There are at most m h leaves an m-ary tree of height h. An m-ary tree with m = 2 is called a binary tree. A rooted m-ary tree of height h is called a balanced tree if all leaves are at level h or h – 1.

13
13 Copyright M.R.K. Krishna Rao 2003 Trees Theorem: A full m-ary tree with –i internal vertices (have children) has n=mi+1 vertices –n vertices has i=(n-1)/m internal vertices and l=[(m- 1)n+1]/m leaves –i internal vertices has n = mi+1 vertices and l = (m- 1)i+ 1 leaves –l leaves has n = (ml-1)/(m-1) vertices and i=(l-1)/(m- 1) internal vertices P1 P2P3 P4P5P6P7

14
14 Copyright M.R.K. Krishna Rao 2003 Application of Trees How should items in a list be stored so that an item can be easily located? binary search trees What series of decisions should be made to find an object with a certain property in a collection of objects of a certain type? decision trees How should a set of characters be efficiently coded by bit strings? Prefix codes

15
15 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees If we want to perform a large number of searches in a particular list of items, it can be worthwhile to arrange these items in a binary search tree to facilitate the subsequent searches. A binary search tree is a binary tree in which each child of a vertex is designated as a right or left child, and each vertex is labeled with a key, which is one of the items. When we construct the 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.

16
16 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Recursive procedure used to form the binary search tree for a list of items: Start with tree with one vertex (the root) First item is assigned as the key of the root To add a new item, compare it with the keys of vertices already in the tree starting at the root move to the left if the item is less than the key of the respective vertex and this vertex has a left child (if no left child, add this item as left child), or move to the right if the item is greater than the key of the respective vertex and this vertex has a right child (if no right child, add this item as right child).

17
17 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math

18
18 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer

19
19 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power

20
20 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power north

21
21 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power northzoo

22
22 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power northzoodentist

23
23 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees Example: Construct a binary search tree for the strings math, computer, power, north, zoo, dentist, book. math computer power northzoodentistbook

24
24 Copyright M.R.K. Krishna Rao 2003 Binary Search Trees To perform a search in such a tree for an item x, we can start at the root and compare its key to x. If x is less than the key, we proceed to the left child of the current vertex, and if x is greater than the key, we proceed to the right one. This procedure is repeated until we either found the item we were looking for, or we cannot proceed any further. In a balanced tree representing a list of n items, search can be performed with a maximum of log(n + 1) steps (compare with binary search).

25
25 Copyright M.R.K. Krishna Rao 2003 Decision Trees Rooted trees can be used to model problems in which a series of decisions leads to a solutionRooted trees can be used to model problems in which a series of decisions leads to a solution 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 subtreeBinary 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 TREEA 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 Possible solutions correspond to the paths from the root to the leaves of this rooted treePossible solutions correspond to the paths from the root to the leaves of this rooted tree

26
26 Copyright M.R.K. Krishna Rao 2003 Application of Trees Decision Trees - example –7 coins, all with same weight, and a counterfeit coin. –How many weighings are necessary using a balance scale to determine which of the eight is the counterfeit one (assuming it is lighter)? –Three possibilities for each weighing: Coins have equal weightCoins have equal weight First pan is heavierFirst pan is heavier Second pan is heavierSecond pan is heavier –Thus, we have a 3-ary tree –There are at least 8 leaves because there are 8 possible outcomes (each of the 8 coins could be the counterfeit lighter coin), and each possible outcome must be represented by at least one leaf

27
27 Copyright M.R.K. Krishna Rao 2003 Application of Trees Prefix Codes –Consider using bit strings to encode the letters of the English alphabet (no distinction between uppercase and lowercase) –Each letter can be represented with a bit string of length 5, since there are 26 letters and 32 bit strings of length 5. –The total number of bits used to encode the data is 5 times the number of characters in the text when each character is encoded with 5 bits –Is it possible to find a coding scheme of these letters so that, when data are coded, fewer bits are used? (this helps saving memory and reducing transmittal time)

28
28 Copyright M.R.K. Krishna Rao 2003 Application of Trees Prefix Codes –Consider using bit strings of different lengths to encode letters Letters occurring more frequently should be encoded using short bit strings; less frequent = longer strings –-- used in Huffman coding,Letters occurring more frequently should be encoded using short bit strings; less frequent = longer strings –-- used in Huffman coding, Need some method to denote where a string starts and endsNeed some method to denote where a string starts and ends One way to ensure that no bit string corresponds to more than one sequence of letters, the bit string for a letter must never occur as the first part of the bit string for another letterOne way to ensure that no bit string corresponds to more than one sequence of letters, the bit string for a letter must never occur as the first part of the bit string for another letter Codes with this property are called PREFIX CODESCodes with this property are called PREFIX CODES

29
29 Copyright M.R.K. Krishna Rao 2003 Application of Trees Prefix Codes –For example, you’ve coded e as 0, a as 10, and t as 11: What does 10110 denote?What does 10110 denote? Ate -- can’t interpret this string of numbers any other way!Ate -- can’t interpret this string of numbers any other way! –Prefix code can be represented using a binary tree, where characters are the labels of the leaves in the tree. –Edges of tree are labeled so that an edge leading to a left child is assigned a 0 and an edge leading to a right child is assigned a 1

30
30 Copyright M.R.K. Krishna Rao 2003 Application of Trees Prefix Codes –Example: how are e,a,t,n,s encoded below? –0, 10, 110, 1110, 1111 1 1 1 1 0 0 0 0 e a t n s

31
31 Copyright M.R.K. Krishna Rao 2003 Application of Trees Prefix Codes –Example: what does 11111011100 denote? 1 1 1 1 0 0 0 0 e a t n s

32
32 Copyright M.R.K. Krishna Rao 2003 Application of Trees Prefix Codes –Example: what does 11111011100 denote? –Sane! 1 1 1 1 0 0 0 0 e a t n s

33
33 Copyright M.R.K. Krishna Rao 2003 Tree Traversal Ordered rooted trees are often used to store information.Ordered rooted trees are often used to store information. We need procedures for visiting each vertex of an ordered rooted tree to access data.We need procedures for visiting each vertex of an ordered rooted tree to access data.

34
34 Copyright M.R.K. Krishna Rao 2003 Universal Address Systems Procedures for traversing all vertices rely on the orderings of children. 1.Label the root with 0. Then label its k children from left to right. 2.For each vertex v at level n with label A, label its kv children as they drawn from left to right with A.1, A.2, …, A.kv.

35
35 Copyright M.R.K. Krishna Rao 2003 Traversal Algorithms 1.Preorder 2.Inorder 3.Postorder

36
36 Copyright M.R.K. Krishna Rao 2003 Spanning Trees DEF: 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. Theorem: A simple graph is connected iff it has a spanning tree.

37
37 Copyright M.R.K. Krishna Rao 2003 Depth-First Search DFS(G) T = tree consisting only of vertex v1 Visit(v1)endVisit(v) For each vertex w adjacent to v and not yet in T –Add vertex w and edge {v, w} to T –Visit(w)

38
38 Copyright M.R.K. Krishna Rao 2003 Breadth-First Search BFS(G) T = tree consisting only of vertex v1 L = empty list Put v1 in the list L While L is not empty –Remove the first vertex, v, from L –For each neighbor w of v If w is not in L and not in T thenIf w is not in L and not in T then –Add w to end of the list L –Add w and edge {v,w} to T

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google