52
1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions in Trees Building Trees Positions and Pointers

1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

Embed Size (px)

Citation preview

Page 1: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

1

Jeff Edmonds

York University COSC 2011Lecture 2

Abstract Positions/PointersPositions in an ArrayPointers in CReferences in JavaImplementing Positions in TreesBuilding Trees

Positions and Pointers

Page 2: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

2

High Level Positions/Pointers

Positions: Given a data structure, we want to have one or more current elements that we are considering.Conceptualizations: • Fingers in pies• Pins on maps• Little girl dancing there• Me

See Goodrich Sec 7.3 Positional Lists

Page 3: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

3

High Level Positions/Pointers

Positions: Given a data structure, we want to have one or more current elements that we are considering.Moving Them: • girl2 = tree.child(girl2,1);

• girl2 = tree.nextSibling(girl2);

• girl2 = tree.nextSibling(girl2);

• girl1 = tree.root();

Page 4: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

4

Positions: Given a data structure, we want to have one or more current elements that we are considering.Storing Them: • A position can be stored in a variable girl2

• or in an array A[4].

0 1 2

A

543

High Level Positions/Pointers

Page 5: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

5

Positions: Given a data structure, we want to have one or more current elements that we are considering.Storing Them: • A position can be stored in a variable girl2

• or in an array A[4].• A position can also be stored in a data element

“pointing” at another data element.

High Level Positions/Pointers

Each element contains two fields• First storing info• Second storing the position of the next element

Page 6: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

6

High Level Positions/Pointers

Page 7: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

7

A data structure is for organizing your data.An abstraction:• does not worry about implementation details• simplicity• intuitive understandability• user friendlyAn implementation• must worry about details to make it work.

Implementations of Positions/PointersHigh Level Positions/Pointers

Page 8: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

8

Array Implantation of Positions:Suppose the data elements are entries in an array A,then the position of an element could be its index.

Implementations of Positions/Pointers

0 1 2

A

543

Page 9: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

9

Array Implantation of Positions:Suppose the data elements are entries in an array A,then the position of an element could be its index.

Implementations of Positions/Pointers

0 1 2

A

543

This entry is at position 2.

head

2

We can build a linked list.• head holds the position of the “first” element.• The first element holds the position of the second.• And so on.

5 3

Page 10: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

10

Array Implantation of Positions:Suppose the data elements are entries in an array A,then the position of an element could be its index.

Implementations of Positions/Pointers

0 1 2

A

543

This entry is at position 2.

One problem is if we shift the elements in order to insert or delete an element, then all the indexes are off

6

Page 11: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

11

Objects/Structure:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };

element next

In C this is called a structure (or record).It defines type called Node.It layouts a block of memory. It has two fields• element of type E used to hold the element’s information• next of type pointer to NodeNo memory is allocated

Here E is some specified type.

Keep track of types!

Lets do it in C first (Sorry it is better).

Page 12: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

12

Objects/Structure:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };Node head;

This allocates memory for variable head of type Node.Useful but we won’t do it. Can’t do it in Java!

Lets do it in C first (Sorry it is better).

headelement next

Keep track of types!

Page 13: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

13

Positions/Pointers

Implementations of Positions/Pointers

struct Node { E element; Node *next; };Node *head;

This allocates memory for variable head.• It holds a position or a pointer. • *head denotes what it is pointing at.• Node *head states that

what head is pointing at is of type Node. i.e. head of type pointer to Node.

Lets do it in C first (Sorry it is better).

head

Keep track of types!

Page 14: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

14

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };Node *head;head =

element next

This allocates enough memory for a Node structure, but without a variable name.malloc returns the node’s address (position) in physical memory.Now head contains the address of the node.We say that head “points” at the structure.

Lets do it in C first (Sorry it is better).

headmalloc( sizeof(Node) );

Keep track of types!

20392039

Page 15: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

15

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };Node *head;head =*head

element next

*head denotes structure that head is pointing at.This denotes the element field of that sturcture.This stores a 5 (of type E) in that element field.

Lets do it in C first (Sorry it is better).

headmalloc( sizeof(Node) );

Keep track of types!

20392039

.element

5

= 5;

Page 16: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

16

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };Node *head;head =*head *head.next =

element next

This allocates memory for a second structureThis gets *head.next to point at it.This is how we build a linked list.

Lets do it in C first (Sorry it is better).

headmalloc( sizeof(Node) );

Keep track of types!

20392039

.element

5

= 5;malloc( sizeof(Node) );

element next2182

2182

Page 17: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

17

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };

a = b;

element next

The right hand side of the “=” specifies a memory location.So does its left hand side.The action is to put the value contained in the first into the second.

Lets do it in C first (Sorry it is better).

head

Keep track of types!

20392039

5element next2182

2182

a b

21822182

Page 18: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

18

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };

element next

The right hand side of the “=” specifies a memory location.So does its left hand side.The action is to put the value contained in the first into the second.

Lets do it in C first (Sorry it is better).

head

Keep track of types!

20392039

5element next2182

2182

head .next(* )(* ).next

head .next;(* )=

2182

Page 19: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

19

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };

element next

Lets do it in C first (Sorry it is better).

head

20392039

5element next2182

2182

head .next(* )(* ).next

head .next;(* )=

2182

Simpler C notation available.

head→next→next = head→next;

head.next.next

head .next;=

Corresponding Notation in Java

Page 20: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

20

Positions/Pointers:

Implementations of Positions/Pointers

element next2039

5element next2182

2182

head.next.next

head .next;=

2182

Note we skipped talking about the object.

Corresponding Notation in Java

head→next→next

head →next;=

head .next(* )(* ).next

head .next;(* )=

struct Node { E element; Node *next; };

Simpler C notation available.

Lets do it in C first (Sorry it is better).

head

2039

Page 21: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

21

Positions/Pointers:

Implementations of Positions/Pointers

struct Node { E element; Node *next; };

element next

Lets do it in C first (Sorry it is better).

head

Keep track of types!

20392039

5element next2182

2182

head .next(* )(* ).next

head .next;(* )=

2182

C does a lot of other great things with pointers but lets move on.

head→next→next

head →next;=

Simpler C notation available.

Page 22: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

22

Objects/Structure:

Implementations of Positions/Pointers

class Node { E element; Node next; }

element next

In Java it is called a class, is instantiated by an object, and can include data and methods (actions).The semicolon after a class declaration is removed.

Keep track of types!

Now lets redo it in Java.

Sorry I have ignored words like private and static.

Page 23: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

23

Objects/Structure:

Implementations of Positions/Pointers

class Node { E element; Node next; }

element next

The key difference is that there is no *.To me, this means that a Node contains a node.

Keep track of types!

Now lets redo it in Java.

element next

elementnext

Page 24: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

24

Objects/Structure:

Implementations of Positions/Pointers

class Node { E element; Node next; }

element next

No surely they just mean that Node contains a reference/pointer to node.

Keep track of types!

Now lets redo it in Java.

element next2182

2182

Page 25: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

25

Objects/Structure:

Implementations of Positions/Pointers

class Node { E element; Node next; }Node head;

Keep track of types!

Now lets redo it in Java.

The variable head of NOT a node.

headelement next

Page 26: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

26

Positions/Pointers

Implementations of Positions/Pointers

class Node { E element; Node next; }Node head;

All Java variables are references/pointers

Now lets redo it in Java.

head

Keep track of types!

(except int i; char c; double d;)

Page 27: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

27

Positions/Pointers:

Implementations of Positions/Pointers

class Node { E element; Node next; }Node head;head =

element next

This allocates enough memory for a Node object, but without a variable name.new returns the node’s address (position) in physical memory.Now head contains the address of the node.We say that head “references” the object.

Now lets redo it in Java.

headnew Node(5);

Keep track of types!

20392039

5

Page 28: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

28

Positions/Pointers:

Implementations of Positions/Pointers

class Node { E element; Node next;

Node(int initial){ element = initial; } }Node head;head =

element next

This is a constructor method for the class.It gets executed on the new Node object as it is constructed.

Now lets redo it in Java.

head

new Node(5);

2039

2039

5

Page 29: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

29

Positions/Pointers:

Implementations of Positions/Pointers

class Node { E element; Node next; Node(){} Node(int initial){ element = initial; } }Node head;head =

element next

This is a constructor method for the class.It gets executed on the new Node object as it is constructed.

Now lets redo it in Java.

new Node

2039

head

2039

0 0

This constructer gets executed if called with no inputs;Default values are zero.

();(5);

5

Page 30: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

30

Positions/Pointers:

Implementations of Positions/Pointers

Node head2 = head;head2.element = 5;head.next = new Node(6);

element next

This declares a second variable that is set to point at the same Node object.

Now lets redo it in Java.

2039

head2

2039

head

2039

5element next2182

2182 6

Page 31: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

31

head

2039

Positions/Pointers:

Implementations of Positions/Pointers

Now lets redo it in Java.

element next

The right hand side of the “=” specifies a memory location.So does its left hand side.The action is to put the value contained in the first into the second.

2039

5element next2182

2182head.next.next

head .next;=2182

Note we skipped talking about the object.

Page 32: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

32

Implementing Positions in Trees

Positions: Given a data structure, we want to have one or more current elements that we are considering.Conceptualizations: • Fingers in pies• Pins on maps• Little girl dancing there• Me

Page 33: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

33

Positions: Given a data structure, we want to have one or more current elements that we are considering.Moving Them: • girl2 = tree.child(girl2,1);

• girl2 = tree.nextSibling(girl2);

• girl2 = tree.nextSibling(girl2);

• girl1 = tree.root();

Implementing Positions in Trees

Page 34: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

34

Implementing Positions in Trees

Defining the class binary trees.

A user will create a new one with LinkedBinaryTree tree The variable tree references this new object of type LinkedBinaryTree. Of course this object won’t start with any nodes.

See Goodrich Sec 8.2,8.3 Binary Trees.

class LinkedBinaryTree {tree

= new LinkedBinaryTree();

Page 35: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

35

Implementing Positions in Treesclass LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; }

A tree contains many nodes.Hence, needs a node class with fields for the element and pointers.No memory is allocated

tree

Page 36: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

36

Implementing Positions in Treesclass LinkedBinaryTree { class Node { E element; Node parent; Node left; Node right; } Node root = null;

Each tree object will have its own such variable root that is private to it.

private

tree

Page 37: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

37

Implementing Positions in Treesclass LinkedBinaryTree { class Node { E element; Node Node Node }

publicp2

p1

p3

tree

The user can have many of its own fingers pointing at nodes Node p1, p2, p3;Type Node would need to be public, so the user can have such variables.But does the user really need access to left & right? This is LinkedBinaryTree’s job.

private

parent; left;right;

Page 38: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

38

Implementing Positions in Treesclass LinkedBinaryTree { class Node implements Position{ E element; Node parent; Node left; Node right; }

p2

p1

p3

public tree

The user can have many of its own fingers pointing at nodes Node p1, p2, p3;Remember that variable pi is not a Node, but is a reference/pointer/position to a Node.We abstracted this as a Position.

private

Position

Page 39: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

39

Implementing Positions in Treesclass LinkedBinaryTree { Position root() { return root; }

At any time the user can get the position of the tree’s root. p1 = tree.root();

This is a method within the tree that is public to the user and returns a value of type Positions.

p2

p1

p3

This is the tree’s private variable

root.

publictree

Page 40: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

40

Implementing Positions in Treesclass LinkedBinaryTree { Position right(Position p) { return p.right; }

At any time the user can move a position to the right child. p2 = tree.right(p1);

This is a method takes a Position p as input and returns a Positions, namely p’s right child.

p2

p1

p3

This is the rightfield of the nodepointed to by p.

tree

Page 41: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

41

Implementing Positions in Treesclass LinkedBinaryTree { Position right(Position p) { return p.right; }

At any time the user can move a position to the right child. p2 = tree.right(p1);This address gets copied to p2.

p.right contains the memory address of

it’s right child.

p2

p1

p32039

2039 2039tree

Page 42: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

42

Implementing Positions in Treesclass LinkedBinaryTree { Position right(Position p) { return p.right; }

Oops! If p is a Position, then it does not know about the right field.It must be cast as a Node.

p2

p1

p32039

2039 2039tree

Node n= p; return n.right;

Page 43: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

43

Implementing Positions in Treesclass LinkedBinaryTree { Position sibling(Position p) { if( n.parent.right = n ) return n.parent.left; else return n.parent.right; }

At any time the user can move a position to the sibling. p3 = tree.sibling(p2);

p2

p1

p3

…tree

Node n=p;

if( n.parent != null )

else throw new IllegalArgumentException(“p is the root"); }

Page 44: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

44

Implementing Positions in Treesclass LinkedBinaryTree { E getElement(Position p) { return p.element; }

At any time the user can access the element of type E stored at a position p2. (Here Seattle) E e = p2.element;Jeff likes this, but the Java police might want to hide things. E e = tree.getElement(p2);

p2

p1

p3

tree

Page 45: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

45

Implementing Positions in Treesclass LinkedBinaryTree { class Node implements Position{

E getElement() { return element; }

At any time the user can access the element of type E stored at a position p2. (Here Seattle) E e = p2.element;But tree does not have to be involved. Put the method in the class Node. E e = p2.getElement();

…p2

p1

p3

tree

Page 46: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

46

Implementing Positions in Treesclass LinkedBinaryTree { Position addRight(Position p,E e) { if( n.right = null ) n.right = return else throw new IllegalArgumentException(

"p already has a right child");

}

At any time the user can add a position/node to the right of a position. p3 = tree.addRight(p2,“Toronto”);

p2

p1

p3

Toronto

new Node(e, ,null,null);nn.right;

tree

Node n=p;

Page 47: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

47

Implementing Positions in Treesclass LinkedBinaryTree {

This tree is built with: LinkedBinaryTree tree = new LinkedBinaryTree(); p1 = tree.addRoot(“Providence”); p2 = tree.addLeft(p1,“Chicago”); p3 = tree.addLeft(p2,“Baltimore”); p3 = tree.addRight(p2,“NewYork”); p2 = tree.addRight(p1,“Seatle”); p3 = tree.addRight(p2,“Toronto”);

Toronto

p2

p1

p3

tree

Page 48: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

48

VancoverMontrealOttawa

tree2

Implementing Positions in Trees

At any time the user can attach it to the left of a position. tree.attachLeft(p3,tree2); (and returns nothing)

p2

p1

p3

Toronto

tree

Suppose we have a second tree object.

class LinkedBinaryTree { attachLeft(Position p, LinkedBinaryTree t2) { n.left = t2.root; t2.root.parent = n;

void

Node n=p;

Page 49: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

49

VancoverMontrealOttawa

tree2

Implementing Positions in Trees

Note tree and tree2 now share nodes.• This is not a problem

if this is what you want.• Or we could make physical copies of

the nodes in tree2 to put into tree.• Or we could disconnect tree2.

p2

p1

p3

Toronto

treeclass LinkedBinaryTree { attachLeft(Position p, LinkedBinaryTree t2) { n.left = t2.root; t2.root.parent = n; t2.root = null; t2.size = 0;

void

Node n=p;

Page 50: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

50

VancoverMontrealOttawa

tree2

Implementing Positions in Trees…

p2

p1

p3

Toronto

treeclass LinkedBinaryTree { attachLeft(Position p, LinkedBinaryTree t2) { if( n.left = null ) n.left = t2.root; t2.root.parent = n; t2.root = null; t2.size = 0; else throw new IllegalArgumentException(

"p already has a left child");

}

void

Node n=p;

Page 51: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

51

Implementing Positions in Trees

Defining the class of trees nodes can have many children.We use the data structure Set or List to store the Positions of a node’s children.

class LinkedTree {tree

Page 52: 1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions

52

End

Positions and Pointers

Start doing the assignment NOW!