Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 20

CSE205 Lecture 20

Object-Oriented Programming and Data Structures
Collections
(202204)

Javier Gonzalez-Sanchez PRO

September 30, 2021

Transcript

26 slides
  1. 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
  2. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3 jgs

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

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

    I introduce you a List, a Linked List
  5. 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.
  6. 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 …
  7. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10 jgs

    The interface List § List <data-type> list1= new LinkedList(); § List <data-type> list4 = new Stack(); § LinkedList <data-type> list2 = new LinkedList(); § Stack <data-type> list4 = new Stack();
  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11 jgs

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

    Stack import java.util.*; public class TestJavaCollection4{ public static void main(String args[]){ Stack<String> stack = new Stack<String>(); 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()); } }
  10. 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<String> queue=new PriorityQueue<String>(); 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:"); }
  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs

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

    I introduce you a List, a Linked List
  13. 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
  14. 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; } }
  15. 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); } } }
  16. 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(); } }
  17. 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); } }
  18. 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming