Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Similar documents
Week 2. CS 400 Programming III. Read: Module 2 readings before lecture

Week 3 Web site:

AVL Trees. See Section 19.4of the text, p

Algorithms. AVL Tree

CS Introduction to Data Structures Week 5, 2017

CS102 Binary Search Trees

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Week 5. CS 400 Programming III

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

A set of nodes (or vertices) with a single starting point

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Programming II (CS300)

Balanced Search Trees. CS 3110 Fall 2010

Trees. (Trees) Data Structures and Programming Spring / 28

We have the pointers reference the next node in an inorder traversal; called threads

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Balanced Binary Search Trees

CS 315 Data Structures mid-term 2

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Trees. A tree is a directed graph with the property

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

TREES. Trees - Introduction

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

CS24 Week 8 Lecture 1

Binary search trees (chapters )

CS200: Balanced Search Trees

CMPE 160: Introduction to Object Oriented Programming

CS 261 Data Structures. AVL Trees

Binary search trees (chapters )

COMP : Trees. COMP20012 Trees 219

Data Structures in Java

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Spring 2018 Mentoring 8: March 14, Binary Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

COMP Analysis of Algorithms & Data Structures

Friday Four Square! 4:15PM, Outside Gates

CSI33 Data Structures

Week 9. CS 400 Programming III

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

8. Binary Search Tree

Ch04 Balanced Search Trees

CSE373 Fall 2013, Midterm Examination October 18, 2013

Balanced Binary Search Trees. Victor Gao

Trees. Prof. Dr. Debora Weber-Wulff

COMP Analysis of Algorithms & Data Structures

CSC148 Week 7. Larry Zhang

Binary Trees, Binary Search Trees

Title Description Participants Textbook

CS Transform-and-Conquer

Properties of red-black trees

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Searching: Introduction

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Part 2: Balanced Trees

AVL Trees Heaps And Complexity

Section 4 SOLUTION: AVL Trees & B-Trees

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

(2,4) Trees. 2/22/2006 (2,4) Trees 1

CSCI2100B Data Structures Trees

Principles of Computer Science

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

Data Structures Lesson 7

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Lecture 23: Binary Search Trees

CSCI Trees. Mark Redekopp David Kempe

CS350: Data Structures Red-Black Trees

CISC 235: Topic 4. Balanced Binary Search Trees

Advanced Tree Data Structures

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Second Examination Solution

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

INF2220: algorithms and data structures Series 1

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

Dynamic Access Binary Search Trees

Data Structures and Algorithms

CS 171: Introduction to Computer Science II. Binary Search Trees

COMP171. AVL-Trees (Part 1)

Binary Search Trees. Chapter 21. Binary Search Trees

CS 350 : Data Structures Binary Search Trees

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

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

Fall, 2015 Prof. Jungkeun Park

Binary Search Tree: Balanced. Data Structures and Algorithms Emory University Jinho D. Choi

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

Trees. Eric McCreath

Red-Black trees are usually described as obeying the following rules :

Data Structures in Java

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

CIS265/ Trees Red-Black Trees. Some of the following material is from:

AVL Trees. Reading: 9.2

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

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

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Transcription:

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 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS TAs and Peer Mentors will focus on helping students get p1 JUnit tests running in Lab. We can not guarantee that we can get your personal computers configured. Module: Week 2 (start on week 3 before next week) THIS WEEK Ready Set Program 1! o Read Assignment - there are getting started instructions there o Create and configure project for JUnit5, compile and run TestDS_My o Testing: JUnit5 Java: inner classes Determining Height of a Tree (Recursion Review) Binary Search Trees (BST) (Review?) o operations o implementing o complexities Classifying Binary Trees Balanced Search Trees George Adelson-Velsky and Evgenii Landis NEXT WEEK X-team Exercise x1 (in-class exercise with your assigned teams) Watch for instructions to find your team number and how to meet Copyright 2018 Deb Deppeler CS 400 (S19): W02-1

Writing JUnit5 Tests What is JUnit? a framework that allows the programmer to just write the tests the JUnit test runner will run and show results of your tests How do I use it? 1. Add JUnit 5 to the build path 2. Add a JUnit Test Case (class) 3. Add test methods with the @Test annotation 4. Run the Test Case class using JUnit5 test runner What does a @test method look like? contains code to do something and some "test" of the state of a variable, object, or other Details: 1. Import JUnit classes import static org.junit.jupiter.api.assertions.*; import org.junit.jupiter.api.afterall; import org.junit.jupiter.api.aftereach; import org.junit.jupiter.api.beforeall; import org.junit.jupiter.api.beforeeach; import org.junit.jupiter.api.test; public class TestClass { } @Test void test123_try_someting_check_results() { // try something // if (cond_expression) fail("descriptive failure message") // assertequals( expr1, expr2 ) // fail if something unexpected occurs or if something expected does not occur. } Note: the order the tests are run is not guaranteed Therefore, th test methods must work independent of the results of other tests Copyright 2018 Deb Deppeler CS 400 (S19): W02-2

Java's Inner Classes https://docs.oracle.com/javase/tutorial/java/javaoo/innerclasses.html What is an inner class? a class type that is defined within another class in Java. make it private (ensures used only within the encompassing class) allows the programmer the freedom to define and refactor the inner class as needed without worry that other code is dependent upon inner class Define a generic Tree cass using an inner class for the individual node type of the tree. public class Tree<K extends Comparable<K>> { } Copyright 2018 Deb Deppeler CS 400 (S19): W02-3

Determining Height of a Tree (or sub-tree) CS 400 Programming III Doing height here partially as a review of recursion and private helper methods height of a tree We define height of a tree as the # nodes on path from the root to the deepest leaf. Write a recursive definition for the height of a general tree. height(t) 0 if t is null 1 if t is leaf 1 + max ( height of children ) What are the base cases? recursive cases? Complete the recursive height method based on the recursive definition. Assume the method is added to a Tree class having a root instance variable. public int height() { } private int height(treenode<t> t) { DO ON OWN! Copyright 2018 Deb Deppeler CS 400 (S19): W02-4

Binary Search Tree (BST) Review Image created at: https://www.cs.usfca.edu/~galles/visualization/bst.html Insert 5, 27, 90, 73, 57 into the above BST tree (recall binary search algorithm) Insert the values 1,2,3,4,5 into an empty BST What can you conclude about the shape of a BST when the values are inserted in sorted order? Will you only get this shape if inserted in sorted order? Copyright 2018 Deb Deppeler CS 400 (S19): W02-5

Practice Deleting from a BST Delete 90 from this tree. Delete 40 and then 65 from this tree. Delete 10 and then 70 from this tree. How do you delete 30 or 50 from this tree? Recall: in-order predecessor and in-order successor Copyright 2018 Deb Deppeler CS 400 (S19): W02-6

Delete 30 from this tree using the in-order predecessor replace 30 with 20 and delete 20 from left sub-tree Delete 50 from this tree using the in-order successor replace 50 with 55 and delete 55 from right sub-treee How do we find in-order predecessor or in-order successor? Copyright 2018 Deb Deppeler CS 400 (S19): W02-7

Implementing BST Operations boolean lookup(bstnode<k> n, K key) { if n is null, return false if n's key = key, return true if key < n's key, return lookup(left,key) return lookup(right,key) public insert(k key) { insert(root,key); } /** pre-cond: key is NOT null */ private BSTnode<K> insert(bstnode<k> n, K key) throws DuplicateKeyException { if n is null, return new node with this key if key equals n's key, throw DuplicateKeyException if key < n's key, assign insert(left,key) to n's left subtree else assign insert(right,key) to n's right subtree return current node Copyright 2018 Deb Deppeler CS 400 (S19): W02-8

Implementing BST Operations delete /** pre-cond: key is not null */ private BSTnode<K> delete(bstnode<k> n, K key) { if n is null, return n // key not found if key equals n's key // key found, 3 cases to consider CASE #1: n has no children // "unlink N" by returning null // to appropriate child of parent CASE #2: n has one child // return n's other child to parent if (n.left==null) return n.right if (n.right==null) return n.left CASE #3: n has two children // find replacement value (v) for n // delete v from appropriate child of n 1. find replacement value largest value in left (in-order predecessor) smallest value in right (in-order successor) 2. copy v into n's key 3. recursively delete v from approp sub-tree KEY CONCEPT: AVOID LOTS OF RELINKING, FIND REPLACEMENT VALUE INSTEAD Copyright 2018 Deb Deppeler CS 400 (S19): W02-9

Complexities of BST Operations Problem size: N = print: O(N) review traversals here lookup: O(H) must compute height as before insert: O(H) must compute height delete: O(H) must compute height k-ary Tree Classification Terms Perfect: (referred to as full in some readings) all internal nodes have n-children and all leaves at same level, H = 2^N -1 Complete: all levels filled except possibly last level, leaves at last level filled left to right Height-Balanced: every node is has sub-trees heights that differ by at most one Balanced A tree is balanced if the insert operation is O(log2 N) Copyright 2018 Deb Deppeler CS 400 (S19): W02-10

Balanced Search Trees Goal: keep height at O(log2N) where N is the number of nodes Idea: have insert and delete maintain balance 1. Detect out of balance 2. Fix balance by??? How do we detect and fix? 1. Check height of each sub-tree 2. If height of each sub-tree differs by more than 1, it is not balanced Balance Factor: The height of the left sub-tree minus the height of the right sub-tree. If balance-factor <= 1 for all nodes of the tree, the tree is height-balanced Copyright 2018 Deb Deppeler CS 400 (S19): W02-11

Height-Balanced Practice Which of these trees is not height-balanced? What is B.F. of each node? Copyright 2018 Deb Deppeler CS 400 (S19): W02-12

Balancing I - Practice Draw BST for: 10,20,30 Draw BST for: 30,20,10 Draw BST for 10,30,20 Draw BST for 30,10,20 Copyright 2018 Deb Deppeler CS 400 (S19): W02-13

Balancing II - General Balancing Assume: a triangle represents sub-tree of unknown height, but that trees shown are height-balanced, and show the relative difference (balance factor of -1, 0, 1) CS 400 Programming III Copyright 2018 Deb Deppeler CS 400 (S19): W02-14

Balancing III - More General Balancing Copyright 2018 Deb Deppeler CS 400 (S19): W02-15

Georgy Adelson-Velsky and Evgenii Landis described a tree that maintain its height-balanced uses the "rotations" to maintain balance CS 400 Programming III Complexity of operations O(H) but what is the height of an almost-balanced AVL tree? AV-L proved that in the worst-case AVL tree is ~1.44 log2(n) Implementation Must know balance factor for each node Detect out of balance: when BF > 1 If insert was in left child's left sub-tree, right rotate If insert was in right child's right sub-tree, left rotate If insert was in right child's left sub-tree, right-left rotate If insert was in left child's right sub-tree, left-right rotate How can we know height of each sub-tree? compute height extra processing on each insert delete store height in node must keep current store height difference - -1,0,1 (or it is out of balance) Copyright 2018 Deb Deppeler CS 400 (S19): W02-16