Presentation is loading. Please wait.

Presentation is loading. Please wait.

Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.

Similar presentations


Presentation on theme: "Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete."— Presentation transcript:

1 Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete Mathematics and It's Applications” Kenneth H. Rosen; 6th edition, 2007.

2 Trees In this chapter we will focus on a particular type of graph called a tree, so named because such graphs resemble trees. For example, family trees are graphs that represent genealogical charts. Definition: A tree is a connected undirected graph with No simple circuits No multiple edges No loops

3 Trees Theorem: An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. Example: Which graphs are trees? G G G G4 Solution: G1 and G2 are trees, because both are connected graphs with no simple circuits. G3 is not a tree because e, b, a, d, e is a simple circuit in this graph. Finally, G4 is not a tree because it is not connected. a b a b a b a b c d d c e f e f e f e f

4 Trees Notes: Any 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. The collection is called a forest.

5 Trees Definition: 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. Rooted trees can also be defined recursively. We can change an unrooted tree into a rooted tree by choosing any vertex as the root. Note that different choices of the root produce different rooted trees. For instance, next figure displays the rooted trees formed by designating a to be the root and c to be the root, respectively, in the tree T. We usually draw a rooted tree with its root at the top of the graph.

6 A Tree and Rooted Trees Formed by Designating Two Roots.
With root a With root c T a f g c d c d f g e f g c

7 Trees Example: Root a b c d e f a b c d e f a b c d e f

8 What if a different root is chosen?
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 A different rooted tree results.

9 Tree Terminology Definitions:
If v is a vertex of 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. When u is the parent of v, v is called the child of u. If two vertices share the same parent, then they are called siblings.

10 Tree Terminology Example: a b c d e f g h i j k l m Siblings

11 Tree Terminology Definitions:
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.

12 Example: Ancestors of k
Tree Terminology Example: Ancestors of k a b c d e f g h i j k l m

13 Example: Descendants of d
Tree Terminology Example: Descendants of d a b c d e f g h i j k l m

14 Tree Terminology Definitions:
A vertex with no children is called a leaf. Vertices with children are called internal vertices. Example: Leaves a b c d e f g h i j k l m

15 Example: Internal vertices
Tree Terminology Example: Internal vertices a b c d e f g h i j k l m

16 Tree Terminology Definition:
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.

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

18 Tree Terminology Definitions:
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.

19 Tree Terminology Example: What is the arity of this tree?
Is this a full m-ary tree? This is a 2-ary, or binary, tree. Yes, this is a full binary tree, since every internal vertex has exactly 2 children.

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

21 Tree Terminology Example: What is the arity of this tree?
Is this a full m-ary tree? This is a full 5-ary tree.

22 Tree Terminology Example: What is the arity of this tree?
Is this a full m-ary tree? Some internal nodes have 2 children, but some have 3, so this is a 3-ary tree. It is not a full-3-ary tree, since one internal node has only 2 children.

23 Ordered Rooted Tree Definitions:
An ordered rooted tree is one where the children of each internal vertex are ordered. In an ordered binary tree, if an internal vertex has two children, then they are called left child and right child. The subtree rooted at the left child of a vertex is called the left subtree and subtree rooted at the right child of a vertex is called the right subtree.

24 Example: Left child of d
Ordered Rooted Tree Example: Left child of d a b c d e h i f g j k l m

25 Ordered Rooted Tree Example: Right child of d a b c d e h i f g j k l

26 Ordered Rooted Tree Example: Left subtree of c a b c d e h i f g j k l

27 Ordered Rooted Tree Example: Right subtree of c a b c d e h i
f g j k l m Right subtree of c

28 Tree Terminology Definitions:
The level of a vertex v in a rooted tree is the length of the unique path from the root to this vertex. What is the level of the root? 0 The height of a rooted tree is the maximum of the levels of the vertices.

29 Tree Terminology Example: Levels 1 2 3 Height = 3 a b c d e f g h
i j k l m 1 2 3 Height = 3

30 Tree Terminology Properties of Trees:
A tree with n vertices has n1 edges. An full m-ary tree with i internal vertices contains n = mi + 1 vertices. A rooted m-ary tree of height h is called balanced if all leaves are at levels h or h–1

31 Tree Terminology Example: Find the level of each vertex in the rooted tree shown in the next figure . What is the height of this tree? Solution: The root a is at level O. Vertices b, j, and k are at level 1 . Vertices c, e, f, and I are at level 2. Vertices d, g, i , m , and n are at level 3 . Finally, vertex h is at level 4. Because the largest level of any vertex is 4, this tree has height 4. Note: A rooted m -ary tree of height h is balanced if all leaves are at levels h or h - 1 a h

32 Example: Is this tree balanced?
Tree Terminology Example: Is this tree balanced?

33 Example: Is this tree balanced?
Tree Terminology Example: Is this tree balanced?

34 Homework Pages 693 Exercises : 1, 2, 3, 4


Download ppt "Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete."

Similar presentations


Ads by Google