cse 326: data structures lecture #6 (end of lists, then) trees alon halevy spring quarter 2001

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

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

CSE 326: Data StructuresLecture #6

(end of Lists, then) Trees

Alon Halevy

Spring Quarter 2001

Application: Polynomial ADT

Ai is the coefficient of the xn-i term:

3x2 + 2x + 5 ( 3 2 5 )

8x + 7 ( 8 7 )

x2 + 3 ( 1 0 3 )

Problem?

3x2001 + 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 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 3 )

Sparse List Data Structure:3x2001 + 4

40

32001

(<4 0> <2001 3>)

Addition of Two Polynomials• Similar to merging two sorted lists – O(n+m)

1050

31200

15+10x50+3x1200

150

p

3050

4100

5+30x50+4x100

50

q

4100

31200

4050

200

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

4 3 5

1 4

1 3

4

2

5

G12345

5 3

5 2

•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 falses.

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 TreeRecursive 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

T1 T2 T3

Tree Terminology

rootchildparentsibling

path

descendentancestor

a

i

d

h j

b

f

k l

ec

g

More Tree Terminology

a

i

d

h j

b

f

k l

subtreeleaf

depthheight

branching factorn-arycomplete

ec

g

Basic Tree Data Structure

data

next_siblingfirst_child

a

b c d e

Logical View of Tree

a

i

d

h j

b

f

k l

ec

g

Actual Data Structurea

b c d e

f gh i j

k l

Combined View of Tree

a

i

d

h j

b

f

k l

ec

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

h j

b

f

k l

ec

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 depthdepth(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

h j

b

f

k l

ec

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 heightheight(node) = 1 +

MAX( height(child1), height(child2), … height(childk))

• Example: size of tree rooted at node size(node) = 1 +

size(child1) + size(child2) + … + size(childk))

Computing Size of TreeSize(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

h j

b

f

k l

ec

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 Treeprint(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

h j

b

f

k l

ec

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.