foundations of software design fall 2002 marti hearst

Post on 13-Feb-2016

40 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Foundations of Software Design Fall 2002 Marti Hearst. Lecture 15: Trees. Trees. Trees. Trees are very important and useful They are usually drawn upside-down The allow us to represent hierarchy File system Book structure Employees in a bureacracy Every node has exactly one parent - PowerPoint PPT Presentation

TRANSCRIPT

1

Foundations of Software DesignFall 2002Marti Hearst

Lecture 15: Trees 

 

2

Trees

3

Trees• Trees are very important and useful• They are usually drawn upside-down• The allow us to represent hierarchy

– File system– Book structure– Employees in a bureacracy

• Every node has exactly one parent – Except the root

4

Anatomy of a Tree

root

siblings

parent &child

Ancestor & descendent

5

Tree Terminology• Parent / Child

– If node u is the parent of node v, then v is the child of u• Siblings

– Two nodes that are children of the same parent.• Leaf (External node)

– A node is a leaf if it has no children• Internal node

– A node is internal if it has one or more children• Ancestor / Descendent

– An ancestor of a node is either the node itself or an ancestor of the parent of the node

– (this is recursive)• Subtree

– The subtree of T rooted at node v is the tree consisting of all the descendents of v in T (including v)

Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html

6

Binary Tree• The simplest form of tree is a Binary Tree• A Binary Tree consists of

– (a) A node (called the root node) and– (b) Left and right subtrees– Both the subtrees are themselves binary trees

• Note: this is a recursive definition

7

Binary Tree

Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html

8

Tree Terminology• Proper tree

– A binary tree is proper if every node has either 2 or 0 children

– Another way to say this: • all internal nodes have exactly 2 children

– Even an improper binary tree is still a tree• Complete tree

– A tree (binary or otherwise) is complete if every leaf is the same distance from the root

Proper treeComplete tree

9

Trees vs. Graphs / Networks• The Web is not a tree. Why not?

– Nodes in a tree can have only one parent• Graphs / Networks are the more general case

10

Tree TerminologyHeight

– The height of a tree is the height of the root• Intuitively – the length of the longest path from the root

to a leaf, across all the leaves– More formally, the height of a node v is

• 0 if v is a leaf• 1 + maximum height of v’s children

– (this is a recursive definition)

Adapted from http://www.oopweb.com/Algorithms/Documents/PLDS210/VolumeFrames.html

11

Tree TerminologyDepth

– The depth of a node v in T is the number of ancestors of v, excluding v itself.

– (The inverse of height, from the node’s viewpoint)– More formally:

• If v is the root, the depth of v is 0• Otherwise, the depth of v is one plus the depth of the

parent of v

12

Our Binary Tree• Defined recursively as consisting of

– Data– A lefthand Binary Tree– A righthand Binary Tree

13

Simple Binary Tree Code

14

15

16

Recursion vs. Iterationprinting the left side of the tree

17

Tree Traversal• Preorder• Inorder• Postorder

• All defined in terms of – When do you visit the node itself– When do you visit the lefthand side– When do you visit the righthand side

root

aaa

abaa

a

aab

b

18

PreOrder root

aaa

abaa

a

aab

b

Why? Textual representation of the tree (parents before children)

19

InOrderroot

aaa

abaa

a

aab

b

Why? Useful for searching in ordered trees

20

PostOrderroot

aaa

abaa

a

aab

b

Why? Computing a property of a node depends on the nodes below it.

21

Depth-first Search

From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html

Full tree

DFS (to see it, run inPresentationMode)

Note: DFS traversal is equivalent to PreOrder traversal

22

Breadth-first Search

From http://www.rci.rutgers.edu/~cfs/472_html/AI_SEARCH/ExhaustiveSearch.html

Full tree

BFS (to see it, run inPresentationMode)

What do we need besides the runtime stack to make BFS work?

23

Breadth First Traversal

We can reuse our Queue!!

24

Depth First Traversal

25

Depth First Search

26

Algorithm Analysis• What is the running time for

– Depth First Traversal?• O(n)

– Depth First Search?• Worst case: O(n)

– Breadth First Traversal?• O(n)

– Breadth First Search• Worst case: O(n)

– How could we improve these search times?

top related