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
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  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

    View Slide

  2. jgs
    Previously

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. jgs
    Collections

    View Slide

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

    View Slide

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

    View Slide

  9. 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 …

    View Slide

  10. 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();

    View Slide

  11. 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");
    }
    }

    View Slide

  12. 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());
    }
    }

    View Slide

  13. 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:");
    }

    View Slide

  14. jgs
    Test Yourselves

    View Slide

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

    View Slide

  16. jgs
    Nuts and bolts of Binary Trees
    Nodes

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  21. 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;
    }
    }

    View Slide

  22. 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);
    }
    }
    }

    View Slide

  23. 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();
    }
    }

    View Slide

  24. 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);
    }
    }

    View Slide

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

    View Slide

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

    View Slide