ECE 242 Data Structures Lecture 19 Tree Traversal October 23, - - PDF document

ece 242 data structures
SMART_READER_LITE
LIVE PREVIEW

ECE 242 Data Structures Lecture 19 Tree Traversal October 23, - - PDF document

ECE 242 Data Structures Lecture 19 Tree Traversal October 23, 2009 ECE242 L19: Tree Traversal Overview Problem: How do we access the data located in trees Well-defined approaches to access data in an orderly fashion Trees can be


slide-1
SLIDE 1

ECE242 L19: Tree Traversal October 23, 2009

ECE 242 Data Structures

Lecture 19

Tree Traversal

ECE242 L19: Tree Traversal October 23, 2009

Overview °Problem: How do we access the data located in trees ° Well-defined approaches to access data in an

  • rderly fashion

° Trees can be implemented in either arrays or with linked structures

  • The traversal approaches are implementation independent

° Recursive patterns can be developed and used

slide-2
SLIDE 2

ECE242 L19: Tree Traversal October 23, 2009

Tree Traversal ° Four meaningful orders in which to traverse a binary tree.

  • Preorder
  • Inorder
  • Postorder
  • Level order

ECE242 L19: Tree Traversal October 23, 2009

Preorder traversal °Preorder traversal is accomplished by visiting each node, followed by its children, starting with the root °Given the complete binary tree on the next slide, a preorder traversal would produce the order:

A B D E C

°Stated in pseudocode, the algorithm for a preorder traversal of a binary tree is:

Visit node Traverse(left child) Traverse(right child)

slide-3
SLIDE 3

ECE242 L19: Tree Traversal October 23, 2009

A complete tree

ECE242 L19: Tree Traversal October 23, 2009

Preorder Traversal

Note that this code is recursive

slide-4
SLIDE 4

ECE242 L19: Tree Traversal October 23, 2009

Preorder Traversal Without Recursion

ECE242 L19: Tree Traversal October 23, 2009

Inorder traversal °Inorder traversal is accomplished by visiting the left child of the node, then the node, then any remaining child nodes starting with the root °An inorder traversal of the previous tree produces the order:

D B E A C

°Stated in pseudocode, the algorithm for an inorder traversal of a binary tree is:

Traverse(left child) Visit node Traverse(right child)

slide-5
SLIDE 5

ECE242 L19: Tree Traversal October 23, 2009

Inorder Traversal

Print left tree, node, right tree

ECE242 L19: Tree Traversal October 23, 2009

Postorder traversal °Postorder traversal is accomplished by visiting the children, then the node starting with the root °Given the same tree, a postorder traversal produces the following order:

D E B C A

  • Stated in pseudocode, the algorithm for a postorder

traversal of a binary tree is:

Traverse(left child) Traverse(right child) Visit node

slide-6
SLIDE 6

ECE242 L19: Tree Traversal October 23, 2009

Postorder Traversal

Note the recursive calls

ECE242 L19: Tree Traversal October 23, 2009

Levelorder traversal °Levelorder traversal is accomplished by visiting all

  • f the nodes at each level, one level at at time,

starting with the root °Given the same tree, a levelorder traversal produces the order:

A B C D E

slide-7
SLIDE 7

ECE242 L19: Tree Traversal October 23, 2009

Levelorder traversal

  • Stated in pseudocode, the algorithm for a level order

traversal of a binary tree is:

ECE242 L19: Tree Traversal October 23, 2009

Level Order Traversal

slide-8
SLIDE 8

ECE242 L19: Tree Traversal October 23, 2009

Tree Traversal Complexity ° Level order traversal is sometimes called breadth- first. ° The other traversals are called depth-first. ° Traversal takes O(n) in both breadth-first and depth-first. ° Memory usage in a perfect tree is O(log n) in depth-first and O(n) in breadth-first traversal.

ECE242 L19: Tree Traversal October 23, 2009

Tree Traversal

slide-9
SLIDE 9

ECE242 L19: Tree Traversal October 23, 2009

Breadth-First vs. Depth-First Traversal

Breadth-first Depth first

ECE242 L19: Tree Traversal October 23, 2009

Summary °Trees can be accessed in many different ways

  • Often the application dictates the implementation

°Depth-first and breadth-first access are popular °Postorder and Preorder traversals are often recursive °Recursion can be eliminated to make the methods iterative