Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #6 (end of Lists, then) Trees Alon Halevy Spring Quarter 2001."— Presentation transcript:

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

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

3 3x 2001 + 4 ( 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 )

4 Sparse List Data Structure: 3x 2001 + 4 4040 3 2001 ( )

5 Addition of Two Polynomials Similar to merging two sorted lists – O(n+m) 10 50 3 1200 15+10x 50 +3x 1200 15 0 p 30 50 4 100 5+30x 50 +4x 100 5050 q 4 100 3 1200 40 50 20 0 r

6 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.

7 Array of Linked Lists: Adjacency List for Graphs 435 1 4 13 4 2 5 G 1 2 3 4 5 53 52 Array G of unordered linked lists Each list entry corresponds to an edge in the graph

8 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 }

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

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

11 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

12 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

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

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

15 Basic Tree Data Structure data next_sibling first_child a bcde 

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

17 Actual Data Structure abcdefghijkl

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

19 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

20 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; } }

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

22 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.

23 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

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

25 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; }

26 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 ))

27 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

28 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?)

29 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

30 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

31 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; } } }

32 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

33 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

34 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.


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

Similar presentations


Ads by Google