240 likes | 425 Views
Lecture 17: Trees and Networks I. Discrete Mathematical Structures: Theory and Applications. Learning Objectives. Learn the basic properties of trees Explore applications of trees Learn about networks. Trees. Each atom of a chemical compound is represented by a point in a plane
E N D
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications
Learning Objectives • Learn the basic properties of trees • Explore applications of trees • Learn about networks Discrete Mathematical Structures: Theory and Applications
Trees • Each atom of a chemical compound is represented by a point in a plane • Atomic bonds are represented by lines • Shown in Figure 11.1 for the chemical compound with the formula C4H10. Discrete Mathematical Structures: Theory and Applications
Trees • In chemistry, chemical compounds with formula CkH2k+2 are known as paraffins, which contain k carbon atoms and 2k + 2 hydrogen atoms. • In the graphical representation, each of the carbon atoms corresponds to a vertex of degree 4 and each of the hydrogen atoms corresponds to a vertex of degree 1. • For the same chemical formula C4H10, the graph shown in Figure 11.2 is also a representation. Discrete Mathematical Structures: Theory and Applications
Trees • These graphs are connected and have no cycles. Hence, each of these graphs is a tree. Discrete Mathematical Structures: Theory and Applications
Trees • Consider the graphs shown in Figure 11.4. Each of these graphs is connected. • However, each of these graphs has a cycle. Hence, none of these graphs is a tree. Discrete Mathematical Structures: Theory and Applications
Trees Discrete Mathematical Structures: Theory and Applications
Trees Discrete Mathematical Structures: Theory and Applications
Trees Discrete Mathematical Structures: Theory and Applications
Rooted Tree Discrete Mathematical Structures: Theory and Applications
Rooted Tree • The level of a vertex v is the length of the path from the root to v. Discrete Mathematical Structures: Theory and Applications
The root of this binary tree is A. Vertex B is the left child of A and vertex C is the right child of A. From the diagram, it follows that B is the root of the left subtree of A, i.e., the left subtree of the root. Similarly, C is the root of the right subtree of A, i.e., the right subtree of the root. LA= {B, D, E, G} and RA = {C, F ,H}. Moreover, for vertex F , the left child is H and F has no right child. Discrete Mathematical Structures: Theory and Applications
Rooted Tree Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Binary Tree Traversal • Item insertion, deletion, and lookup operations require the binary tree to be traversed. Thus, the most common operation performed on a binary tree is to traverse the binary tree, or visit each vertex of the binary tree. The traversal must start at the root because one is typically given a reference to the root. For each vertex, there are two choices. • Visit the vertex first. • Visit the subtrees first. Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Inorder Traversal: In an inorder traversal, the binary tree is traversed as follows. • Traverse the left subtree. • Visit the vertex. • Traverse the right subtree. Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Preorder Traversal: In a preorder traversal, the binary tree is traversed as follows. • Visit the vertex. • Traverse the left subtree. • Traverse the right subtree. Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Postorder Traversal: In a postorder traversal, the binary tree is traversed as follows. • Traverse the left subtree. • Traverse the right subtree. • Visit the vertex. Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Each of these traversal algorithms is recursive. • The listing of the vertices produced by the inorder traversal of a binary tree is called the inorder sequence. • The listing of the vertices produced by the preorder traversal of a binary tree is called the preorder sequence. • The listing of the vertices produced by the postorder traversal of a binary tree is called the postorder sequence. Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Binary Search Trees • To determine whether 50 is in the binary tree, any of the previous traversal algorithms to visit each vertex and compare the search item with the data stored in the vertex can be used. • However, this could require traversal of a large part of the binary tree, so the search would be slow. • Each vertex in the binary tree must be visited until either the item is found or the entire binary tree has been traversed because no criteria exist to guide the search. Discrete Mathematical Structures: Theory and Applications
Rooted Tree • Binary Search Trees • In the binary tree in Figure 11.22, the value of each vertex is larger than the values of the vertices in its left subtree and smaller than the values of the vertices in its right subtree. • The binary tree in Figure 11.22 is a special type of binary tree, called a binary search tree. Discrete Mathematical Structures: Theory and Applications