Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II
|
|
- Thomas Lawson
- 5 years ago
- Views:
Transcription
1 05 A: Trees II CS1102S: Data Structures and Algorithms Martin Henz February 10, 2010 Generated on Wednesday 10 th February, 2010, 10:54 CS1102S: Data Structures and Algorithms 05 A: Trees II 1
2 1 Review: Trees 2 3 CS1102S: Data Structures and Algorithms 05 A: Trees II 2
3 1 Review: Trees 2 3 CS1102S: Data Structures and Algorithms 05 A: Trees II 3
4 Trees in computer science Trees are ubiquitous in CS, covering operating systems, computer graphics, data bases, etc. CS1102S: Data Structures and Algorithms 05 A: Trees II 4
5 Trees in computer science Trees are ubiquitous in CS, covering operating systems, computer graphics, data bases, etc. Trees as data structures Provide O(log N) search operations CS1102S: Data Structures and Algorithms 05 A: Trees II 5
6 Trees in computer science Trees are ubiquitous in CS, covering operating systems, computer graphics, data bases, etc. Trees as data structures Provide O(log N) search operations Heaps Serve as basis for other efficient data structures, such as heaps CS1102S: Data Structures and Algorithms 05 A: Trees II 6
7 Example CS1102S: Data Structures and Algorithms 05 A: Trees II 7
8 Binary Trees Definition A binary tree is a tree in which no node can have more than two children. CS1102S: Data Structures and Algorithms 05 A: Trees II 8
9 Implementation ClassBinaryNode { / / accessible by other package r o u t i n e s Object element ; / / The data i n the node BinaryNode l e f t ; / / L e f t c h i l d BinaryNode r i g h t ; / / Right c h i l d } CS1102S: Data Structures and Algorithms 05 A: Trees II 9
10 1 Review: Trees 2 3 CS1102S: Data Structures and Algorithms 05 A: Trees II 10
11 Review: Trees Setup We would like to quickly find out if a given data item is included in a collection. CS1102S: Data Structures and Algorithms 05 A: Trees II 11
12 Review: Trees Setup We would like to quickly find out if a given data item is included in a collection. Example In an underground carpark, a system captures the licence plate numbers of incoming and outgoing cars. CS1102S: Data Structures and Algorithms 05 A: Trees II 12
13 Review: Trees Setup We would like to quickly find out if a given data item is included in a collection. Example In an underground carpark, a system captures the licence plate numbers of incoming and outgoing cars. Problem: Find out if a particular car is in the carpark. CS1102S: Data Structures and Algorithms 05 A: Trees II 13
14 Operations for Sets interface Set<T> { public void add ( T x ) ; / / same as i n s e r t ( T x ) ; } public void remove ( T x ) ; public boolean contains ( T x ) ;... CS1102S: Data Structures and Algorithms 05 A: Trees II 14
15 How About Lists, Arrays, Stacks, Queues? Problem with Lists, Arrays, Stacks, Queues With lists, arrays, stacks and queues, we can only access the collection using an index or in a LIFO/FIFO manner. CS1102S: Data Structures and Algorithms 05 A: Trees II 15
16 How About Lists, Arrays, Stacks, Queues? Problem with Lists, Arrays, Stacks, Queues With lists, arrays, stacks and queues, we can only access the collection using an index or in a LIFO/FIFO manner. Therefore, search takes linear time. CS1102S: Data Structures and Algorithms 05 A: Trees II 16
17 How About Lists, Arrays, Stacks, Queues? Problem with Lists, Arrays, Stacks, Queues With lists, arrays, stacks and queues, we can only access the collection using an index or in a LIFO/FIFO manner. Therefore, search takes linear time. How to avoid linear access? For efficient data structures, we often exploit properties of data items. CS1102S: Data Structures and Algorithms 05 A: Trees II 17
18 Example Review: Trees Simple license plates Let us say the license plate numbers are positive integers from 0 to CS1102S: Data Structures and Algorithms 05 A: Trees II 18
19 Example Review: Trees Simple license plates Let us say the license plate numbers are positive integers from 0 to Solution Keep an array incarpark of boolean values (initially all false). CS1102S: Data Structures and Algorithms 05 A: Trees II 19
20 Example Review: Trees Simple license plates Let us say the license plate numbers are positive integers from 0 to Solution Keep an array incarpark of boolean values (initially all false). insert ( i ) sets incarpark[i] to true CS1102S: Data Structures and Algorithms 05 A: Trees II 20
21 Example Review: Trees Simple license plates Let us say the license plate numbers are positive integers from 0 to Solution Keep an array incarpark of boolean values (initially all false). insert ( i ) sets incarpark[i] to true remove(i) sets incarpark[i] to false CS1102S: Data Structures and Algorithms 05 A: Trees II 21
22 Example Review: Trees Simple license plates Let us say the license plate numbers are positive integers from 0 to Solution Keep an array incarpark of boolean values (initially all false). insert ( i ) sets incarpark[i] to true remove(i) sets incarpark[i] to false contains( i ) returns incarpark[i]. CS1102S: Data Structures and Algorithms 05 A: Trees II 22
23 The Sad Truth Review: Trees Not all data items are small integers! In Singapore, license plate numbers start with 2 3 letters, followed by a number, followed by another letter. CS1102S: Data Structures and Algorithms 05 A: Trees II 23
24 The Sad Truth Review: Trees Not all data items are small integers! In Singapore, license plate numbers start with 2 3 letters, followed by a number, followed by another letter. But: one property remains We can compare two license plate numbers, for example lexicographically. CS1102S: Data Structures and Algorithms 05 A: Trees II 24
25 Lexicographic Ordering on License Plate Numbers First compare the first letters as in a dictionary e.g. SBX... < SCY..., SA... < SAB... CS1102S: Data Structures and Algorithms 05 A: Trees II 25
26 Lexicographic Ordering on License Plate Numbers First compare the first letters as in a dictionary e.g. SBX... < SCY..., SA... < SAB... If the letters are the same, use the following number e.g. SBX 100 < SBX 101 CS1102S: Data Structures and Algorithms 05 A: Trees II 26
27 Lexicographic Ordering on License Plate Numbers First compare the first letters as in a dictionary e.g. SBX... < SCY..., SA... < SAB... If the letters are the same, use the following number e.g. SBX 100 < SBX 101 If the letters and numbers are the same, use the final letter e.g. SBX 101 P < SBX 101 Q CS1102S: Data Structures and Algorithms 05 A: Trees II 27
28 The Comparable Interface API Interface Comparable interface Comparable<T> { public i n t compareto ( T o ) ; } CS1102S: Data Structures and Algorithms 05 A: Trees II 28
29 Mathematics of Comparable Ordering Instances of the Comparable interface are subject to a total ordering. For any two elements x and y, we know whether: x smaller then y: x.compareto(y) returns negative int x smaller then y: x.compareto(y) returns positive int x equals y: x.compareto(y) returns 0 CS1102S: Data Structures and Algorithms 05 A: Trees II 29
30 Type variables allow the programmer to refer to a type at multiple places. Example public s t a t i c <Any> SchemeList<Any> c o n c a t A l l ( SchemeList<SchemeList<Any>> a L i s t L i s t ) {... } CS1102S: Data Structures and Algorithms 05 A: Trees II 30
31 Wildcard Types Sometimes, a generic type is completely unrestricted. We use? without having to declare it. Example public s t a t i c i n t i t e r a t i v e L e n g t h ( SchemeList<?> a L i s t ) { i n t acc = 0; while (! a L i s t. i s N i l ( ) ) { a L i s t = a L i s t. cdr ( ) ; acc ++; } return acc ; } CS1102S: Data Structures and Algorithms 05 A: Trees II 31
32 Upper bounds for types Sometimes, a type variable must be bounded to restrict the types that it stands for to a class and all its sub-classes. Example interface C o l l e c t i o n <E> {... boolean add (E e ) ; boolean addall ( C o l l e c t i o n <? extends E> c ) ;... } CS1102S: Data Structures and Algorithms 05 A: Trees II 32
33 interface Comparable interface Comparable<T> { public i n t compareto ( T o ) ; } Invariance of generic types If Lion is a subtype of Animal, then Cage<Lion> is not a subtype of Cage<Animal>. CS1102S: Data Structures and Algorithms 05 A: Trees II 33
34 Invariance of generic types If Lion is a subtype of Animal, then Cage<Lion> is not a subtype of Cage<Animal>. CS1102S: Data Structures and Algorithms 05 A: Trees II 34
35 Invariance of generic types If Lion is a subtype of Animal, then Cage<Lion> is not a subtype of Cage<Animal>. Invariance of Comparable Therefore, if Animal implements Comparable<Animal>, Lion does not necessarily implement Comparable<Lion>. CS1102S: Data Structures and Algorithms 05 A: Trees II 35
36 Invariance of generic types If Lion is a subtype of Animal, then Cage<Lion> is not a subtype of Cage<Animal>. Invariance of Comparable Therefore, if Animal implements Comparable<Animal>, Lion does not necessarily implement Comparable<Lion>. Lower bounds for Comparable We want to allow Lion to implement Comparable<T> as long as T is a super type of Lion. CS1102S: Data Structures and Algorithms 05 A: Trees II 36
37 Lower bounds for Comparable We want to allow Lion to implement Comparable<T> as long as T is a super type of Lion. class BinarySearchTree <Any extends Comparable<? super Any>> {... } CS1102S: Data Structures and Algorithms 05 A: Trees II 37
38 Review: Trees Setup Keep items in a tree. Each node holds one data item. CS1102S: Data Structures and Algorithms 05 A: Trees II 38
39 Review: Trees Setup Keep items in a tree. Each node holds one data item. Idea The left subtree of a node V only contains items smaller than V and the right subtree only contains items larger than V. CS1102S: Data Structures and Algorithms 05 A: Trees II 39
40 Review: Trees Setup Keep items in a tree. Each node holds one data item. Idea The left subtree of a node V only contains items smaller than V and the right subtree only contains items larger than V. Search can then proceed top-down, starting at the root. If the search item is smaller than the item at the root, go down to the left, and if it is larger, go right. CS1102S: Data Structures and Algorithms 05 A: Trees II 40
41 Example Review: Trees Both trees are binary trees, but only the left tree is a search tree. CS1102S: Data Structures and Algorithms 05 A: Trees II 41
42 Implementation Review: Trees private s t a t i c class BinaryNode<AnyType>{ AnyType element ; BinaryNode<AnyType> l e f t ; BinaryNode<AnyType> r i g h t ; BinaryNode ( AnyType theelement ) { this ( theelement, null, null ) ; } BinaryNode ( AnyType theelement, BinaryNode<AnyType> l t, BinaryNode<AnyType> r t ) { element = theelement ; l e f t = l t ; r i g h t = r t ; } } CS1102S: Data Structures and Algorithms 05 A: Trees II 42
43 Implementation Review: Trees public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> { private s t a t i c class BinaryNode<AnyType> {.. } private BinaryNode<AnyType> r o o t ; public BinarySearchTree ( ) { r o o t = null ; } public void makeempty ( ) { r o o t = null ; } public boolean isempty ( ) { return r o o t == null ; }... } CS1102S: Data Structures and Algorithms 05 A: Trees II 43
44 Implementation Review: Trees public class BinarySearchTree<AnyType extends Comparable<? super AnyType>> {... public boolean contains ( AnyType x ) { return contains ( x, r o o t ) ; } public AnyType findmin ( ) { / / findmax s i m i l a r i f ( isempty ( ) ) throw new UnderflowException ( return findmin ( r o o t ). element ; } public void i n s e r t ( AnyType x ) { r o o t = i n s e r t ( x, r o o t ) ; } public void remove ( AnyType x ) { r o o t = remove ( x, r o o t ) ; } CS1102S:... Data Structures and Algorithms 05 A: Trees II 44
45 Implementation of Search private boolean contains ( AnyType x, BinaryNode<AnyType> t ) { i f ( t == null ) return false ; i n t compareresult = x. compareto ( t. element ) ; i f ( compareresult < 0) return contains ( x, t. l e f t ) ; else i f ( compareresult > 0 ) return contains ( x, t. r i g h t ) ; else return true ; } CS1102S: Data Structures and Algorithms 05 A: Trees II 45
46 Insertion Review: Trees Idea Proceed like in search. If item is found, do nothing. If not, insert it in the last visited position. Example CS1102S: Data Structures and Algorithms 05 A: Trees II 46
47 Deletion Review: Trees Idea Proceed like in search. If item is not found, do nothing. If item is found, take action depending on node. CS1102S: Data Structures and Algorithms 05 A: Trees II 47
48 Deletion Review: Trees Idea Proceed like in search. If item is not found, do nothing. If item is found, take action depending on node. Leaf If the node is leaf, delete it from parent. CS1102S: Data Structures and Algorithms 05 A: Trees II 48
49 Deletion Review: Trees Idea Proceed like in search. If item is not found, do nothing. If item is found, take action depending on node. Leaf If the node is leaf, delete it from parent. One child If the node has one child, move the child to parent. CS1102S: Data Structures and Algorithms 05 A: Trees II 49
50 Example: Deletion of Node with One Child One child If the node has one child, move the child to parent. CS1102S: Data Structures and Algorithms 05 A: Trees II 50
51 Example: Deletion of Node with One Child One child If the node has one child, move the child to parent. CS1102S: Data Structures and Algorithms 05 A: Trees II 51
52 Deletion of Node with Two Children Idea Replace data with data of smallest child on the right; then delete smallest child on the right. CS1102S: Data Structures and Algorithms 05 A: Trees II 52
53 Deletion of Node with Two Children Idea Replace data with data of smallest child on the right; then delete smallest child on the right. Example CS1102S: Data Structures and Algorithms 05 A: Trees II 53
54 Average-case Average Depth If all insertion sequences are equally likely, the average depth of any node is O(log N) (proof in Chapter 7) CS1102S: Data Structures and Algorithms 05 A: Trees II 54
55 Average-case Average Depth If all insertion sequences are equally likely, the average depth of any node is O(log N) (proof in Chapter 7) Deletion introduces imbalance Deletion favours right subtree, and therefore trees become left-heavy on the long run. CS1102S: Data Structures and Algorithms 05 A: Trees II 55
56 Average-case Randomly generated binary search tree CS1102S: Data Structures and Algorithms 05 A: Trees II 56
57 Average-case Search tree after N 2 insert/delete CS1102S: Data Structures and Algorithms 05 A: Trees II 57
58 1 Review: Trees 2 3 CS1102S: Data Structures and Algorithms 05 A: Trees II 58
59 Sets Idea A Set (interface) is a Collection (interface) that does not allow duplicate entries. Sorted Sets A SortedSet (interface) assumes that the data items are comparable (using a Comparator operation). interface SortedSet<E> extends Set<E> Implementation The most common implementation of SortedSet is TreeSet. CS1102S: Data Structures and Algorithms 05 A: Trees II 59
60 Next Week Friday: Midterm Monday Lab: Lab tasks (attendance taken) Wednesday: Hashing Thursday: Tutorial on midterm solutions Friday: Priority Queues CS1102S: Data Structures and Algorithms 05 A: Trees II 60
Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748
Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu Search Tree Structures Binary Tree Operations u Tree Traversals u Search O(n) calls to visit() Why? Every recursive has one
More informationCMSC 341. Binary Search Trees CMSC 341 BST
CMSC 341 Binary Search Trees CMSC 341 BST Announcements Homework #3 dues Thursday (10/5/2017) Exam #1 next Thursday (10/12/2017) CMSC 341 BST A Generic Tree CMSC 341 BST Binary Tree CMSC 341 BST The Binary
More informationCIS 121. Binary Search Trees
CIS 121 Binary Search Trees 1 Binary Search Tree n A Binary Search Tree is a Binary Tree in which, at every node v, the values stored in the left subtree of v are less than the value at v and the values
More informationCE 221 Data Structures and Algorithms
CE 2 Data Structures and Algorithms Chapter 6: Priority Queues (Binary Heaps) Text: Read Weiss, 6.1 6.3 Izmir University of Economics 1 A kind of queue Priority Queue (Heap) Dequeue gets element with the
More informationPriority Queues. 04/10/03 Lecture 22 1
Priority Queues It is a variant of queues Each item has an associated priority value. When inserting an item in the queue, the priority value is also provided for it. The data structure provides a method
More informationCMSC 341 Lecture 10 Binary Search Trees
CMSC 341 Lecture 10 Binary Search Trees John Park Based on slides from previous iterations of this course Review: Tree Traversals 2 Traversal Preorder, Inorder, Postorder H X M A K B E N Y L G W UMBC CMSC
More informationData Structures in Java
Data Structures in Java Lecture 9: Binary Search Trees. 10/7/015 Daniel Bauer 1 Contents 1. Binary Search Trees. Implementing Maps with BSTs Map ADT A map is collection of (key, value) pairs. Keys are
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -
More information! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge
Trees & Heaps Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Fall 2018 Jill Seaman!1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every
More informationTree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.
Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except
More informationGeneric BST Interface
Generic BST Interface Here s a partial generic BST interface: public class BST
More informationData Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748
Data Structures Giri Narasimhan Office: ECS 254A Phone: x-3748 giri@cs.fiu.edu Motivation u Many applications where Items have associated priorities Job scheduling Long print jobs vs short ones; OS jobs
More information07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31
Recap: Sorting 07 B: Sorting II CS1102S: Data Structures and Algorithms Martin Henz March 5, 2010 Generated on Friday 5 th March, 2010, 08:31 CS1102S: Data Structures and Algorithms 07 B: Sorting II 1
More information08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58
08 A: Sorting III CS1102S: Data Structures and Algorithms Martin Henz March 10, 2010 Generated on Tuesday 9 th March, 2010, 09:58 CS1102S: Data Structures and Algorithms 08 A: Sorting III 1 1 Recap: Sorting
More informationFigure 18.4 A Unix directory. 02/10/04 Lecture 9 1
Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002 Addison Wesley Figure 18.4 A Unix directory 02/10/04 Lecture 9 1 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss 2002
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant
More informationITI Introduction to Computing II
ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Binary search tree (part I) Version of March 24, 2013 Abstract These lecture notes are meant
More information4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees
4. Trees 4.1 Preliminaries 4.2 Binary trees 4.3 Binary search trees 4.4 AVL trees 4.5 Splay trees 4.6 B-trees Malek Mouhoub, CS340 Fall 2002 1 4.1 Preliminaries A Root B C D E F G Height=3 Leaves H I J
More informationCOMP : Trees. COMP20012 Trees 219
COMP20012 3: Trees COMP20012 Trees 219 Trees Seen lots of examples. Parse Trees Decision Trees Search Trees Family Trees Hierarchical Structures Management Directories COMP20012 Trees 220 Trees have natural
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 12: Heaps and Priority Queues MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Heaps and Priority Queues 2 Priority Queues Heaps Priority Queue 3 QueueADT Objects are added and
More informationCSE 214 Computer Science II Heaps and Priority Queues
CSE 214 Computer Science II Heaps and Priority Queues Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Introduction
More informationCS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:
CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS: OVERVIEW: motivation naive tree search sorting for trees and binary trees new tree classes search insert delete 1. Motivation 1.1 Search Structure continuing
More informationprotected BinaryNode root; } 02/17/04 Lecture 11 1
Binary Search Trees // BinarySearchTree class // void insert( x ) --> Insert x // void remove( x ) --> Remove x // void removemin( ) --> Remove minimum item // Comparable find( x ) --> Return item that
More informationCSE 2123: Collections: Priority Queues. Jeremy Morris
CSE 2123: Collections: Priority Queues Jeremy Morris 1 Collections Priority Queue Recall: A queue is a specific type of collection Keeps elements in a particular order We ve seen two examples FIFO queues
More informationCS 350 : Data Structures Binary Search Trees
CS 350 : Data Structures Binary Search Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary
More informationBinary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1
Binary Node class BinaryNode public BinaryNode( ) this( null, null, null ); public BinaryNode( Object theelement,binarynode lt,binarynode rt); public static int size( BinaryNode t ); // size of subtree
More informationPriority Queues (Heaps)
Priority Queues (Heaps) 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the
More informationPriority Queues (Heaps)
Priority Queues (Heaps) October 11, 2016 CMPE 250 Priority Queues October 11, 2016 1 / 29 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full
More informationHeaps. Heaps. A heap is a complete binary tree.
A heap is a complete binary tree. 1 A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. A min-heap is defined
More informationPriority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments
Heaps 1 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted order. Often we collect a set of items and process the one with the current
More informationProgramming II (CS300)
1 Programming II (CS300) Chapter 10: Search and Heaps MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Search and Heaps 2 Linear Search Binary Search Introduction to trees Priority Queues Heaps Linear Search
More informationCS350: Data Structures Binary Search Trees
Binary Search Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary search tree is a binary tree that
More informationCMSC 341. Binary Heaps. Priority Queues
CMSC 341 Binary Heaps Priority Queues Priority Queues Priority: some property of an object that allows it to be prioritized with respect to other objects of the same type Min Priority Queue: homogeneous
More informationCMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011
CMPSCI 187: Programming With Data Structures Lecture #28: Binary Search Trees 21 November 2011 Binary Search Trees The Idea of a Binary Search Tree The BinarySearchTreeADT Interface The LinkedBinarySearchTree
More information3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.
3. Priority Queues 3. Priority Queues ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority. Malek Mouhoub, CS340 Winter 2007 1 3. Priority Queues
More informationThe smallest element is the first one removed. (You could also define a largest-first-out priority queue)
Priority Queues Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The smallest element is the first one removed (You could also define a largest-first-out
More informationDictionaries. Priority Queues
Red-Black-Trees.1 Dictionaries Sets and Multisets; Opers: (Ins., Del., Mem.) Sequential sorted or unsorted lists. Linked sorted or unsorted lists. Tries and Hash Tables. Binary Search Trees. Priority Queues
More informationBinary Trees, Binary Search Trees
Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)
More informationCollections, Maps and Generics
Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.
More information3137 Data Structures and Algorithms in C++
3137 Data Structures and Algorithms in C++ Lecture 4 July 17 2006 Shlomo Hershkop 1 Announcements please make sure to keep up with the course, it is sometimes fast paced for extra office hours, please
More informationvoid insert( Type const & ) void push_front( Type const & )
6.1 Binary Search Trees A binary search tree is a data structure that can be used for storing sorted data. We will begin by discussing an Abstract Sorted List or Sorted List ADT and then proceed to describe
More informationReadings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue
Readings Priority Queues & Binary Heaps Chapter Section.-. CSE Data Structures Winter 00 Binary Heaps FindMin Problem Quickly find the smallest (or highest priority) item in a set Applications: Operating
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationTREES cs2420 Introduction to Algorithms and Data Structures Spring 2015
TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due Thursday at midnight -asking for regrades through assignment 5 and midterm must be complete by
More informationCMSC 341 Lecture 15 Leftist Heaps
Based on slides from previous iterations of this course CMSC 341 Lecture 15 Leftist Heaps Prof. John Park Review of Heaps Min Binary Heap A min binary heap is a Complete binary tree Neither child is smaller
More informationLec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)
Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)
More informationRecursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, Recursion public static long fib(int n) if (n
More informationRecall: Properties of B-Trees
CSE 326 Lecture 10: B-Trees and Heaps It s lunch time what s cookin? B-Trees Insert/Delete Examples and Run Time Analysis Summary of Search Trees Introduction to Heaps and Priority Queues Covered in Chapters
More informationPriority queues. Priority queues. Priority queue operations
Priority queues March 8, 08 Priority queues The ADT priority queue stores arbitrary objects with priorities. An object with the highest priority gets served first. Objects with priorities are defined by
More informationBINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015
BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due tonight at midnight -asking for regrades through assignment 5 and midterm must
More informationExam Data structures DIT960/DAT036
Exam Data structures DIT960/DAT036 Time Monday 26th May 2014, 14:00 18:00 Place Hörsalsvägen Course responsible Nick Smallbone, tel. 0707 183062 The exam consists of six questions. Some of the questions
More informationTrees. (Trees) Data Structures and Programming Spring / 28
Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r
More informationAlgorithms and Data Structures
Algorithms and Data Structures Dr. Malek Mouhoub Department of Computer Science University of Regina Fall 2002 Malek Mouhoub, CS3620 Fall 2002 1 6. Priority Queues 6. Priority Queues ffl ADT Stack : LIFO.
More informationCS 315 Data Structures mid-term 2
CS 315 Data Structures mid-term 2 1) Shown below is an AVL tree T. Nov 14, 2012 Solutions to OPEN BOOK section. (a) Suggest a key whose insertion does not require any rotation. 18 (b) Suggest a key, if
More informationBinary Heaps in Dynamic Arrays
Yufei Tao ITEE University of Queensland We have already learned that the binary heap serves as an efficient implementation of a priority queue. Our previous discussion was based on pointers (for getting
More informationDescribe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods
Describe how to implement deque ADT using two stacks as the only instance variables. What are the running times of the methods 1 2 Given : Stack A, Stack B 3 // based on requirement b will be reverse of
More informationCMSC 341 Priority Queues & Heaps. Based on slides from previous iterations of this course
CMSC 341 Priority Queues & Heaps Based on slides from previous iterations of this course Today s Topics Priority Queues Abstract Data Type Implementations of Priority Queues: Lists BSTs Heaps Heaps Properties
More informationData Structures and Algorithms
Data Structures and Algorithms Spring 2017-2018 Outline 1 Priority Queues Outline Priority Queues 1 Priority Queues Jumping the Queue Priority Queues In normal queue, the mode of selection is first in,
More informationSome Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees
Some Search Structures Balanced Search Trees Lecture 8 CS Fall Sorted Arrays Advantages Search in O(log n) time (binary search) Disadvantages Need to know size in advance Insertion, deletion O(n) need
More informationPriority Queues and Binary Heaps. See Chapter 21 of the text, pages
Priority Queues and Binary Heaps See Chapter 21 of the text, pages 807-839. A priority queue is a queue-like data structure that assumes data is comparable in some way (or at least has some field on which
More informationCS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.
CS251-SE1 Midterm 2 Tuesday 11/1 8:00pm 9:00pm There are 16 multiple-choice questions and 6 essay questions. Answer the multiple choice questions on your bubble sheet. Answer the essay questions in the
More informationCMSC 341 Leftist Heaps
CMSC 341 Leftist Heaps Based on slides from previous iterations of this course Today s Topics Review of Min Heaps Introduction of Left-ist Heaps Merge Operation Heap Operations Review of Heaps Min Binary
More informationCS200 Midterm 2 Fall 2007
CS200 Midterm 2 Fall 2007 Name Topic Possible Received Generics, programming 10 Trees 35 Priority Queues, Heaps 30 Graphs 25 TOTAL 100 1. Generics/Programming [10 points] a. [4 points] Why is it important
More informationCISC 320 Midterm Exam
Name: CISC 320 Midterm Exam Wednesday, Mar 25, 2015 There are 19 questions. The first 15 questions count 4 points each. For the others, points are individually shown. The total is 100 points. Multiple
More informationCSC 321: Data Structures. Fall 2016
CSC 321: Data Structures Fall 2016 Balanced and other trees balanced BSTs: AVL trees, red-black trees TreeSet & TreeMap implementations heaps priority queue implementation heap sort 1 Balancing trees recall:
More informationCSC 321: Data Structures. Fall 2017
CSC 321: Data Structures Fall 2017 Balanced and other trees balanced BSTs: AVL trees, red-black trees TreeSet & TreeMap implementations heaps priority queue implementation heap sort 1 Balancing trees recall:
More informationOutline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications
Trees 1 Outline Preliminaries What is Tree? Implementation of Trees using C++ Tree traversals and applications Binary Trees Binary Search Trees Structure and operations Analysis 2 What is a Tree? A tree
More informationName CPTR246 Spring '17 (100 total points) Exam 3
Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in
More informationCSCI2100B Data Structures Heaps
CSCI2100B Data Structures Heaps Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction In some applications,
More informationWeek 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS
ASSIGNMENTS h0 available and due before 10pm on Monday 1/28 h1 available and due before 10pm on Monday 2/4 p1 available and due before 10pm on Thursday 2/7 Week 2 TA Lab Consulting - See schedule (cs400
More informationCS 315 Data Structures Spring 2012 Final examination Total Points: 80
CS 315 Data Structures Spring 2012 Final examination Total Points: 80 Name This is an open-book/open-notes exam. Write the answers in the space provided. Answer for a total of 80 points, including at least
More informationCMPSCI 187: Programming With Data Structures. Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012
CMPSCI 187: Programming With Data Structures Lecture #26: Binary Search Trees David Mix Barrington 9 November 2012 Binary Search Trees Why Binary Search Trees? Trees, Binary Trees and Vocabulary The BST
More informationPriority Queues Heaps Heapsort
Priority Queues Heaps Heapsort Complete the Doublets partner(s) evaluation by tonight. Use your individual log to give them useful feedback! Like 230 and have workstudy funding? We are looking for CSSE230
More informationBinary Heaps. CSE 373 Data Structures Lecture 11
Binary Heaps CSE Data Structures Lecture Readings and References Reading Sections.1-. //0 Binary Heaps - Lecture A New Problem Application: Find the smallest ( or highest priority) item quickly Operating
More informationPrelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer
Prelim 2 CS 2110, November 20, 2014, 7:30 PM 1 2 3 4 5 Extra Total Question True/False Short Answer Complexity Induction Trees Graphs Extra Credit Max 20 10 15 25 30 5 100 Score Grader The exam is closed
More information- Alan Perlis. Topic 24 Heaps
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program." - Alan Perlis Recall priority queue Priority Queue
More informationTrees. A tree is a directed graph with the property
2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees
More informationCSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills
CSCI 136 Data Structures & Advanced Programming Lecture 22 Fall 2018 Instructor: Bills Last Time Lab 7: Two Towers Array Representations of (Binary) Trees Application: Huffman Encoding 2 Today Improving
More informationPriority Queues and Huffman Trees
Priority Queues and Huffman Trees 1 the Heap storing the heap with a vector deleting from the heap 2 Binary Search Trees sorting integer numbers deleting from a binary search tree 3 Huffman Trees encoding
More informationCS102 Binary Search Trees
CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one
More informationSecond Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011
Second Exam SAMPLE Computer Programming 338 Dr. St. John Lehman College City University of New York Tuesday, 5 April 2011 Exam Rules Show all your work. Your grade will be based on the work shown. The
More informationFigure 1: A complete binary tree.
The Binary Heap A binary heap is a data structure that implements the abstract data type priority queue. That is, a binary heap stores a set of elements with a total order (that means that every two elements
More informationEXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
T E W H A R E W Ā N A N G A O T E Student ID:....................... Ū P O K O O T E I K A A M Ā U I VUW VICTORIA U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2015 TRIMESTER 2 COMP103 INTRODUCTION
More information13 A: External Algorithms II; Disjoint Sets; Java API Support
13 A: External Algorithms II; ; Java API Support Martin Henz April 15, 2009 Generated on Friday 13 17 A: th External April, 2009, Algorithms 12:37 II; ; Java API Support 1 1 External Sorting 2 3 4 13 A:
More informationDraw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).
Problem 1. Binary Search Trees (36 points) a) (12 points) Assume that the following numbers are inserted into an (initially empty) binary search tree in the order shown below (from left to right): 42 36
More informationBinary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017
Binary Search Trees Section.4 BinaryTree Class (cont.) public class BinaryTree { // Data members/fields...just the root is needed - Node root; // Constructor(s) + BinaryTree() + BinaryTree(Node
More informationExam Data structures DAT036/DAT037/DIT960
Exam Data structures DAT036/DAT037/DIT960 Time Thursday 18 th August 2016, 08:30 12:30 Place Maskinhuset / SB Multisal Course responsible Nick Smallbone, tel. 0707 183062 The exam consists of six questions.
More informationPriority Queues and Binary Heaps. See Chapter 21 of the text, pages
Priority Queues and Binary Heaps See Chapter 21 of the text, pages 807-839. A priority queue is a queue-like data structure that assumes data is comparable in some way (or at least has some field on which
More informationCS 315 April 1. Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin. algoritms for decrasekey increasekey Build-heap
CS 315 April 1 Goals: Heap (Chapter 6) continued review of Algorithms for Insert DeleteMin percolate-down algoritms for decrasekey increasekey Build-heap heap-sorting, machine scheduling application Binary
More informationTrees, Binary Trees, and Binary Search Trees
COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,
More informationCSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 12: Binary Heaps Instructor: Lilian de Greef Quarter: Summer 2017 Today Announcements Binary Heaps insert delete Array representation of tree Floyd s Method
More informationAdministration CSE 326: Data Structures
Administration SE : Data Structures Priority Queues and inary Heaps Due tonight: Project Released today: Project, phase A Due Wednesday: Homework Released Wednesday: Homework ary has office hours tomorrow
More informationLecture 27. Binary Search Trees. Binary Search Trees
Lecture Binary Search Trees Binary Search Trees In the previous lecture, we defined the concept of binary search tree as a binary tree of nodes containing an ordered key with the following additional property.
More informationINFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues
INFO1x05 Tutorial 6 Heaps and Priority Queues Exercise 1: 1. How long would it take to remove the log n smallest elements from a heap that contains n entries, using the operation? 2. Suppose you label
More informationext Total Score /20 /20 /15 /20 /25 /5 Grader
NAME: NETID: CS2110 Fall 2013 Prelim 2 November 21, 2013 Write your name and Cornell netid. There are 5 questions plus one extra-credit question on 10 numbered pages. Check now that you have all the pages.
More informationPriority Queues and Binary Heaps
Yufei Tao ITEE University of Queensland In this lecture, we will learn our first tree data structure called the binary heap which serves as an implementation of the priority queue. Priority Queue A priority
More informationCOSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)
COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back
More informationHEAPS & PRIORITY QUEUES
HEAPS & PRIORITY QUEUES Lecture 13 CS2110 Spring 2018 Announcements 2 A4 goes out today! Prelim 1: regrades are open a few rubrics have changed No Recitations next week (Fall Break Mon & Tue) We ll spend
More informationPriority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.
Priority Queues 1 Introduction Many applications require a special type of queuing in which items are pushed onto the queue by order of arrival, but removed from the queue based on some other priority
More information