Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.

Slides:



Advertisements
Similar presentations
Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Marc Smith and Jim Ten Eyck
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
Section 10.1 Introduction to Trees These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth.
Trees CSC 172 SPRING 2002 LECTURE 14. Lists We have seen lists: public class Node { Object data; Node next; } 
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Trees Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Chapter 11. Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum Spanning.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Trees Chapter 11.
Paul Tymann and Andrew Watkins
Trees Another Abstract Data Type (ADT)
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Tree.
Csc 2720 Instructor: Zhuojun Duan
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Trees Another Abstract Data Type (ADT)
Trees Another Abstract Data Type (ADT)
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Trees Chapter 11.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees

Section 5.2Trees and Their Representations1 Tree Terminology A special type of graph called a tree turns out to be a very useful representation of data. DEFINITION: TREE A tree is an acyclic, connected graph with one node designated as the root of the tree. An acyclic, connected graph with no designated root node is called a nonrooted tree or a free tree.

Section 5.2Trees and Their Representations2 Defining Trees Recursively A tree can also be defined recursively. A single node is a tree (with that node as its root). If T 1, T 2,..., T t are disjoint trees with roots r 1, r 2,..., r t, the graph formed by attaching a new node r by a single arc to each of r 1, r 2,..., r t is a tree with root r. The nodes r 1, r 2,..., r t are children of r, and r is a parent of r 1, r 2,..., r t.

Section 5.2Trees and Their Representations3 Tree Terminology The depth of a node in a tree is the length of the path from the root to the node; the root itself has depth 0. The depth (height) of the tree is the maximum depth of any node in the tree; in other words, it is the length of the longest path from the root to any node. A node with no children is called a leaf of the tree. All nonleaves are internal nodes. A forest is an acyclic graph (not necessarily connected).

Section 5.2Trees and Their Representations4 Tree Terminology Binary trees are those where each node has at most two children. Each child of a node is designated as either the left child or the right child. A full binary tree (as seen in the middle figure below) occurs when all internal nodes have two children and all leaves are at the same depth. A complete binary tree (as seen in the right figure below) is an almost-full binary tree; the bottom level of the tree is filling from left to right but may not have its full complement of leaves.

Section 5.2Trees and Their Representations5 Applications of Trees Decision trees were used to solve counting problems in Chapter 3. By using trees, a collection of records can be efficiently searched to locate a particular record or to determine that a record is not in the collection. A family tree is usually, indeed, a tree. Files stored on a computer are organized in a hierarchical (treelike) structure. Algebraic expressions involving binary operations can be represented by labeled binary trees.

Section 5.2Trees and Their Representations6 Binary Tree Representation Because a tree is also a graph, representations for graphs in general can also be used for trees. Binary trees, however, have special characteristics that we want to capture in the representation, namely, the identity of the left and right child. The equivalent of an adjacency matrix is a two- column array (or an array of records) where the data for each node is the left and right child of that node. The equivalent of the adjacency list representation is a collection of records with three fields containing, respectively, the current node, a pointer to the record for the left-child node, and a pointer to the record for the right-child node.

Section 5.2Trees and Their Representations7 Binary Tree Representation Example The tree represented by the figure above has the following adjacency list and adjacency matrix representations.

Section 5.2Trees and Their Representations8 Tree Traversal Algorithms If a tree structure is being used to store data, it is often helpful to have a systematic mechanism for writing out the data values stored at all the nodes. This can be accomplished by traversing the tree, that is, visiting each of the nodes in the tree structure. The three common tree traversal algorithms are preorder, inorder, and postorder traversal. The terms preorder, inorder, and postorder refer to the order in which the root of a tree is visited compared to the subtree nodes.

Section 5.2Trees and Their Representations9 Tree Traversal Algorithms In preorder traversal, the root of the tree is visited first and then the subtrees are processed left to right, each in preorder. ALGORITHM Preorder Preorder(tree T) //Writes the nodes of a tree with root r in preorder write(r) for i 1 to t do Preorder(T i ) end for end Preorder

Section 5.2Trees and Their Representations10 Tree Traversal Algorithms In inorder traversal, the left subtree is processed by an inorder traversal, then the root is visited, and then the remaining subtrees are processed from left to right, each in inorder. If the tree is binary, the result is that the root is visited between processing of the two subtrees. ALGORITHM Inorder Inorder(tree T ) //Writes the nodes of a tree with root r in inorder Inorder(T 1 ) write(r) for i 2 to t do Inorder(T i ) end for end Inorder

Section 5.2Trees and Their Representations11 Tree Traversal Algorithms In postorder traversal, the root is visited last, after all subtrees have been processed from left to right in postorder. ALGORITHM Postorder Postorder(tree T ) //Writes the nodes of a tree with root r in postorder for i 1 to t do Postorder(T i ) end for write(r) end Postorder

Section 5.2Trees and Their Representations12 Tree Traversal Algorithms Example What is the preorder, inorder, and postorder traversal for the following tree? The preorder (root, left, right) traversal produces: a, b, d, e, c, f, h, i, g. The inorder (left, root, right) traversal produces: d, b, e, a, h, f, i, c, g. The postorder (left, right, root) traversal produces: d, e, b, h i, f, g, c, a.

Section 5.2Trees and Their Representations13 Infix Notation The binary tree in the figure below represents the algebraic expression (2 + x)  (y * 3). If we do an inorder traversal of the expression tree, we retrieve the original algebraic expression. Parentheses are added as we complete the processing of a subtree. This is called infix notation.

Section 5.2Trees and Their Representations14 Polish Notation A preorder traversal of a tree as seen in this figure gives the expression * 2 x 4. Here the operation symbol precedes its operands. This form of an expression is called prefix notation, or Polish notation. The expression can be translated into infix form as follows: * + 2 x 4  * (2 + x) 4  (2 + x ) * 4 A postorder traversal gives the expression 2 x 4 *, where the operation symbol follows its operands. This form of an expression is called postfix notation, or reverse Polish notation (or just RPN). The expression can be translated into infix form as follows: 2 x 4 *  (2 + x) 4 *  (2 + x) * 4 Neither prefix nor postfix form requires parentheses to avoid ambiguity.