Graphs & Trees Intro2CS – week 8-9 1. General Graphs Nodes (Objects) can have more than one reference. Example: think of implementing a Class “Person”

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

Introduction to Trees Chapter 6 Objectives
Data Structures: A Pseudocode Approach with C 1 Chapter 6 Objectives Upon completion you will be able to: Understand and use basic tree terminology and.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
1 Section 9.1 Introduction to Trees. 2 Tree terminology Tree: a connected, undirected graph that contains no simple circuits –must be a simple graph:
CS 171: Introduction to Computer Science II
Computer Science 2 Data Structures and Algorithms V section 2 Introduction to Trees Professor: Evan Korth New York University.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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; } 
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Lecture 8 Tree.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
More Trees Discrete Structures (CS 173)
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
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.
BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
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. 
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Trees A non-linear implementation for collection classes.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
Paul Tymann and Andrew Watkins
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Tree.
Tree traversal from Introduction to Trees Chapter 6 Objectives
Binary Trees, Binary Search Trees
Trees CMSC 202, Version 5/02.
CMSC 202 Trees.
Section 9.3 by Andrew Watkins
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Trees Construction Paths Height/Depth Ordered Trees Traversals
Trees.
Chapter 20: Binary Trees.
8.2 Tree Traversals Chapter 8 - Trees.
Data Structures – Binary Tree
Presentation transcript:

Graphs & Trees Intro2CS – week 8-9 1

General Graphs Nodes (Objects) can have more than one reference. Example: think of implementing a Class “Person” Each Person has a list of friends, which are also of the same “Person” class. A “Social Network” 2 Joe Jill John Jack Jen

Graphs

Paths

Traversing general graphs Suppose we wish to visit all nodes in a graph and print their data (only once!) How do we do this? 5

6

First attempt 7

What went wrong? What if we had a loop in the graph?

Fixing the problem 9

Trees Directed Rooted Trees are one particularly useful class of Graphs. They have a special node called “the root” There is exactly one path from the root to every other node in the tree. – No Cycles!

Tree Terminology A node that is directly linked from v is often called a child of v (and v is called the parent) – Example: 6,7 are children of 3 A Node that has no outgoing links is called a Leaf. – Example: 4,6,7,8 are all leaves

Tree Terminology The height of a node is the length of its path from the root (the root is of height 0). Example: Node 5 has height 2. The height of the tree is the length of the longest path. Example: the tree here has height 3 (due to the path to node 8)

Example: Trees that represent expressions One thing to naturally represent with trees is mathematical expressions: Leaves are all literals, internal nodes are operators Notice that the order of the children matters 14 x ++ / ( ) x ( 6 + ( 2 / 7 ) )

In Python 15

Computing the value of an expression 16

Printing the expression 17

Polish Notation Polish notation (also called prefix notation) is just a different way to write mathematical expressions The operator is always written to the left. Instead of (2+3) write: (+ 2 3) We never need parentheses when writing this way. Order of operations is always well defined: + x (+ (x 3 4) (- 2 5)) In “regular” notation: (3 x 4) + (2-5) 18

19

Traversing a tree The order of visiting a tree can be defined (recursively) Pre order: print the root, then print the subtrees – Example: when we were printing an expr in polish notation In order: left subtree, root, right subtree – Example: when we were printing a “regular expression” Post order: print the subtrees, then the root – Example: reverse polish notation 20

(Extra) Parsing Polish Notation 21

Trees – Twenty Questions Does it have four legs? y Is it really large? y Is it an elephant? n I guessed wrong. What did you have in mind? a rhino Please enter a question to differentiate between an elephant and a rhino: does it have a horn? an elephant. does it have a horn? n Do you want to play again? y 22

23 Four legs? Really large? Has wings? dog elephantbirdsnake y yyn n n n

24 Four legs? Really large? Has wings? dog Has horn?birdsnake y yyn n n n y n elephantrhino

25

26

27

28

29

30