Different Features. Glasses vs. No Glasses Beard vs. No Beard.
Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to...
-
Upload
gerald-poole -
Category
Documents
-
view
215 -
download
0
Transcript of Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to...
![Page 1: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/1.jpg)
Problem of the Day
Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?
![Page 2: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/2.jpg)
Problem of the Day
Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?
Take the 2nd glass & pour it into the 5th glass!
![Page 3: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/3.jpg)
LECTURE 36:TREE IMPLEMENTATION
CSC 212 – Data Structures
![Page 4: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/4.jpg)
“Node” Sounds Technical…
Links in linked list commonly called “nodes”
Nodes name for links in a TREE also Position interface implemented by the
node class Each node’s data retrieved using element()
Node class often used by data structures Node is not specific, but sounds better than
“thingy” Position interface implemented by node
(usually)
![Page 5: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/5.jpg)
Node Heights
Longest distance to leaf node D's height is 0 B's height is 2 A's height is 3
A
B DC
G HE F
I J K
![Page 6: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/6.jpg)
Node Heights
Height 1 more than taller child F's height is 1 B's height is 2 E's height is 0
Height of leaf always 0
A
B DC
G HE F
I J K
![Page 7: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/7.jpg)
Node Depths
Distance to root node of Tree A's depth is 0 C's depth is 1 I's depth is 3
A
B DC
G HE F
I J K
![Page 8: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/8.jpg)
Node Depths
Depth of a node is1 more than its parent Root's depth always 0 B, C, & D have depth of 1 E, F, G, H have depth of 2 I, J, K have depth of 3
A
B DC
G HE F
I J K
![Page 9: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/9.jpg)
Tree Interface
Interesting methods from the interfacePosition<E> root()Position<E> parent(p)Iterable<Position<E>> children(p)boolean isInternal(p)boolean isExternal(p)boolean isRoot(p)E replace (p, e)
![Page 10: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/10.jpg)
Tree Interface
Often have other methods beyond these in class
![Page 11: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/11.jpg)
Tree Interface
Often have other methods beyond these in class For example, want methods to add or
remove data
![Page 12: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/12.jpg)
Tree Interface
Often have other methods beyond these in class For example, want methods to add or
remove data
![Page 13: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/13.jpg)
Tree Interface
Often have other methods beyond these in class For example, want methods to add or
remove data
Else root & size fields needed to change Tree
![Page 14: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/14.jpg)
Node Class For Tree
class TNode<E> implements Position<E> {private E element;private TNode<E> parent;private Sequence<TNode<E>> kids;
// Besides getters & setters, often include methods like:public TNode<E> getChild(int i) { return kids.get(i);}public void addChild(TNode<E> kid) { kids.addLast(kid);}public TNode<E> removeChild(int i) { return kids.remove(i); }
}
![Page 15: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/15.jpg)
Tree
D
Visualization of Tree
B
DA
C E
F
B
A F
C E
Tree
root
size6
![Page 16: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/16.jpg)
D
Node Structure of the Tree
B
DA
C E
F
B
A F
C E
![Page 17: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/17.jpg)
D
Links Between Tree's Nodes
B
DA
C E
F
B
A F
C E
![Page 18: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/18.jpg)
D
Links Between Tree's Nodes
B
DA
C E
F
B
A F
C E
![Page 19: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/19.jpg)
Tree
D
Actual View of Tree
B
DA
C E
F
B
A F
C E
Tree
root
size6
![Page 20: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/20.jpg)
View of an Actual Tree
![Page 21: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/21.jpg)
Linked Node for BinaryTree
class BTNode<E> implements Position<E> {private E element;private BTNode<E> parent;private BTNode<E> left;private BTNode<E> right;
// Add getters & setters for each field
public Iterable<Position<E>> children(){ IndexList<Position<E>> il = new … il.add(0, left); il.add(1, right); return il;}
}
![Page 22: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/22.jpg)
BinaryTree
Picturing Linked BinaryTree
B
CA
D
B
A C
D
BinaryTree
root
size4
![Page 23: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/23.jpg)
Nodes in Linked BinaryTree
B
CA
D
B
A C
D
![Page 24: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/24.jpg)
Pointers in LinkedBinaryTree
B
CA
D
B
A C
D
![Page 25: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/25.jpg)
Array-based BinaryTree
Node at index specified for location in TREE Root node stored at index 0 Root’s left child at index 1 Right child of root at index 2 Left child’s right child at index 4 Right child’s left child at index 5 Node at index n’s left child is at index 2n + 1 Node at index n’s right child is at index 2n +
2
![Page 26: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/26.jpg)
Array-based Implementation
Tree’s depth limited by array size Where in array determined by location
Sequence overcomes limits null added to pad space replace() used to add nodes
![Page 27: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/27.jpg)
0 1 2 3 4 5 6 7 8 9 10
Array-based Impl.
A
HG
FE
D
C
B
J
![Page 28: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/28.jpg)
0 1 2 3 4 5 6 7 8 9 10
Array-based Impl.
A
HG
FE
D
C
B
J
A
B D
E F C J
G H
![Page 29: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/29.jpg)
0 1 2 3 4 5 6 7 8 9 10
2nd Array-based Impl.
A
HG
FE
D
C
B
J
A
B D
E F C J
G H
![Page 30: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/30.jpg)
0 1 2 3 4 5 6 7 8 9 10
Correct Array-based Impl.
A
HG
FE
D
C
B
J4
F
6
J
0
A
1
B
2
D
3
E
5
C
9
G
10
H
![Page 31: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/31.jpg)
0 1 2 3 4 5 6 7 8 9 10
Array Refers to Positions
A
HG
FE
D
C
B
J4
F
6
J
0
A
1
B
2
D
3
E
5
C
9
G
10
H
![Page 32: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/32.jpg)
0 1 2 3 4 5 6 7 8 9 10
Nodes Implicitly Linked Only
A
HG
FE
D
C
B
J4
F
6
J
0
A
1
B
2
3
E
5
C
9
G
10
H
D
![Page 33: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/33.jpg)
0 1 2 3 4 5 6 7 8 9 10
Nodes Implicitly Linked Only
A
HG
FE
D
C
B
J4
F
6
J
0
A
1
B
2
3
E
5
C
9
G
10
H
D
![Page 34: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/34.jpg)
Node for Array-based Impl.
class BANode<E> implements Position<E> {private E element;private int myIndex;private boolean hasLeft;private boolean hasRight;
// Add getters & setters for each field
public int getLeft() { if (!hasLeft) { return -1; } return (myIndex * 2) + 1;}public void setRight() { hasRight = true; }
}
![Page 35: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/35.jpg)
Your Turn
Get into your groups and complete activity
![Page 36: Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?](https://reader035.fdocuments.us/reader035/viewer/2022062500/5697bff91a28abf838cbfd53/html5/thumbnails/36.jpg)
For Next Lecture
Read GT 8.1 – 8.1.2 for Wednesday's lecture What is an Entry and why would you use Entry?
Comparable sounds cool; what does it mean?
What if we need more complex data than elements?
Week #13 posted tomorrow & due next Tuesday Week #12 due tomorrow as you should be
used to
Programming Assignment #3 available now