Top Banner
Tree Traversals • A traversal is a way of walking the tree structure • Some common traversals: – pre-order traversal – in-order traversal – post-order traversal
23

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

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

Tree Traversals

• A traversal is a way of walking the tree structure

• Some common traversals:– pre-order traversal– in-order traversal– post-order traversal

Page 2: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

(Depth-first) traversal path

Fred

WilmaBetty

Barney Pebbles

Page 3: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Each node is reached three times

Fred

WilmaBetty

Barney Pebbles

1

2

3

1

1

1

1

2

2

2

2

3

3

3

3

Page 4: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Pre-order traversal (1)visit node before children

Fred

WilmaBetty

Barney Pebbles

1

1

1

1

1

Fred Betty Barney Wilma Pebbles

Page 5: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

In-order traversal (2)visit node between children

Fred

WilmaBetty

Barney Pebbles

2

2

2

2

2

Barney Betty Fred Pebbles Wilma

Page 6: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Post-order traversal (3)visit node after children

Fred

WilmaBetty

Barney Pebbles 3

3

3

3

3

Barney Betty Pebbles Wilma Fred

Page 7: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Example

Fred

WilmaBetty

Barney Pebbles

Pre-order traversal:Fred Betty Barney Wilma Pebbles

In-order traversal:Barney Betty Fred Pebbles Wilma

Post-order traversal:Barney Betty Pebbles Wilma Fred

Page 8: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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.

Page 9: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Behavior of an inOrderIterator> java.util.Iterator it = bst.inOrderIterator();> System.out.println("it.hasNext() ==> "+it.hasNext());it.hasNext() ==> true

Fred

WilmaBetty

Barney Pebbles

Page 10: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 11: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 12: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 13: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 14: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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> System.out.println("it.next() ==> " + it.next());it.next() ==> Wilma> System.out.println("it.hasNext() ==> "+it.hasNext());it.hasNext() ==> false

Fred

WilmaBetty

Barney Pebbles

Page 15: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Implementation?

• How is state of iterator maintained?

Page 16: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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()

Page 17: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Behavior of an inOrderIterator> java.util.Iterator it = bst.inOrderIterator();> System.out.println("it.hasNext() ==> "+it.hasNext());it.hasNext() ==> true

Fred

WilmaBetty

Barney Pebbles

Page 18: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

Implementation:

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

Page 19: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 20: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 21: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 22: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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

Fred

WilmaBetty

Barney Pebbles

Page 23: Tree Traversals A traversal is a way of walking the tree structure Some common traversals: –pre-order traversal –in-order traversal –post-order traversal.

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> System.out.println("it.next() ==> " + it.next());it.next() ==> Wilma> System.out.println("it.hasNext() ==> "+it.hasNext());it.hasNext() ==> false

Fred

WilmaBetty

Barney Pebbles