Top Banner
CSE 326 Trees David Kaplan Dept of Computer Science & Engineering Autumn 2001
26

CSE 326 Trees

Jan 03, 2016

Download

Documents

halee-mcdowell

CSE 326 Trees. David Kaplan Dept of Computer Science & Engineering Autumn 2001. Trees. Family Trees Organization Charts Classification trees what kind of flower is this? is this mushroom poisonous? File directory structure Parse trees (x + y * z) Search trees - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 326 Trees

CSE 326Trees

David Kaplan

Dept of Computer Science & EngineeringAutumn 2001

Page 2: CSE 326 Trees

TreesCSE 326 Autumn 2001

2

Trees Family Trees Organization Charts Classification trees

what kind of flower is this? is this mushroom poisonous?

File directory structure Parse trees (x + y * z) Search trees Non-recursive procedure call chains

Non-recursive? (I thought a tree was a recursive structure …)

Page 3: CSE 326 Trees

TreesCSE 326 Autumn 2001

3

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, …, Tkr

T1 T2 T3

Page 4: CSE 326 Trees

TreesCSE 326 Autumn 2001

4

Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

root:leaf:child:parent:sibling:ancestor:descendent:subtree:

Page 5: CSE 326 Trees

TreesCSE 326 Autumn 2001

5

More Tree TerminologyA

E

B

D F

C

G

IH

LJ MK N

depth:

height:

degree:

branching factor:

Page 6: CSE 326 Trees

TreesCSE 326 Autumn 2001

6

YMTT (Yet More Tree Terminology)

JIH

GFED

CB

A

binary:

n-ary:

complete:

Page 7: CSE 326 Trees

TreesCSE 326 Autumn 2001

7

Tree Calculations Example:

Longest Undirected PathFind longest undirected path

ignore direction don’t repeat nodes

ObservationsLongest undirected path is either: longest path within a subtree longest path through root

If path goes through root, its length is:height(tallest subtree) + height(next-tallest subtree) + 2

Why?

r

Page 8: CSE 326 Trees

TreesCSE 326 Autumn 2001

8

Tree Calculations Example:

Longest Undirected PathAlgorithm

Start at rootRecursively calculate

{LUD path length, height}

for each subtree

A

E

B

D F

C

G

IH

KJ L

M

L

N

{2,2}{?,?}

{?,?}

{?,?}{2,1}

{?,?}

Page 9: CSE 326 Trees

TreesCSE 326 Autumn 2001

9

Logical View of Treea

i

d

h j

b

f

k l

ec

g

Page 10: CSE 326 Trees

TreesCSE 326 Autumn 2001

10

Basic Tree Data Structure:

First child/next sibling

data

first_child

a

b c d e

next_sibling

Page 11: CSE 326 Trees

TreesCSE 326 Autumn 2001

11

Actual Data Structurea

b c d e

f gh i j

k l

Page 12: CSE 326 Trees

TreesCSE 326 Autumn 2001

12

Combined View of Treea

i

d

h j

b

f

k l

ec

g

Page 13: CSE 326 Trees

TreesCSE 326 Autumn 2001

13

Tree 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: node, then children Post-order: children, then node Level-order: nodes at depth d, nodes at depth d+1, … In-order: left, then node, then right (specific to binary

trees)

Page 14: CSE 326 Trees

TreesCSE 326 Autumn 2001

14

Pre-Order TraversalPerform 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; } }}

Page 15: CSE 326 Trees

TreesCSE 326 Autumn 2001

15

Pre-Order Traversal Example

a

i

d

h j

b

f

k l

ec

g

Page 16: CSE 326 Trees

TreesCSE 326 Autumn 2001

16

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 )

Example: printing out a directory structure

Page 17: CSE 326 Trees

TreesCSE 326 Autumn 2001

17

Pre-Order Example:

Computing Depth of All Nodes Add a field depth to

all nodes Call Depth(root,0) to

set depth field

Depth(node * n, int d) { node * c; if (n != NULL) { n->depth = d; c = n->first_child; while (c != NULL) { Depth(c, d+1); c = c->next_sibling; } }

}

Page 18: CSE 326 Trees

TreesCSE 326 Autumn 2001

18

Post-Order TraversalRecursively perform computation on each child, 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;

}

Page 19: CSE 326 Trees

TreesCSE 326 Autumn 2001

19

Post-Order Applications Use when computation at node depends on

values calculated lower in tree (closer to leaves)

Example: computing heightheight(node) = 1 + MAX( height(child1), … height(childk) )

Example: size of tree rooted at node size(node) = 1 + size(child1) + … + size(childk)

Page 20: CSE 326 Trees

TreesCSE 326 Autumn 2001

20

Post-Order Example:

Computing Size of Tree Call Size(root) to

compute number of nodes in tree

int Size(node * n) { node * c; if (n == NULL) return 0; else { int m = 1; c = n->first_child; while (c != NULL) { m += Size(c); c = c->next_sibling; } } return m;}

Page 21: CSE 326 Trees

TreesCSE 326 Autumn 2001

21

Depth-First Search Pre-Order and Post-Order traversals are

examples of depth-first search: Nodes are visited deeply on left-most branches before any nodes are visited on right-most branches

NOTE: visiting right deeply before 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?)

Page 22: CSE 326 Trees

TreesCSE 326 Autumn 2001

22

Level-Order (Breadth-First) Traversal Consider task of traversing tree level-by-level from

top to bottom (alphabetic order, in example below)

Which data structure can best keep track of nodes?

a

i

d

h j

b

f

k l

ec

g

Page 23: CSE 326 Trees

TreesCSE 326 Autumn 2001

23

Put root in a Queue

Repeat until Queue is empty: Dequeue a node Process it Add its children to queue

Level-Order (Breadth-First) Algorithm

Page 24: CSE 326 Trees

TreesCSE 326 Autumn 2001

24

Level-Order Example:

Printing the Tree Call Print(root) to

print tree contents

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

Page 25: CSE 326 Trees

TreesCSE 326 Autumn 2001

25

a

i

d

h j

b

f

k l

ec

g

Process Enqueue

Q

a a

a b,c,d,e bcde

b f,g cdefg

c defg

d h,i,j efghij

e fghij

f ghij

g k hijk

h ijk

i l jkl

j kl

k l

l

Example:

Level-Order Queue

Page 26: CSE 326 Trees

TreesCSE 326 Autumn 2001

26

Applications of

Breadth-First Search Find shortest path from 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 farther and

farther away