CS 261 Data Structures - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CS 261 Data Structures

Description:

Node, left, right Pre-order. Left, node, right In-order. Left, right, node Post-order ... Preorder traversal, Postorder traversal also keep a stack. Level order ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 15
Provided by: ericnmo
Category:

less

Transcript and Presenter's Notes

Title: CS 261 Data Structures


1
CS 261 Data Structures
  • Binary search Tree Iterator
  • And other tree traversals

2
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

3
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
  • Node, left, right ? Pre-order
  • Left, node, right ? In-order
  • Left, right, node ? Post-order
  • Node, right, left
  • Right, node, left
  • Right, left, node
  • Most common traversals.
  • ?
  • Subtrees are not
  • usually analyzed
  • ? from right to left.

4
Pre-Order Traversal
  • 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

p
s
e
a
m
r
l
a
e
t
e
5
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

p
s
e
m
r
a
l
a
e
t
e
6
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

p
s
e
m
r
a
l
a
e
t
e
7
Traversal Example
  • 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

c
b
8
Suppose we want an iterator
  • Which traversal is the correct one to produce the
    values of a binary search tree in order?

9
Simple (but space inefficient) iterator
  • A simple iterator could be created by recursively
    traversing the tree, placing all the nodes into a
    list, then doing an iterator on the list.
  • Problem duplicates the data structure, uses
    twice as much space.
  • Can we do better?

10
Yes, a stack would be useful
  • As you probably guessed, a stack would be useful.
  • Need to stack up the path as we traverse down to
    find the first (and smallest) element. Remember,
    the smallest value in found at the leftmost child
  • Here is a useful routine
  • Private void slideLeft (Node n)
  • while n is not null
  • push n on stack
  • n left child of n

11
Here is the algorithm for BSTIterator
  • To initialize, create an empty stack
  • Boolean hasNext
  • if stack is empty perform slide left on root
  • otherwise let n be top of stack. Pop n
  • slide left on right child of n
  • return true if stack is not empty
  • EleType next
  • let n be top of stack. Return value of n

12
Try it. Keep track of stack as you loop
  • Can you characterize what it is that is being
    stored in the stack?

13
Other traversals
  • Preorder traversal, Postorder traversal also keep
    a stack.
  • Level order traversal keeps a queue
  • See if you can characterize the values that are
    maintained in the stack or queue for each type of
    traversal.

14
Worksheet time
  • Any questions?
Write a Comment
User Comments (0)
About PowerShow.com