Binary Tree Iterators and Properties Displayable Binary Trees - - PowerPoint PPT Presentation
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 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
Displayable Binary Trees Binary Tree iterators Another induction example WA4 hints, questions # of nodes in Binary tree with height h
SLIDE 4
Individual assignment
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
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
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
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
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
Add a stack trace in main()
SLIDE 11
Like WA4, problem 3 Consider:
- chars = ‘
‘ROSEHULMAN’
- children = ‘
‘22002R0RL0’
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
/** 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
A node should be part of one and only one
tree.
SLIDE 15
Weiss, figure 18.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
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
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 20
SLIDE 21
SLIDE 22
SLIDE 23
SLIDE 24
SLIDE 25
SLIDE 26
SLIDE 27
Q1 Q1-4
SLIDE 28
Weiss’s way isn’t the only one
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
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 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
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
In how many
possible orders can the cars leave the station?
A B C D
Stack
SLIDE 35
If a tree falls in the forest and there are two people around to hear it…
SLIDE 36
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
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
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
Want to prove some properties about trees Weak induction isn’t enough Need strong induction instead: The former governor of California
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