Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Jeff Edmonds York University COSC 2011 Lecture 2 Abstract Positions/Pointers Positions in an Array Pointers in C References in Java Implementing Positions."— Presentation transcript:

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 in Trees Building Trees Positions and Pointers

2 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

3 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: girl 2 = tree.child(girl 2,1); girl 2 = tree.nextSibling(girl 2 ); girl 1 = tree.root();

4 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 girl 2 or in an array A[4]. n 012 A 5 4 3 High Level Positions/Pointers

5 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 girl 2 or in an array A[4]. A position can also be stored in a data element “pointing” at another data element. n High Level Positions/Pointers Each element contains two fields First storing info Second storing the position of the next element

6 6 High Level Positions/Pointers

7 7 A data structure is for organizing your data. An abstraction: does not worry about implementation details simplicity intuitive understandability user friendly An implementation must worry about details to make it work. Implementations of Positions/PointersHigh Level Positions/Pointers

8 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 12 A 5 4 3

9 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 12 A 5 4 3 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

10 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 12 A 5 4 3 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

11 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 Node No memory is allocated Here E is some specified type. Keep track of types! Lets do it in C first (Sorry it is better).

12 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). head element next Keep track of types!

13 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!

14 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). head malloc( sizeof(Node) ); Keep track of types! 2039

15 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). head malloc( sizeof(Node) ); Keep track of types! 2039.element 5 = 5;

16 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 structure This 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). head malloc( sizeof(Node) ); Keep track of types! 2039.element 5 = 5; malloc( sizeof(Node) ); element next 2182

17 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! 2039 5 element next 2182 a b

18 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! 2039 5 element next 2182 head.next (* ).next head.next; (* ) = 2182

19 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 2039 5 element next 2182 head.next (* ).next head.next; (* ) = 2182 Simpler C notation available. head→next→next = head→next; head.next head.next; = Corresponding Notation in Java

20 20 Positions/Pointers: Implementations of Positions/Pointers element next 2039 5 element next 2182 head.next head.next; = 2182 Note we skipped talking about the object. Corresponding Notation in Java head →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

21 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! 2039 5 element next 2182 head.next (* ).next head.next; (* ) = 2182 C does a lot of other great things with pointers but lets move on. head →next head→next; = Simpler C notation available.

22 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.

23 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 element next

24 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 next 2182

25 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. head element next

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

27 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. head new Node(5); Keep track of types! 2039 5

28 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 5

29 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 This constructer gets executed if called with no inputs; Default values are zero. (); (5); 5

30 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 5 element next 2182 6

31 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 5 element next 2182 head.next head.next; = 2182 Note we skipped talking about the object.

32 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

33 33 Positions: Given a data structure, we want to have one or more current elements that we are considering. Moving Them: girl 2 = tree.child(girl 2,1); girl 2 = tree.nextSibling(girl 2 ); girl 1 = tree.root(); Implementing Positions in Trees

34 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();

35 35 Implementing Positions in Trees class 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

36 36 Implementing Positions in Trees class 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

37 37 Implementing Positions in Trees class LinkedBinaryTree { class Node { E element; Node Node Node } public p2p2 p1p1 p3p3 tree The user can have many of its own fingers pointing at nodes Node p 1, p 2, p 3 ; 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;

38 38 Implementing Positions in Trees class LinkedBinaryTree { class Node implements Position{ E element; Node parent; Node left; Node right; } p2p2 p1p1 p3p3 public tree The user can have many of its own fingers pointing at nodes Node p 1, p 2, p 3 ; Remember that variable p i is not a Node, but is a reference/pointer/position to a Node. We abstracted this as a Position. private Position

39 39 Implementing Positions in Trees class LinkedBinaryTree { Position root() { return root; } At any time the user can get the position of the tree’s root. p 1 = tree.root(); … This is a method within the tree that is public to the user and returns a value of type Positions. p2p2 p1p1 p3p3 This is the tree’s private variable root. public tree

40 40 Implementing Positions in Trees class LinkedBinaryTree { Position right(Position p) { return p.right; } At any time the user can move a position to the right child. p 2 = tree.right(p 1 ); … This is a method takes a Position p as input and returns a Positions, namely p’s right child. p2p2 p1p1 p3p3 This is the right field of the node pointed to by p. tree

41 41 Implementing Positions in Trees class LinkedBinaryTree { Position right(Position p) { return p.right; } At any time the user can move a position to the right child. p 2 = tree.right(p 1 ); This address gets copied to p 2. … p.right contains the memory address of it’s right child. p2p2 p1p1 p3p3 2039 tree

42 42 Implementing Positions in Trees class 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. … p2p2 p1p1 p3p3 2039 tree Node n= p; return n.right;

43 43 Implementing Positions in Trees class 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. p 3 = tree.sibling(p 2 ); p2p2 p1p1 p3p3 … tree Node n=p; if( n.parent != null ) else throw new IllegalArgumentException(“p is the root"); }

44 44 Implementing Positions in Trees class LinkedBinaryTree { E getElement(Position p) { return p.element; } At any time the user can access the element of type E stored at a position p 2. (Here Seattle) E e = p 2.element; Jeff likes this, but the Java police might want to hide things. E e = tree.getElement(p 2 ); … p2p2 p1p1 p3p3 tree

45 45 Implementing Positions in Trees class 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 p 2. (Here Seattle) E e = p 2.element; But tree does not have to be involved. Put the method in the class Node. E e = p 2.getElement(); … p2p2 p1p1 p3p3 tree

46 46 Implementing Positions in Trees class 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. p 3 = tree.addRight(p 2,“Toronto”); … p2p2 p1p1 p3p3 Toronto new Node(e,,null,null); n n.right; tree Node n=p;

47 47 Implementing Positions in Trees class LinkedBinaryTree { This tree is built with: LinkedBinaryTree tree = new LinkedBinaryTree(); p 1 = tree.addRoot(“Providence”); p 2 = tree.addLeft(p 1,“Chicago”); p 3 = tree.addLeft(p 2,“Baltimore”); p 3 = tree.addRight(p 2,“NewYork”); p 2 = tree.addRight(p 1,“Seatle”); p 3 = tree.addRight(p 2,“Toronto”); … Toronto p2p2 p1p1 p3p3 tree

48 48 Vancover Montreal Ottawa tree 2 Implementing Positions in Trees At any time the user can attach it to the left of a position. tree.attachLeft(p 3,tree 2 ); (and returns nothing) … p2p2 p1p1 p3p3 Toronto tree Suppose we have a second tree object. class LinkedBinaryTree { attachLeft(Position p, LinkedBinaryTree t 2 ) { n.left = t 2.root; t 2.root.parent = n; void Node n=p;

49 49 Vancover Montreal Ottawa tree 2 Implementing Positions in Trees Note tree and tree 2 now share nodes. This is not a problem if this is what you want. Or we could make physical copies of the nodes in tree 2 to put into tree. Or we could disconnect tree 2. … p2p2 p1p1 p3p3 Toronto tree class LinkedBinaryTree { attachLeft(Position p, LinkedBinaryTree t 2 ) { n.left = t 2.root; t 2.root.parent = n; t 2.root = null; t 2.size = 0; void Node n=p;

50 50 Vancover Montreal Ottawa tree 2 Implementing Positions in Trees … p2p2 p1p1 p3p3 Toronto tree class LinkedBinaryTree { attachLeft(Position p, LinkedBinaryTree t 2 ) { if( n.left = null ) n.left = t 2.root; t 2.root.parent = n; t 2.root = null; t 2.size = 0; else throw new IllegalArgumentException( "p already has a left child"); } void Node n=p;

51 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

52 52 End Positions and Pointers Start doing the assignment NOW!


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

Similar presentations


Ads by Google