Problem of the Day Can you move exactly one glass to arrange the glasses containing orange juice to...

Preview:

Citation preview

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!

LECTURE 36:TREE IMPLEMENTATION

CSC 212 – Data Structures

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

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

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

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

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

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)

Tree Interface

Often have other methods beyond these in class

Tree Interface

Often have other methods beyond these in class For example, want methods to add or

remove data

Tree Interface

Often have other methods beyond these in class For example, want methods to add or

remove data

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

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); }

}

Tree

D

Visualization of Tree

B

DA

C E

F

B

A F

C E

Tree

root

size6

D

Node Structure of the Tree

B

DA

C E

F

B

A F

C E

D

Links Between Tree's Nodes

B

DA

C E

F

B

A F

C E

D

Links Between Tree's Nodes

B

DA

C E

F

B

A F

C E

Tree

D

Actual View of Tree

B

DA

C E

F

B

A F

C E

Tree

root

size6

View of an Actual Tree

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;}

}

BinaryTree

Picturing Linked BinaryTree

B

CA

D

B

A C

D

BinaryTree

root

size4

Nodes in Linked BinaryTree

B

CA

D

B

A C

D

Pointers in LinkedBinaryTree

B

CA

D

B

A C

D

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

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

0 1 2 3 4 5 6 7 8 9 10

Array-based Impl.

A

HG

FE

D

C

B

J

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

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

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

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

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

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

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; }

}

Your Turn

Get into your groups and complete activity

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

Recommended