Binary Tree Iterators and Properties Displayable Binary Trees - - PowerPoint PPT Presentation

binary tree iterators and properties displayable binary
SMART_READER_LITE
LIVE PREVIEW

Binary Tree Iterators and Properties Displayable Binary Trees - - PowerPoint PPT Presentation

Binary Tree Iterators and Properties Displayable Binary Trees Displayable Binary Trees (next assignment) DE DEPARTMENT OF OF COM OMPUTER S SCIENCE & & SO SOFTW TWARE RE E ENGINEERI RING PI PICN CNIC SA SATU TURD RDAY


slide-1
SLIDE 1

Binary Tree Iterators and Properties Displayable Binary Trees

Displayable Binary Trees (next assignment)

slide-2
SLIDE 2

DE DEPARTMENT OF OF COM OMPUTER S SCIENCE & & SO SOFTW TWARE RE E ENGINEERI RING PI PICN CNIC SA SATU TURD RDAY SE Y SEPTEMBER 28, R 28, 2013 2013 HA HAWT WTHO HORN P PARK BURKEY KEYBYLE S LE SHELTER ER 3: 3:00 00 – 6: 6:00 00 P.M. Outd tdoor

  • or ga

games at 3 t 3:00 p.m. Food serv served ed 4:3 :30 p.m .m. Food

  • od ca

cate tered b by y Qdo Qdoba ba Mexic xican G Grill ill FOOD S FOOD SPON ONSOR ORED BY Y X-by by-2

slide-3
SLIDE 3

 Displayable Binary Trees  Binary Tree iterators  Another induction example  WA4 hints, questions  # of nodes in Binary tree with height h

slide-4
SLIDE 4

Individual assignment

slide-5
SLIDE 5

 Levels all spaced

evenly.

 Level-to-right

spaced evenly,

  • rdered by in-
  • rder traversal.

 Node and font

sizes depend on window size

See demo of solution, trees #9 and #7

slide-6
SLIDE 6

 Suggested order for your graphics work:

  • Figure out how to calculate node locations
  • Get code to display correctly sized windows
  • Add code to draw nodes
  • Add code to draw lines
  • Only work on arrow heads if all the rest works!
slide-7
SLIDE 7

 Check out Dis

ispla layable le from your individual repo.

 If you get errors on the Weiss imports like

import weiss.nonstandard.Stack;

then install the Weiss packages now (see link from Schedule page)

 Should be no errors.  If errors, see next slide.

slide-8
SLIDE 8

 Close all Eclipse projects except Displayable  Did you put jars in the right folder?  Are they jars and not zips?  Is Eclipse using that JRE?

  • See Windows  Preferences,

then Java  Installed J JREs  Edit.

  • They should be in that list.

Get help now if you’re stuck. Help others if you aren’t.

slide-9
SLIDE 9

 Solve the sub-problems in this order:

  • BuildTree.preOrderBuild()

()

  • BinaryTree.inOrder()

()

  • Graphics

 Run CheckDisplaybleBinaryTree to test

  • Doesn’t use JUnit
  • Tests preOrderBuild and inOrder first
  • Prompts for test case for which to display graphics
  • Each tree should be displayed in a separate window.
slide-10
SLIDE 10

 Add a stack trace in main()

slide-11
SLIDE 11

 Like WA4, problem 3  Consider:

  • chars = ‘

‘ROSEHULMAN’

  • children = ‘

‘22002R0RL0’

slide-12
SLIDE 12

 The iterators in TestTreeIterators.java are there

for a reason!

 Recall how we can use Weiss iterators in a for

loop:

  • for(

for(iter.first iter.first(); ();iter.isValid iter.isValid(); ();iter.advance iter.advance()) { ()) { Object Object elem elem = = iter.retrieve iter.retrieve(); (); // // … do something with elem … }

slide-13
SLIDE 13

 /** Replaces the root element of this

/** Replaces the root element of this * tree with the given item and the * tree with the given item and the * * subtrees subtrees with the given ones. with the given ones. * … */ * … */ public void merge(T public void merge(T rootItem rootItem, BinaryTree BinaryTree<T> left, <T> left, BinaryTree BinaryTree<T> right) <T> right)

 Simple approach:

  • this.root

this.root = new = new BinaryTreeNode BinaryTreeNode<T>( <T>(rootItem rootItem, , left.root left.root, , right.root right.root); );

What could go wrong?

slide-14
SLIDE 14

 A node should be part of one and only one

tree.

slide-15
SLIDE 15

Weiss, figure 18.16

slide-16
SLIDE 16

What if we want to iterate over the elements in the nodes of the tree one-at-a-time instead of just printing all of them?

slide-17
SLIDE 17

 How do you “slow down” recursion to be one

step at a time?

  • Hint: what data structure is used to hold recursive

calls at runtime?

 How many times is each node visited in a

traversal/iterator?

  • See the visualization linked to from day 9 schedule.
slide-18
SLIDE 18

 What methods does an iterator typically provide?  How do we get to the first item in:

  • a pre-order traversal?
  • an in-order traversal?
  • a post-order traversal?

 In what order should we advance?  What instance variables do we need?  The Displayable

layable project has Weiss’s TestTreeIter TreeIterators ators implementation

  • Most of the code is on the next slides.
slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27

Q1 Q1-4

slide-28
SLIDE 28

Weiss’s way isn’t the only one

slide-29
SLIDE 29

 Each node can store pointer to the next node

in a traversal

 Must update extra info in constant time as

tree changes

An upcoming written assignment will include these “threaded binary trees”

slide-30
SLIDE 30

 If we did not have to maintain the stack for

these iterators?

 If we could somehow “tap into” the stack used

in the recursive traversal?

  • I.e. Take a “snapshot of that call stack, and restore

it later when we need it.

  • This is called a contin

inuatio ion.

 A big subject in the PLC course, CSSE 304

slide-31
SLIDE 31
slide-32
SLIDE 32

 Railroad switching  Problem is

equivalent to counting the number

  • f possible orders

the cars can leave the station

A B C D

Stack

slide-33
SLIDE 33

 Make up tiny examples like the given problem

  • No really tiny, I’m serious

 Solve the tiny problem  Solve a slightly larger problem  Solve a slightly larger problem than that  Once you see the pattern, then try to solve

the given problem

slide-34
SLIDE 34

 In how many

possible orders can the cars leave the station?

A B C D

Stack

slide-35
SLIDE 35

If a tree falls in the forest and there are two people around to hear it…

slide-36
SLIDE 36
slide-37
SLIDE 37

 A Binary Tree is either

  • empty

ty, or

  • consists

ts o

  • f:

 a distinguished node called the root, which contains an element, and two disjoint subtrees  A left subtree TL, which is a binary tree  A right subtree TR, which is a binary tree

root TL TR

slide-38
SLIDE 38

 Notation:

  • Let T be a tree
  • Write h(T)

(T) for the height of the tree, and

  • N(T)

(T) for the size (i.e., number of nodes) of the tree

 Given h(T), what are the bounds on N(T)?  Given N(T), what are the bounds on h(T)?

Q5 Q5-6

slide-39
SLIDE 39

 A tree with the maximum number of nodes for

its height is a full ll tree.

  • Its height is O(lo

(log N N)

 A tree with the minimum number of nodes for

its height is essentially a .

  • Its height is O(N

(N)

 Height matters!

  • We will see that the algorithms for search, insertion,

and deletion in a Binary search tree are O(h(T) (T))

Q7 Q7-8

slide-40
SLIDE 40

 Want to prove some properties about trees  Weak induction isn’t enough  Need strong induction instead: The former governor of California

slide-41
SLIDE 41

 To prove that p(n) is true for all n >= n0:

  • Prove that p(n0) is true, and
  • For all k > n0, prove that if we assume

p(j) is true for n0 ≤ j < k, then p(k) is also true

 Weak induction uses the previous domino to

knock down the next

 Strong induction uses a whole box of

dominoes to knock down the rest!

Q9 Q9-11 11