/department of mathematics and computer science 1/58

Size: px
Start display at page:

Download "/department of mathematics and computer science 1/58"

Transcription

1 =! # Linked lists and binary search trees Linked list: % $ " A N J A N J A N J A N J E E I J A A A K > A A N J E E I - A A K > A L C A J 5 E A E J E I A H J - A A J ) K > A E J L E I - F J O > A = E I = I J - A A J > A = E E I J E E I K > A H A L A - A A J ) J E J L H A L A = I J - A A J L /department of mathematics and computer science 1/58

2 A linked list public class LinkedList { protected double element; protected LinkedList next; public LinkedList() { this.element = Double.POSITIVE_INFINITY; // empty this.next = null; public LinkedList(double element) { this.element = element; this.next = null; /department of mathematics and computer science 2/58

3 A linked list public boolean isempty() { return element == Double.POSITIVE_INFINITY; public boolean islastelement() { return next == null; public int getsize() { if (isempty()) return 0; else { if (islastelement()) return 1; else return 1 + next.getsize(); /department of mathematics and computer science 3/58

4 A linked list public void addelement(double element) { if (isempty()) this.element = element; else { if (islastelement()) { this.next = new LinkedList(element); else { next.addelement(element); /department of mathematics and computer science 4/58

5 A linked list public void removelastelement() { if (islastelement()) { // only if this is first element in list! element = Double.POSITIVE_INFINITY; else { if (next.islastelement()) next = null; else next.removelastelement(); /department of mathematics and computer science 5/58

6 A linked list public void removeelementat(int index) { if (index == 0) { if (islastelement()) element = Double.POSITIVE_INFINITY; else { element = next.element; next = next.next; else if (index == 1) { next = next.next; else if (index > 1 &&!islastelement()) { next.removeelementat(index - 1); /department of mathematics and computer science 6/58

7 A linked list public void insertat(double element, int index) { if (index == 0) { if (isempty()) this.element = element; else { LinkedList oldnext = next; next = new LinkedList(this.element); this.element = element; next.next = oldnext; else if (index > 0) { if (islastelement()) next = new LinkedList(element); else next.insertelementat(element, index - 1); /department of mathematics and computer science 7/58

8 A linked list public String tostring() { String s = ""; if (!isempty()) { s = s + " " + element; if (!islastelement()) { return s + next.tostring(); return s; /department of mathematics and computer science 8/58

9 A linked list LinkedList list = new LinkedList(); E I J /department of mathematics and computer science 9/58

10 A linked list LinkedList list = new LinkedList(); list.addelement(3.0);! E I J /department of mathematics and computer science 10/58

11 ! # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); E I J A N J /department of mathematics and computer science 11/58

12 ! # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); E I J A N A N J /department of mathematics and computer science 12/58

13 ! # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); E I J A N A N J! /department of mathematics and computer science 13/58

14 # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); % E I J A N A N J! /department of mathematics and computer science 14/58

15 # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); % E I J A N A N J! A N J /department of mathematics and computer science 15/58

16 # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); %! E I J A N J A N J /department of mathematics and computer science 16/58

17 # A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); list.removelastelement(); %! E I J A N J /department of mathematics and computer science 17/58

18 A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); list.removelastelement(); E I J % A N J! /department of mathematics and computer science 18/58

19 A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); list.removelastelement(); list.addelement(1.9); %! ' E I J A N J A N J /department of mathematics and computer science 19/58

20 ! A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); list.removelastelement(); list.addelement(1.9); list.removeelementat(0);! ' E I J A N J A N J /department of mathematics and computer science 20/58

21 ! A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); list.removelastelement(); list.addelement(1.9); list.removeelementat(0);! ' E I J A N J A N J /department of mathematics and computer science 21/58

22 A linked list LinkedList list = new LinkedList(); list.addelement(3.0); list.addelement(5.0); list.insertelementat(7.2, 0); list.removelastelement(); list.addelement(1.9); list.removeelementat(0);! ' E I J A N J /department of mathematics and computer science 22/58

23 A sorted linked list public class SortedLinkedList extends LinkedList { public SortedLinkedList() { super(); public SortedLinkedList(double element) { super(element); /department of mathematics and computer science 23/58

24 A sorted linked list Of course the method insertelementat should be overridden (it cannot be removed since it is automatically inherited from LinkedList. public void insertelementat(double element, int idx) { addelement(element); /department of mathematics and computer science 24/58

25 A sorted linked list public void addelement(double element) { if (isempty()) this.element = element; else { if (element < this.element) super.insertelementat(element, 0); else { if (islastelement()) { this.next = new SortedLinkedList(element); else { next.addelement(element); /department of mathematics and computer science 25/58

26 A sorted linked list SortedLinkedList list = new SortedLinkedList(); /department of mathematics and computer science 26/58

27 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); 3.0 /department of mathematics and computer science 27/58

28 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); list.addelement(5.0); /department of mathematics and computer science 28/58

29 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); list.addelement(5.0); list.addelement(4.0); /department of mathematics and computer science 29/58

30 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); list.addelement(5.0); list.addelement(4.0); list.insertelementat(7.2, 0); /department of mathematics and computer science 30/58

31 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); list.addelement(5.0); list.addelement(4.0); list.insertelementat(7.2, 0); list.removelastelement(); /department of mathematics and computer science 31/58

32 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); list.addelement(5.0); list.addelement(4.0); list.insertelementat(7.2, 0); list.removelastelement(); list.addelement(1.9); /department of mathematics and computer science 32/58

33 A sorted linked list SortedLinkedList list = new SortedLinkedList(); list.addelement(3.0); list.addelement(5.0); list.addelement(4.0); list.insertelementat(7.2, 0); list.removelastelement(); list.addelement(1.9); list.removeelementat(0); /department of mathematics and computer science 33/58

34 ! % & A binary search tree A B J H E C D J A B J A B J H E C D J " Advantage: looking up and storing an element is much faster than in a sorted linked list. /department of mathematics and computer science 34/58

35 A binary search tree We would like to put any object in the tree, not just doubles. But in order to find the correct order, the objects have to be comparable. This way we can determine which object is "less than" the other object. The java.lang.comparable interface: This interface contains one method: compareto(object). It returns an integer that defines the comparison (the distance) between the two object. Classes like Double, Integer, String implement this interface. /department of mathematics and computer science 35/58

36 To make an Event comparable: public class Event implements Comparable<Event> { public int compareto(event o) { Double thistime = new Double(this.time); return thistime.compareto(o.time); /department of mathematics and computer science 36/58

37 A binary search tree public class BinarySearchTree { protected Comparable root; protected BinarySearchTree left; protected BinarySearchTree right; public BinarySearchTree() { this.left = null; this.right = null; this.root = null; public BinarySearchTree(Comparable item) { this(); addelement(item); /department of mathematics and computer science 37/58

38 A binary search tree public boolean isempty() { return root == null; public Comparable getsmallestelement() { if (left == null) { return root; else { return left.getsmallestelement(); /department of mathematics and computer science 38/58

39 A binary search tree public void addelement(comparable element) { if (isempty()) this.root = element; else { if (element.compareto(root) < 0) { if (left == null) left = new BinarySearchTree(element); else left.addelement(element); else { if (right == null) right = new BinarySearchTree(element); else right.addelement(element); /department of mathematics and computer science 39/58

40 A binary search tree public void removesmallestelement() { if (left == null) { if (right!= null) { root = right.root; left = right.left; right = right.right; else root = null; else if (left.left == null && left.right == null) { left = null; else { left.removesmallestelement(); /department of mathematics and computer science 40/58

41 A binary search tree public void removelargestelement() { if (right == null) { if (left!= null) { root = left.root; right = left.right; left = left.left; else root = null; else if (right.left == null && right.right == null) right = null; else { right.removelargestelement(); /department of mathematics and computer science 41/58

42 A binary search tree public int getsize() { int n = 0; if (this.root!= null) n++; if (left!= null) n += left.getsize(); if (right!= null) n += right.getsize(); return n; public String tostring() { String s = ""; if (left!= null) s += left.tostring(); if (root!= null) s += " " + root; if (right!= null) s += right.tostring(); return s; /department of mathematics and computer science 42/58

43 A binary search tree Advantage: storing and retrieving an element from a binary search tree is very fast (order log n compared to order n) especially suitable for huge lists Disdvantages: recursive methods are used to traverse the tree, which requires more memory than sequential methods it is difficult to retrieve the n-th element from the tree. /department of mathematics and computer science 43/58

44 A binary search tree BinarySearchTree list = new BinarySearchTree(); list.addelement(3.0); list.addelement(5.0); list.addelement(4.0); list.addelement(7.2); list.removelargestelement(); list.addelement(1.9); list.removelargestelement(); list.removesmallestelement(); list.removesmallestelement(); /department of mathematics and computer science 44/58

45 A binary search tree BinarySearchTree list = new BinarySearchTree(); /department of mathematics and computer science 45/58

46 A binary search tree list.addelement(3.0);! /department of mathematics and computer science 46/58

47 ! A binary search tree list.addelement(5.0); H E C D J # /department of mathematics and computer science 47/58

48 ! " # A binary search tree list.addelement(4.0); H E C D J A B J /department of mathematics and computer science 48/58

49 ! # A binary search tree list.addelement(7.2); H E C D J A B J H E C D J " % /department of mathematics and computer science 49/58

50 ! # A binary search tree list.removelargestelement(); H E C D J A B J " % /department of mathematics and computer science 50/58

51 ! " # A binary search tree list.removelargestelement(); H E C D J A B J /department of mathematics and computer science 51/58

52 ! " # A binary search tree list.addelement(1.9); A B J H E C D J ' A B J /department of mathematics and computer science 52/58

53 ! " " A binary search tree list.removelargestelement(); A B J H E C D J ' /department of mathematics and computer science 53/58

54 ! " A binary search tree list.removelargestelement(); A B J H E C D J ' /department of mathematics and computer science 54/58

55 ! " A binary search tree list.removesmallestelement(); H E C D J ' /department of mathematics and computer science 55/58

56 ! A binary search tree list.removesmallestelement(); H E C D J " /department of mathematics and computer science 56/58

57 " A binary search tree list.removesmallestelement(); " /department of mathematics and computer science 57/58

58 A binary search tree list.removesmallestelement(); " /department of mathematics and computer science 58/58

a graph is a data structure made up of nodes in graph theory the links are normally called edges

a graph is a data structure made up of nodes in graph theory the links are normally called edges 1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in

More information

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

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 information

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12 CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can

More information

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2016 CSC 321: Data Structures Fall 2016 Trees & recursion trees, tree recursion BinaryTree class BST property BinarySearchTree class: override add, contains search efficiency 1 Trees a tree is a nonlinear data

More information

AVL Trees. See Section 19.4of the text, p

AVL Trees. See Section 19.4of the text, p AVL Trees See Section 19.4of the text, p. 706-714. AVL trees are self-balancing Binary Search Trees. When you either insert or remove a node the tree adjusts its structure so that the remains a logarithm

More information

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions Birkbeck (University of London) Software and Programming 1 In-class Test 2.1 16 Mar 2017 Student Name Student Number Answer ALL Questions 1. What output is produced when the following Java program fragment

More information

Building Java Programs

Building Java Programs Building Java Programs Binary Trees reading: 17.1 17.3 2 Trees in computer science TreeMap and TreeSet implementations folders/files on a computer family genealogy; organizational charts AI: decision trees

More information

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1 Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2 Administrative

More information

Generic BST Interface

Generic BST Interface Generic BST Interface Here s a partial generic BST interface: public class BST

More information

: Advanced Programming Final Exam Summer 2008 June 27, 2008

: Advanced Programming Final Exam Summer 2008 June 27, 2008 15-111 : Advanced Programming Final Exam Summer 2008 June 27, 2008 Name: Andrew ID: Answer the questions in the space provided following each question. We must be able to clearly understand your answer.

More information

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree Binary Search Trees CMPUT 115 - Lecture Department of Computing Science University of Alberta Revised 21-Mar-05 In this lecture we study an important data structure: Binary Search Tree (BST) Motivation

More information

Binary Search Trees. Ravi Chugh March 28, 2014

Binary Search Trees. Ravi Chugh March 28, 2014 Binary Search Trees Ravi Chugh March 28, 2014 1 Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length SoluIon: Linked Lists null 2 Review: Linked Lists class

More information

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W EXERCISES SOFTWARE DEVELOPMENT I 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W Recursion I RECURSION :: MOTIVATION AND DEFINITION Many complex real-world problems can be solved

More information

COMP 103. Data Structures and Algorithms

COMP 103. Data Structures and Algorithms T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I V I C T O R I A UNIVERSITY OF WELLINGTON EXAMINATIONS 2004 END-YEAR COMP 103 Data Structures and Algorithms Time Allowed: 3 Hours Instructions:

More information

CSC 321: Data Structures. Fall 2012

CSC 321: Data Structures. Fall 2012 CSC 321: Data Structures Fall 2012 Proofs & trees proof techniques direct proof, proof by contradiction, proof by induction trees tree recursion BinaryTree class 1 Direct proofs the simplest kind of proof

More information

Programming II (CS300)

Programming 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 information

University of KwaZulu-Natal Pietermaritzburg Campus Examinations

University of KwaZulu-Natal Pietermaritzburg Campus Examinations University of KwaZulu-Natal Pietermaritzburg Campus Examinations Data Structures COMP201 Date: November 2006. Examiners: Hugh Murrell and Satya Baboolal time limit: 3 hours max marks: 100 This paper consists

More information

ITI Introduction to Computing II

ITI 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 information

ITI Introduction to Computing II

ITI 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 information

CS 151. Binary Search Trees. Wednesday, October 10, 12

CS 151. Binary Search Trees. Wednesday, October 10, 12 CS 151 Binary Search Trees 1 Binary Search Tree A binary search tree stores comparable elements in a binary tree such that for every node r all nodes in the left BST are

More information

1. Binary Tree Traversal. 2. Binary Search Tree. 1 of 12. Consider the following tree: Pre-order: In-order: Post-order:

1. Binary Tree Traversal. 2. Binary Search Tree. 1 of 12. Consider the following tree: Pre-order: In-order: Post-order: 1. Binary Tree Traversal Consider the following tree: +---+ 4 +---+ 1 9 / / +---+ 6 0 2 +---+ 3 8 \ \ \ \ 7 5 Fill in each of the traversals below : Pre-order: In-order: Post-order: 2. Binary Search Tree

More information

CmpSci 187: Programming with Data Structures Spring 2015

CmpSci 187: Programming with Data Structures Spring 2015 CmpSci 187: Programming with Data Structures Spring 2015 Lecture #17, Implementing Binary Search Trees John Ridgway April 2, 2015 1 Implementing Binary Search Trees Review: The BST Interface Binary search

More information

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces. CSM 61B Abstract Classes & Interfaces Spring 2017 Week 5: February 13, 2017 1 An Appealing Appetizer 1.1 public interface Consumable { public void consume (); public abstract class Food implements Consumable

More information

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

More information

Back To LinkedLists 1. 3 public Node left; 4 public Node right; 6 public Node(int data, Node left, Node right) {

Back To LinkedLists 1. 3 public Node left; 4 public Node right; 6 public Node(int data, Node left, Node right) { Adam Blank Leture Autumn 0 CSE 3X Aelerated Computer Programming I/II CSE 3X: Aelerated Computer Programming I/II Binary Trees 0 00 00 00 00 0 000 00 00 0 00 0 000 000 000 0 0 00 0000 00 000 00 00 0 000

More information

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

Points off Total off Net Score. CS 314 Final Exam Spring 2017

Points off Total off Net Score. CS 314 Final Exam Spring 2017 Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Spring 2017 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points.

More information

Programming II (CS300)

Programming 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 information

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2012 MID YEAR COMP103 Introduction to Data

More information

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction

More information

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

CS211, 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 information

Spring 2018 Mentoring 8: March 14, Binary Trees

Spring 2018 Mentoring 8: March 14, Binary Trees CSM 6B Binary Trees Spring 08 Mentoring 8: March 4, 08 Binary Trees. Define a procedure, height, which takes in a Node and outputs the height of the tree. Recall that the height of a leaf node is 0. private

More information

Facebook. / \ / \ / \ Accenture Nintendo

Facebook. / \ / \ / \ Accenture Nintendo 1. Binary Tree Traversal Consider the following tree: +---+ 4 +---+ 1 9 / / +---+ 6 0 2 +---+ 3 8 \ \ \ \ 7 5 Fill in each of the traversals below : Pre-order: 4 1 6 3 7 0 8 5 9 2 In-order: 3 7 6 1 0 8

More information

CSC 421: Algorithm Design & Analysis. Spring 2015

CSC 421: Algorithm Design & Analysis. Spring 2015 CSC 421: Algorithm Design & Analysis Spring 2015 Divide & conquer divide-and-conquer approach familiar examples: merge sort, quick sort other examples: closest points, large integer multiplication tree

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization Object Structures Trees CISH 4020 Tom Blough blought@rh.edu www.rh.edu/~blought 860.618.4148 Lecture Topics Tree Concepts Traversals of a Tree Java Interfaces for Trees Examples of Binary Trees Examples

More information

Programming II (CS300)

Programming 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 information

IMPLEMENTING BINARY TREES

IMPLEMENTING BINARY TREES IMPLEMENTING BINARY TREES Chapter 6 A Binary Tree BinaryTree a = new BinaryTree(); a A B C D E F 1 A Binary Tree BinaryTree a = new BinaryTree(); a A B C D E F A Binary

More information

Heaps. Heaps. A heap is a complete binary tree.

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

Priority Queues. 04/10/03 Lecture 22 1

Priority 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 information

CS S-20 Linked Lists IV 1

CS S-20 Linked Lists IV 1 CS112-2012S-20 Linked Lists IV 1 20-0: Doubly Linked List Deleting from (and inserting into!) a linked can be challenging because you need to find the node before the node you are looking for Once you

More information

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp CSE 143 Lecture 19 Binary Trees read 17.1-17.2 slides created by Marty Stepp http://www.cs.washington.edu/143/ Trees tree: A directed, acyclic structure of linked nodes. directed : Has one-way links between

More information

Lecture 27. Binary Search Trees. Binary Search Trees

Lecture 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 information

Hierarchical Structures

Hierarchical Structures Chapter 10 Hierarchical Structures So far, we have studied sequential structures that assign each of their elements an index in the structure. This imposes an ordering relationship between elements based

More information

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003 Data abstractions: ADTs Invariants, Abstraction function Lecture 4: OOP, autumn 2003 Limits of procedural abstractions Isolate implementation from specification Dependency on the types of parameters representation

More information

protected BinaryNode root; } 02/17/04 Lecture 11 1

protected 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 information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name 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 information

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures Final Examination (17 pages) Instructor: Douglas Harder April 14, 2004 9:00-12:00 Name (last,

More information

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1 Topic #9: Collections CSE 413, Autumn 2004 Programming Languages http://www.cs.washington.edu/education/courses/413/04au/ If S is a subtype of T, what is S permitted to do with the methods of T? Typing

More information

CSC 207 (17fa) Practice Final Page 1 of 19. Practice Final

CSC 207 (17fa) Practice Final Page 1 of 19. Practice Final CSC 207 (17fa) Practice Final Page 1 of 19 Practice Final Like the midterms, the final is closed-book, closed technology. The examination period is 180 minutes. The final covers everything that we have

More information

void insert( Type const & ) void push_front( Type const & )

void 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 information

Linked Lists. References and objects

Linked Lists. References and objects Linked Lists slides created by Marty Stepp http://www.cs.washington.edu/143/ Modified by Sarah Heckman Reading: RS Chapter 16 References and objects In Java, objects and arrays use reference semantics.

More information

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering Readings and References Java Collections References» "Collections", Java tutorial» http://java.sun.com/docs/books/tutorial/collections/index.html CSE 403, Winter 2003 Software Engineering http://www.cs.washington.edu/education/courses/403/03wi/

More information

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa CS 2230 CS II: Data structures Meeting 21: trees Brandon Myers University of Iowa Today s learning objectives Define tree (a data structure that can store data hierarchically) and describe trees in terms

More information

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

Figure 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 information

Tables and Priority Queues

Tables and Priority Queues Chapter 12 Tables and Priority Queues 2011 Pearson Addison-Wesley. All rights reserved 12 A-1 The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that

More information

Sample Questions for Midterm Exam 2

Sample Questions for Midterm Exam 2 Sample Questions for Midterm Exam 2 The following are meant to give you some examples of questions that might be asked on the second midterm exam. The sample exam questions do not represent the length

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 15, 2017 Outline Outline 1 C++ Supplement: 1.2 Outline C++ Supplement: 1.2 1 C++ Supplement: 1.2 The Binary Search

More information

Arrays. Chapter Arrays What is an Array?

Arrays. Chapter Arrays What is an Array? Chapter 8 Arrays 81 Arrays 811 What is an Array? To motivate why we might be interested in using arrays, let us implement an app that creates a collection of doubles We will keep track of the number of

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! 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 information

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi Stack and Queue How do we implement a Queue using Array? : A collection of nodes with linear ordering defined on them. Each node holds an element and points to the next node in the order. The first node

More information

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II 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 1 Review:

More information

Tables and Priority Queues!

Tables and Priority Queues! Chapter 12! Tables and Priority Queues! 2011 Pearson Addison-Wesley. All rights reserved 12 A-1 2015-11-30 21:52:31 1/49 Chapter-12.pdf (#14) The ADT Table The ADT table, or dictionary! Uses a search key

More information

CMSC 341 Lecture 10 Binary Search Trees

CMSC 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 information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-12-01 09:30:53 1/54 Chapter-11.pdf (#13) Terminology Definition of a general tree! A general tree T is a set of one or

More information

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 Chapter 11!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1 2015-03-25 21:47:41 1/53 Chapter-11.pdf (#4) Terminology Definition of a general tree! A general tree T is a set of one or more

More information

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST) CSCI Lab 9 Implementing and Using a Binary Search Tree (BST) Preliminaries In this lab you will implement a binary search tree and use it in the WorkerManager program from Lab 3. Start by copying this

More information

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

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

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time

1 (5pts) 2 (20 pts) 3 (20 pts) 4 (10 pts) 5 (15 pts) 6 (30 pts) Total NAME: CU ID: Recitation instructor/time CS 211 Computers and Programming Fall 2003 Prelim II 11/18/2003 NAME: CU ID: Recitation instructor/time You have one and a half hours to do this exam All programs in this exam must be written in Java Excessively

More information

Task 2 What is printed out when the code is executed?

Task 2 What is printed out when the code is executed? Task 1 What is printed out when the code is executed? public class Class1 { public static void main(string[] args) { int array[] = {14,5,7; for (int counter1 = 0; counter1 < array.length; counter1++) {

More information

Introduction to Computing II (ITI 1121) Final Examination

Introduction to Computing II (ITI 1121) Final Examination Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of Engineering School of Electrical Engineering and Computer Science Introduction

More information

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3. Linked Lists Walls and Mirrors Chapter 5 Linked List Nodes public class Node { private int item; private Node next; public Node(int item) { this(item,null); public Node(int item, Node next) { setitem(item);

More information

EXAMINATIONS 2017 TRIMESTER 2

EXAMINATIONS 2017 TRIMESTER 2 T E W H A R E W Ā N A N G A O T E Ū 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 2017 TRIMESTER 2 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

More information

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

DM537 Object-Oriented Programming. Peter Schneider-Kamp. DM537 Object-Oriented Programming Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm537/! RECURSION (REVISITED) 2 Recursion (Revisited) recursive function = a function that calls

More information

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Trees Chapter 19, 20. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Trees Chapter 19, 20 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Trees: Trees as data structures Tree terminology Tree implementations Analyzing tree efficiency Tree traversals

More information

(Constructor) public A (int n){ for (int i = 0; i < n; i++) { new A(i); } System.out.println("*"); }

(Constructor) public A (int n){ for (int i = 0; i < n; i++) { new A(i); } System.out.println(*); } !!#!#"! (Constructor) A public A (int n){ for (int i = 0; i < n; i++) { new A(i); System.out.println("*"); % 1. new A(0); 2. new A(1); 3. new A(2); 4. new A(3); & ) n 5. A a = new A(n);! '#" +"()* " %floating

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008 CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008 Name : Section (eg. AA) : TA : This is an open-book/open-note exam. Space is provided for your answers. Use the backs of pages if necessary. The

More information

C Sc 227 Practice Final

C Sc 227 Practice Final C Sc 227 Practice Final Name 1. Use the Node class shown question 13 on this page below with data and next instance variables and this view of a linked structure to answer the questions a) through d) a)

More information

Binary Trees Ernesto

Binary Trees Ernesto General Trees: Terminology (1) inary Trees Ernesto David hris Elsa nna EES2030: dvanced Object Oriented Programming Fall 2017 HEN-WEI WNG Shirley Vanessa Peter root of tree : top element of the tree e.g.,

More information

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers

03/29/2004. A dispenser has three essential features: adding, removing, accessing. Dispensers Dispensers stacks queues priority queues Dictionaries A dispenser is a container that restricts access to its elements. Only one element in the container can be accessed or removed. This element is called

More information

Csci 102: Sample Exam

Csci 102: Sample Exam Csci 102: Sample Exam Duration: 65 minutes Name: NetID: Student to your left: Student to your right: DO NOT OPEN THIS EXAM UNTIL INSTRUCTED Instructions: Write your full name and your NetID on the front

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! 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

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes

Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes Solution to Test of Computer Science 203x Level Score: / 100 Time: 100 Minutes PART I: Multiple Choice (3 points each) Note: The correct answer can be any combination of A, B, C, D. Example, Sample Question:

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

CS200 Midterm 2 Fall 2007

CS200 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 information

Computer Science II Fall 2009

Computer Science II Fall 2009 Name: Computer Science II Fall 2009 Exam #2 Closed book and notes. This exam should have five problems and six pages. Problem 0: [1 point] On a scale of 0 5, where 5 is highest, I think I deserve a for

More information

Collections Algorithms

Collections Algorithms Collections Algorithms 1 / 11 The Collections Framework A collection is an object that represents a group of objects. The collections framework allows different kinds of collections to be dealt with in

More information

CS171 Midterm Exam. October 29, Name:

CS171 Midterm Exam. October 29, Name: CS171 Midterm Exam October 29, 2012 Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 50 minutes to complete this exam. Read each problem carefully, and

More information

Balanced Binary Search Trees

Balanced Binary Search Trees Balanced Binary Search Trees Why is our balance assumption so important? Lets look at what happens if we insert the following numbers in order without rebalancing the tree: 3 5 9 12 18 20 1-45 2010 Pearson

More information

Maximum Grade: 5 points out of 10 of the exam.

Maximum Grade: 5 points out of 10 of the exam. Systems Programming Audiovisual Systems Engineering, Communications Systems Engineering, Telecommunication Technologies Engineering and Telematics Engineering Degrees Leganés, May 20th, 2014. Duration:

More information

CMPSCI 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 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 information

COMP 250 Midterm #2 March 11 th 2013

COMP 250 Midterm #2 March 11 th 2013 NAME: STUDENT ID: COMP 250 Midterm #2 March 11 th 2013 - This exam has 6 pages - This is an open book and open notes exam. No electronic equipment is allowed. 1) Questions with short answers (28 points;

More information

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References

Class Libraries. Readings and References. Java fundamentals. Java class libraries and data structures. Reading. Other References Reading Readings and References Class Libraries CSE 142, Summer 2002 Computer Programming 1 Other References» The Java tutorial» http://java.sun.com/docs/books/tutorial/ http://www.cs.washington.edu/education/courses/142/02su/

More information

Communications of the ACM Learning to Learn Denning & Flores https://cacm.acm.org/magazines/2016/12/ learning-to-learn/fulltext

Communications of the ACM Learning to Learn Denning & Flores https://cacm.acm.org/magazines/2016/12/ learning-to-learn/fulltext Communications of the ACM Learning to Learn Denning & Flores https://cacm.acm.org/magazines/2016/12/210369-learning-to-learn/fulltext CS 2230 CS II: Data structures Meeting 22: tree traversal Brandon Myers

More information

CS 307 Final Fall 2009

CS 307 Final Fall 2009 Points off 1 2 3 4 5 6 Total off Net Score CS 307 Final Fall 2009 Name UTEID login name Instructions: 1. Please turn off your cell phones. 2. There are 6 questions on this test. 3. You have 3 hours to

More information

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps

9/26/2018 Data Structure & Algorithm. Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 9/26/2018 Data Structure & Algorithm Assignment04: 3 parts Quiz: recursion, insertionsort, trees Basic concept: Linked-List Priority queues Heaps 1 Quiz 10 points (as stated in the first class meeting)

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Trees Kostas Alexis Trees List, stacks, and queues are linear in their organization of data. Items are one after another In this section, we organize data in a

More information

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

DM537 Object-Oriented Programming. Peter Schneider-Kamp. DM537 Object-Oriented Programming Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm537/! ABSTRACT DATA TYPES FOR (BINARY) TREES 2 Trees trees store elements non-sequentially every

More information