cs 261 – winter 2009

23
CS 261 – Winter 2009 Trees

Upload: leon

Post on 08-Jan-2016

33 views

Category:

Documents


0 download

DESCRIPTION

CS 261 – Winter 2009. Trees. Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: Vectors and Arrays Lists Trees. Tree Characteristics. A tree consists of a collection of nodes connected by directed arcs A tree has a single root node - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 261 – Winter 2009

CS 261 – Winter 2009

Trees

Page 2: CS 261 – Winter 2009

Trees

• Ubiquitous – they are everywhere in CS

• Probably ranks third among the most used data structure:

1. Vectors and Arrays

2. Lists

3. Trees

Page 3: CS 261 – Winter 2009

Tree Characteristics• A tree consists of a collection of nodes connected by

directed arcs

• A tree has a single root node– By convention, the root node is usually drawn at the top

• A node that points to (one or more) other nodes is the parent of those nodes while the nodes pointed to are the children

• Every node (except the root) has exactly one parent

• Nodes with no children are leaf nodes

• Nodes with children are interior nodes

Page 4: CS 261 – Winter 2009

Tree Characteristics (cont.)

• Nodes that have the same parent are siblings

• The descendents of a node consist of its children, and their children, and so on– All nodes in a tree are descendents of the root node (except, of

course, the root node itself)

• Any node can be considered the root of a subtree– Like a subset, a subtree need not be “proper” (i.e., be strictly smaller

than the original)

• A subtree rooted at a node consists of that node and all of its descendents

Page 5: CS 261 – Winter 2009

Tree Characteristics (cont.)

• There is a single, unique path from the root to any node– Arcs don’t join together

• A path’s length is equal to the number of arcs traversed

• A node’s height is equal to the maximum path length from that node to a leaf node:– A leaf node has a height of 0– The height of a tree is equal to the height of the root

• A node’s depth is equal to the path length from the root to that node:– The root node has a depth of 0

– A tree’s depth is the maximum depth of all its leaf nodes (which, of course, is equal to the tree’s height)

Page 6: CS 261 – Winter 2009

Tree Characteristics (cont.)

Root (depth = 0, height = 4)A

B C

D E

Subtree rootedat node C

• Nodes D and E are children of node B

• Node B is the parent of nodes D and E

• Nodes B, D, and E are descendents of node A (as are all other nodes in the tree…except A)

• E is an interior node

• F is a leaf node

FLeaf node (depth = 4, height = 0)

Page 7: CS 261 – Winter 2009

Tree Characteristics (cont.)

Are these trees?

Yes No No

Page 8: CS 261 – Winter 2009

Binary Tree

• Nodes have no more than two children:– Children are generally ordered from left to right

• Full Binary Tree: every leaf is at the same depth

– Every internal node has 2 children

– Height of n will have 2n+1 – 1 nodes

– Height of n will have 2n leaves

Page 9: CS 261 – Winter 2009

Binary Tree

• Nodes have no more than two children:– Children are generally ordered from left to right

• Full Binary Tree: every leaf is at the same depth

– Every internal node has 2 children

– Height of n will have 2n+1 – 1 nodes

– Height of n will have 2n leaves

• Complete Binary Tree:full except for the bottomlevel which is filled fromleft to right

Page 10: CS 261 – Winter 2009

Relationship of Height to Number of Nodes• If a complete binary tree has N nodes, what it its height?

• We will come back to this when

We later have algorithms

That run in time

Proportional to the

Path length

Page 11: CS 261 – Winter 2009

Array Implementation

• Complete binary tree have structure that is efficiently implemented with an array:

– Children of node i are stored at 2i + 1 and 2i + 2

– Parent of node i is at floor((i - 1) / 2)

a

b c

d e f0a

1b

2c

3d

4e

5f

6 7

Root

Page 12: CS 261 – Winter 2009

Vector Implementation (cont.)

• If the tree is not complete (it is thin,

unbalanced, etc.), the Array

• implementation will be full of holes

a

b c

f

d e

0a

1b

2c

3 4d

5 6e

7 8 9 10 11 12 13f

14 15

Page 13: CS 261 – Winter 2009

Dynamic Memory Implementation

struct node { EleType value; struct node * left; // Left child. struct node * right; // Right child.

};

Like the Link class in LinkedList:

we will use this class in several data structures

Page 14: CS 261 – Winter 2009

Binary Tree Application: Animal Game

• Purpose: guess an animal using a sequence of questions– Internal nodes contain yes/no questions– Leaf nodes are animals– Initially, tree contains a single animal (e.g., a “cat”) stored in

the root node

1. Start at root.

2. If internal node ask yes/no question• Yes go to left child and repeat step 2• No go to right child and repeat step 2

3. If leaf node ask “I know. Is it a …”:• If right done• If wrong “learn” new animal by asking for a yes/no question that

distinguishes the new animal from the guess

Page 15: CS 261 – Winter 2009

Binary Tree Traversals

• Just as a list, it is often necessary to examine every node in a tree

• A list is a simple linear structure: can be traversed either forward or backward – but usually forward

• What order do we visit nodes in a tree?

• Most common traversal orders:– Pre-order

– In-order

– Post-order

Page 16: CS 261 – Winter 2009

Binary Tree Traversals (cont.)

• All traversal algorithms have to:– Process node– Process left subtree– Process right subtree

Traversal order determined by the order these operations are done.

• Six possible traversal orders:1. Node, left, right Pre-order

2. Left, node, right In-order

3. Left, right, node Post-order

4. Node, right, left

5. Right, node, left

6. Right, left, node

Subtrees are not usually analyzed from right to left.

Most common traversals.

Page 17: CS 261 – Winter 2009

• Process order Node, Left subtree, Right subtree

// Not in the BinaryNode class.void preorder(BinaryNode node) { if (node != null){ process (node.obj); preorder(node.left); preorder(node.rght); }}

Example result: p s a m a e l r t e e

e

l

s

a

a m r

t e

e

Pre-Order Traversal

p

Page 18: CS 261 – Winter 2009

Post-Order Traversal

• Process order Left subtree, Right subtree, Node

void postorder(BinaryNode node) { if (node != null){ postorder(node.left); postorder(node.rght); process (node.obj); }}

Example result: a a m s l t e e r e p

e

l

s

a

m r

t e

e

a

p

Page 19: CS 261 – Winter 2009

In-Order Traversal

• Process order Left subtree, Node, Right subtree

void inorder(BinaryNode node) { if (node != null){ inorder(node.left); process (node.obj); inorder(node.rght); }}

Example result: a sample tree

e

l

s

a

m r

t

p

a

e

e

Page 20: CS 261 – Winter 2009

Binary Tree Traversals: Euler Tour

• An Euler Tour “walks” around the tree’s perimeter

• Each node is visited three times:– 1st visit: left side of node

– 2nd visit: bottom side of node

– 3rd visit: right side of node

– Leaf nodes are visited threetimes in succession

• Traversal order dependson when node processed:– Pre-order: 1st visit

– In-order: 2nd visit

– Post-order: 3rd visit

e

l

s

a

m r

t

p

a

e

e

Page 21: CS 261 – Winter 2009

Pre-order: + a * + b c d (Polish notation)

In-order: a + (b + c) * d (parenthesis added)

Post-order: a b c + d * + (reverse Polish notation)

*

+

a

d

b c

Traversal Example

+

Page 22: CS 261 – Winter 2009

Traversals

• Computational complexity:– Each traversal requires constant work at each node (not including

the recursive calls)

– Order not important

– Iterating over all n elements in a tree requires O(n) time

• Problem with traversal code:– The process function must be rewritten (or replaced) for each

new task

– Programmer writing new process function sees the internal tree structure representation

– Not good information hiding

– Solution Iterator (more on this later)

Page 23: CS 261 – Winter 2009

Questions?• Do the worksheet on trees.

• Next topic, how do we make a useful data structure out of a tree?