problem of the day can you move exactly one glass to arrange the glasses containing orange juice to...

36
Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to be next to one another?

Upload: gerald-poole

Post on 21-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

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?

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?

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?

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?

“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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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