Tree Traversals - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Tree Traversals

Description:

Fred Betty Barney Wilma Pebbles. In-order traversal (2) visit node between children ... Barney Betty Pebbles Wilma Fred. Tree iterators ... – PowerPoint PPT presentation

Number of Views:79
Avg rating:3.0/5.0
Slides: 24
Provided by: alph8
Category:

less

Transcript and Presenter's Notes

Title: Tree Traversals


1
Tree Traversals
  • A traversal is a way of walking the tree
    structure
  • Some common traversals
  • pre-order traversal
  • in-order traversal
  • post-order traversal

2
(Depth-first) traversal path
3
Each node is reached three times
1
3
2
1
1
3
3
2
2
1
1
3
3
2
2
4
Pre-order traversal (1)visit node before children
1
1
1
1
1
Fred ? Betty ? Barney ? Wilma ? Pebbles
5
In-order traversal (2)visit node between children
2
2
2
2
2
Barney ? Betty ? Fred ? Pebbles ? Wilma
6
Post-order traversal (3)visit node after children
3
3
3
3
3
Barney ? Betty ? Pebbles ? Wilma ? Fred
7
Example
Pre-order traversal Fred Betty Barney Wilma
Pebbles
Fred
In-order traversal Barney Betty Fred Pebbles
Wilma
Wilma
Betty
Post-order traversal Barney Betty Pebbles Wilma
Fred
Barney
Pebbles
8
Tree iterators
  • We can define tree iterators which follow the
    same traversal path.
  • Unlike the visitors, iterators stop at each node
    they must remember where they are!
  • Let us consider first an in-order iterator.

9
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

10
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

11
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

12
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Fred
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

13
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Fred
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Pebbles
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

14
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Fred
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Pebbles
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

15
Implementation?
  • How is state of iterator maintained?

16
Implementation
  • Using a stack!
  • On creation of the iterator, references to all
    nonEmpty trees along the left edge of the tree
    are pushed onto the stack
  • hasNext() is implemented to return
    !_stack.isEmpty()

17
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

18
Implementation
  • The next() method pops an item off the stack,
    walks down its right childs left edge (pushing
    BRS references onto the stack along the way)

19
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

20
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

21
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Fred
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

22
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Fred
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Pebbles
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true

23
Behavior of an inOrderIterator
  • java.util.Iterator it bst.inOrderIterator()
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Barney
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Betty
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Fred
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
  • System.out.println("it.next() "
    it.next())
  • it.next() Pebbles
  • System.out.println("it.hasNext()
    "it.hasNext())
  • it.hasNext() true
Write a Comment
User Comments (0)
About PowerShow.com