260

# CSE205 Lecture 21

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

October 01, 2021

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

4. ### Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4 jgs

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

I introduce you a List, a Linked List

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

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); } }
13. ### Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13 jgs

Tree | Traverse In Order
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); } }
15. ### Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15 jgs

Tree | Traverse Pre Order
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); } }
17. ### Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17 jgs

Tree | Traverse Post Order

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