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

CSE205 Lecture 21

CSE205 Lecture 21

Object-Oriented Programming and Data Structures
Connecting the Dots
(202111)

Javier Gonzalez-Sanchez

October 01, 2021
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    View full-size slide

  2. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 2
    jgs
    Quiz 05
    § Open Today.
    § You have a week to work
    § A 30 minutes Quiz about data structures (including Trees)

    View full-size slide

  3. jgs
    Nuts and bolts of Binary Trees
    Nodes

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 9
    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 full-size slide

  10. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10
    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 full-size slide

  11. jgs
    Nuts and bolts of Binary Trees
    Traverse

    View full-size slide

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

    View full-size slide

  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13
    jgs
    Tree | Traverse In Order

    View full-size slide

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

    View full-size slide

  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15
    jgs
    Tree | Traverse Pre Order

    View full-size slide

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

    View full-size slide

  17. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17
    jgs
    Tree | Traverse Post Order

    View full-size slide

  18. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 30
    jgs
    Questions

    View full-size slide

  19. 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 full-size slide