Slide 1

Slide 1 text

jgs CSE 205 Object-Oriented Programming and Data Structures Lecture 20: Collections Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Previously

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3 jgs I introduce you a List, a Linked List

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs I introduce you a List, a Linked List

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5 jgs I introduce you a List, a Linked List

Slide 6

Slide 6 text

jgs Collections

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7 jgs Definition § Java Collections is a framework that provides an architecture to store and manipulate the group of objects. § Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8 jgs Hierarchy

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9 jgs Common methods § public boolean add(E e) § public boolean remove(E e) § public int size() § public void clear() § public boolean contains(Object element) § public boolean isEmpty() § And more …

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs The interface List § List list1= new LinkedList(); § List list4 = new Stack(); § LinkedList list2 = new LinkedList(); § Stack list4 = new Stack();

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs LinkedList import java.util.*; public class TestJavaCollection2{ public static void main(String a []){ LinkedList al=new LinkedList(); al.add(”Mary"); al.add(”John"); al.add(”Tom"); al.add(”Susan"); } }

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12 jgs Stack import java.util.*; public class TestJavaCollection4{ public static void main(String args[]){ Stack stack = new Stack(); stack.push(”Sun"); stack.push(”Earth"); stack.push(”Pluto"); stack.push(”Saturn"); System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.pop()); } }

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs PriorityQueue // PriorityQueue doesn't allow null values to be stored in the queue import java.util.*; public class TestJavaCollection5{ public static void main(String args[]){ PriorityQueue queue=new PriorityQueue(); queue.add(”One"); queue.add(”Two"); queue.add(”Three"); queue.add(”Four"); // Retrieves, but does not remove, the head of this queue System.out.println("head:"+queue.element()); System.out.println("head:"+queue.peek()); // Retrieves and removes the head of this queue queue.remove(); queue.poll(); System.out.println("after removing two elements:"); }

Slide 14

Slide 14 text

jgs Test Yourselves

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs What about the classes § Student § Section § Course § AcademicProgram

Slide 16

Slide 16 text

jgs Nuts and bolts of Binary Trees Nodes

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17 jgs What is the problem with arrays?

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 18 jgs I introduce you a List, a Linked List

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19 jgs What about this?

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20 jgs Binary Tree § A non-linear data structure § where each node can have 2 children at most. § keep the tree sorted

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21 jgs Node class Node { public int value; public Node left public Node right; Node(int value){ value = value; left = null; right = null; } }

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 22 jgs Tree | insert public class Tree { public void insert(Node node, int value) { if (value < node.value) { if (node.left != null) { insert(node.left, value); } else { System.out.println(" Inserted to left"); node.left = new Node(value); } } else if (value > node.value) { if (node.right != null) { insert(node.right, value); } else { System.out.println(" Inserted to right "); node.right = new Node(value); } } }

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 23 jgs Tree | Example public static void main(String args[]) { Tree tree = new Tree(); Node root = new Node(5); tree.insert(root, 2); tree.insert(root, 4); tree.insert(root, 8); tree.insert(root, 6); tree.insert(root, 7); tree.insert(root, 3); tree.insert(root, 9); tree.traverseLevelOrder(); } }

Slide 24

Slide 24 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 24 jgs Tree | Traverse public void traverseInOrder(Node node) { if (node != null) { traverseInOrder(node.left); System.out.print(" " + node.value); traverseInOrder(node.right); } }

Slide 25

Slide 25 text

Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 25 jgs Questions

Slide 26

Slide 26 text

jgs CSE 205 Object-Oriented Programming and Data Structures Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University. They cannot be distributed or used for another purpose.