CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.

Slides:



Advertisements
Similar presentations
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Advertisements

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
COMP5712 Tutorial 4. 2 Using an Array to Represent a Heap When a binary tree is complete – Can use level-order traversal to store data in consecutive.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
CST 230 Razdan et alhttp://dcst2.east.asu.edu/~razdan/cst230/ Razdan with contribution from others 1 Tree Traversals Pre-order traversal –process root.
Tree properties and traversals
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
CS 3610 Midterm Review.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Data Structures and Algorithms Lecture (BinaryTrees) Instructor: Quratulain.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CH 8. HEAPS AND PRIORITY QUEUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Programming Abstractions Cynthia Lee CS106X. Topics:  Binary Search Tree (BST) › Starting with a dream: binary search in a linked list? › How our dream.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Traversing a tree means visiting each node in a specified order. There are different ways to traverse a tree. Tree Traversing Depth first search Pre-orderIn-orderPost-order.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 CMSC 341 Introduction to Trees Textbook sections:
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
(c) University of Washington20-1 CSC 143 Java Trees.
12 C Data Structures.
Tree.
Section 8.1 Trees.
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 21: Binary Trees.
CSE 12 – Basic Data Structures
CSC 143 Java Trees.
Chapter 20: Binary Trees.
CO4301 – Advanced Games Development Week 4 Binary Search Trees
Presentation transcript:

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics 1. Heap implementation issues  Heap uniqueness  Heapsort in place  HW suggestions 2. Back to generic binary trees  In-order traversal  Pre-order traversal  Post-order traversal  Level-order traversal (also called “breadth-first”) 2

Reading quiz!

1. A node in a binary tree may have ___ children A. 0 B. 1 C. 2 D. 3 E. Other/none of the above/more than one of the above Reading quiz!

2. A preorder traversal visits the current node, performs a preorder traversal of its ___ subtree and then performs a preorder traversal of the its ___ subtree. A. right, right B. left, right C. left, left D. right, left Reading quiz!

3. A full binary tree is a tree in which every node other than the leaves has ____ children. A. 0 B. 1 C. 2 D. 3 E. Other/none of the above/more than one of the above Reading quiz!

Heap uniqueness

TRUE OR FALSE  There is only one configuration of a valid min-heap containing the elements {34, 22, 3, 9, 18} A. TRUE B. FALSE

Heap outcomes by insert order Create a MIN-heap by inserting the elements, one by one, in the order given below for the first letter of your last name :  A-F: {3, 9, 18, 22, 34}  G-L: {3, 33, 18, 9, 34}  M-R: {9, 22, 18, 3, 34}  S-Z: {18, 22, 3, 9, 34}

Heap outcomes by insert order Create a MIN-heap by inserting the elements, one by one, in the order given below for the first letter of your last name :  A-F: {18, 9, 34, 3, 22}  G-L: {3, 18, 22, 9, 34}  M-R: {22, 9, 34, 3, 18}  S-Z: {34, 3, 9, 18, 22}

How many distinct min-heaps are possible for the elements {3, 9, 18, 22, 34} ? A. 1 B. 2-4 C. 5-8 D. 5! (5 factorial) E. Other/none/more

Heapsort

Heapsort is super easy 1. Insert unsorted elements one at a time into a heap until all are added 2. Remove them from the heap one at a time. We will always be removing the next biggest[smallest] item from the max-heap[min-heap], so the items come out in sorted order! THAT’S IT!

Example: using heapsort to print an array in sorted order (note: this is not the in- place version you need for HW) public static void printSorted(String[] unsortedlist) { Heap heap = new Heap (); for (int i=0; i<unsortedlist.length; i++) { heap.add(unsortedlist[i]); } while (!heap.isEmpty()) { System.out.println(heap.remove()); }

Implementing heapsort Devil’s in the details

We can do the entire heapsort in place in one array  Unlike mergesort, we don’t need a separate array for our workspace  We can do it all in place in one array (the same array we were given as input)

Build heap by inserting elements one at a time:

Sort array by removing elements one at a time:

Build heap by inserting elements one at a time IN PLACE:

Sort array by removing elements one at a time IN PLACE:

Important tip!  The in-place approach will not work if your test to see if index i is past the end of the heap is to check if heaparray[i] is null.  Things will be in the array that are NOT in the heap!  You need to keep track of an int size (in addition to an int capacity ) in order to check where the heap ends!

Binary tree traversals

Binary trees  Recall from last time, a binary tree is any tree where each node has 0, 1, or 2 children  That’s the only restriction  Recall: heaps are a special case of binary trees, and they have two additional restrictions

Trees vs. Lists  Lists have an obvious ordering  1 st element is first, 2 nd element is second, …  Trees don’t  More than one reasonable order

Pre-order traversal preorder(node) { if (node != null){ visit this node preorder(node.left) preorder(node.right) } A. D B E A F C G B. A B D E C F G C. A B C D E F G D. D E B F G C A E. Other/none/more

Post-order traversal postorder(node) { if (node != null){ postorder(node.left) postorder(node.right) visit this node } A. D B E A F C G B. A B D E C F G C. A B C D E F G D. D E B F G C A E. Other/none/more

In-order traversal inorder(node) { if (node != null){ inorder(node.left) visit this node inorder(node.right) } A. D B E A F C G B. A B D E C F G C. A B C D E F G D. D E B F G C A E. Other/none/more