CSE 326: Data Structures Lecture #6 (end of Lists, then) Trees Alon Halevy Spring Quarter 2001.

Slides:



Advertisements
Similar presentations
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Advertisements

Binary Trees CS 110: Data Structures and Algorithms First Semester,
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
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.
CSE 326: Data Structures Lecture #5 Alon Halevy Spring Quarter 2001.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
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.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
Compiled by: Dr. Mohammad Omar Alhawarat
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.
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.
Data Structures TREES.
Week 8 - Monday.  What did we talk about last time?  BST traversals  Get range implementation.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
CSE 326 More Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees A non-linear implementation for collection classes.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Non Linear Data Structure
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
i206: Lecture 13: Recursion, continued Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Trees.
Trees Definitions Implementation Traversals K-ary Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
CSE 326: Data Structures Lecture #1 Lists, MultiLists & Trees
Binary Trees.
Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

CSE 326: Data Structures Lecture #6 (end of Lists, then) Trees Alon Halevy Spring Quarter 2001

Application: Polynomial ADT A i is the coefficient of the x n-i term: 3x 2 + 2x + 5 ( ) 8x + 7 ( 8 7 ) x ( ) Problem?

3x ( )

Sparse List Data Structure: 3x ( )

Addition of Two Polynomials Similar to merging two sorted lists – O(n+m) x 50 +3x p x 50 +4x q r

Multiple Linked Lists Many ADTS such as graphs, relations, sparse matrices, multivariate polynomials use multiple linked lists Several options –array of lists –lists of lists –multi lists General principle throughout the course: use one ADT to implement a more complicated one.

Array of Linked Lists: Adjacency List for Graphs G Array G of unordered linked lists Each list entry corresponds to an edge in the graph

Reachability by Marking Suppose we want to mark all the nodes in the graph which are reachable from a given node k. –Let G[1..n] be the adjacency list rep. of the graph –Let M[1..n] be the mark array, initially all false s. mark(int i){ M[i] = true; x = G[i] while (x != NULL) { if (M[x->node] == false) mark(G[x->node]) x = x->next }

Multi-Lists Suppose we have a set of movies and cinemas, and we want a structure that stores which movies are playing where.

More on Multi-Lists What if we also want to store the playing times of movies?

Trees Family Trees Organization Charts Classification trees –is this mushroom poisonous? File directory structure Parse Trees (x+y*z) Search Trees – often better than lists for sorted data

Definition of a Tree Recursive definition: –empty tree has no root –given trees T1,…,Tk and a node r, there is a tree T where r is the root of T the children of r are the roots of T1, T2, …, Tk r T1T1 T2T2 T3T3

Tree Terminology root child parent sibling path descendent ancestor a i d hj b f kl e c g

More Tree Terminology a i d hj b f kl subtree leaf depth height branching factor n-ary complete e c g

Basic Tree Data Structure data next_sibling first_child a bcde 

Logical View of Tree a i d hj b f kl e c g

Actual Data Structure abcdefghijkl

Combined View of Tree a i d hj b f kl e c g

Traversals Many algorithms involve walking through a tree, and performing some computation at each node Walking through a tree is called a traversal Common kinds of traversal –Pre-order –Post-order –Level-order

Pre-Order Traversal Perform computation at the node, then recursively perform computation on each child preorder(node * n){ node * c; if (! n==NULL){ DO SOMETHING; c = n->first_child; while (! c==NULL){ preorder(c); c = c->next_sibling; } }

Pre-Order Traversal Example a i d hj b f kl e c g Start with a -

Pre-Order Applications Use when computation at node depends upon values calculated higher in the tree (closer to root) Example: computing depth depth(node) = 1 + depth( parent of node ) Another example: printing out a directory structure.

Computing Depth of All Nodes Add a field “depth” to all nodes Depth(node * n, int d){ node * c; if (! n==NULL){ n->depth = d; d = d+1; c = n->first_child; while (! c==NULL){ Depth(c, d); c = c->next_sibling; } } Call Depth(root,0) to set depth field correctly

Depth Calculation a i d hj b f kl e c g

Post-Order Traversal Recursively perform computation on each child, and then perform computation at node postorder(node * n){ node * c; if (! n==NULL){ c = n->first_child; while (! c==NULL){ postorder(c); c = c->next_sibling; } DO SOMETHING; }

Post-Order Applications Use when computation at node depends upon values calculated lower in the tree (closer to leafs) Example: computing height height(node) = 1 + MAX( height(child 1 ), height(child 2 ), … height(child k )) Example: size of tree rooted at node size(node) = 1 + size(child 1 ) + size(child 2 ) + … + size(child k ))

Computing Size of Tree Size(node * n){ node * c; if (! n==NULL) return 0; else { int m=1; c = n->first_child; while (! c==NULL){ m = m + Size(c); c = c->next_sibling; } return m; } Call Size(root) to compute number of nodes in tree

Depth-First Search Both Pre-Order and Post-Order traversals are examples of depth-first search –nodes are visited deeply on the left-most branches before any nodes are visited on the right-most branches visiting the right branches deeply before the left would still be depth-first! Crucial idea is “go deep first!” In DFS the nodes “being worked on” are kept on a stack (where?)

Level-Order/Breadth-first Traversal Consider task of traversing tree level by level from top to bottom (alphabetic order) What data structure to use to keep track of nodes?? a i d hj b f kl e c g

Level-Order (Breadth First) Traversal Put root in a Queue Repeat until Queue is empty: –Dequeue a node –Process it –Add it’s children to queue

Example: Printing the Tree print(node * root){ node * n, c; queue Q; Q.enqueue(root); while (! Q.empty()){ n = Q.dequeue(); print n->data; c = n->first_child; while (! c==NULL){ Q.enqueue(c); c = c->next_sibling; } } }

QUEUE a b c d e c d e f g d e f g e f g h i j f g h i j g h i j h i j k i j k j k l k l l a i d hj b f kl e c g

Applications of BFS Find the shortest path from the root to a given node N –if N is at depth k, BFS will never visit a node at depth>k –important for really deep trees Generalizes to finding shortest paths in graphs Spidering the world wide web –From a root URL, fetch pages that are further and further away

Coming Up Binary Trees & Binary Search Trees (finally!) –Weiss 4.2 – 4.3 Section 4.3 is quite long. It will probably take us two lectures to get through it.