Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)"— Presentation transcript:

1 Chapter 11

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

3 Section 11.1

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

5 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

6 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

7 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.

8 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

9 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).

10 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.

11 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

12 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.

13 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.

14 Tree as models

15 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.

16 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

17 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.

18 Section 11.2

19 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?

20 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

21 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.

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

23 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

24 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

25 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).

26 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.

27 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.

28 Prefix codes

29 Section 11.3

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

31 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.

32 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.

33 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.

34 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.

35 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.

36 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.

37 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.

38 Section 11.4

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

40 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

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

42 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

43 Depth-first search  Example:

44 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.

45 breadth-first search  Example:


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

Similar presentations


Ads by Google