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

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Data Structure Lecture-5
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Written by: Dr. JJ Shepherd
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Chapter 61 Chapter 6 Index Structures for Files. Chapter 62 Indexes Indexes are additional auxiliary access structures with typically provide either faster.
Memory Management CS Chakrabarti Variable storage thus far  Never used global variables  All variables allocated inside functions, passed.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
AITI Lecture 20 Trees, Binary Search Trees Adapted from MIT Course 1.00 Spring 2003 Lecture 28 and Tutorial Note 10 (Teachers: Please do not erase the.
Information and Computer Sciences University of Hawaii, Manoa
CSE AU B-Trees1 B-Trees CSE 373 Data Structures.
B-Trees. CSM B-Trees 2 Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
B-Trees. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Lecture1 introductions and Tree Data Structures 11/12/20151.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Binary Trees. A Binary tree is a tree in which every node has none, one, or at most, two children A Proper Binary Tree has two children or no children.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Written by: Dr. JJ Shepherd
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Windows Programming Lecture 03. Pointers and Arrays.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Memory Management.
Java Yingcai Xiao.
Linked List :: Basic Concepts
Jeff Edmonds York University
CMSC 341 Lecture 13 Leftist Heaps
Pointers and References
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Chapter 20: Binary Trees.
Pointers and References
Chapter 21: Binary Trees.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Chapter 8: Data Abstractions
Pointers and References
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
Presentation transcript:

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 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 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 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 High Level Positions/Pointers

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 High Level Positions/Pointers

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

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

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 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 Positions/Pointers: Implementations of Positions/Pointers element next 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 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! 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 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 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 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 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 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 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!

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

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

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

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

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 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 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 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 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 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 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 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 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 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 p3p tree

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 p3p tree Node n= p; return n.right;

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 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 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 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 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 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 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 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 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 End Positions and Pointers Start doing the assignment NOW!