DISCUSSION OF SOME QUESTIONS ON PRELIM 2 Lecture 23 CS2110 – Spring 2015.

Slides:



Advertisements
Similar presentations
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Advertisements

CS 206 Introduction to Computer Science II 09 / 24 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
CS 261 – Fall 2009 Binary Search Trees Again, but in detail.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
TREES Lecture 12 CS2110 – Fall Announcements  Prelim #1 is tonight!  Olin 155  A-L  5:30  M-Z  5:30  A4 will be posted today  Mid-semester.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
Binary Search Trees (BST)
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
CS 367 Introduction to Data Structures Lecture 8.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
TREES Lecture 11 CS2110 – Spring Readings and Homework  Textbook, Chapter 23, 24  Homework: A thought problem (draw pictures!)  Suppose you use.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
(c) University of Washington20-1 CSC 143 Java Trees.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Recursive Objects (Part 4)
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
Trees Lecture 12 CS2110 – Spring 2017.
Binary Trees Linked lists: efficient insertion/deletion, inefficient search ArrayList: search can be efficient, insertion/deletion not Binary trees: efficient.
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
October 30th – Priority QUeues
Trees Lecture 12 CS2110 – Fall 2016.
Trees.
Cse 373 April 14th – TreEs pt 2.
Chapter 20: Binary Trees.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Chapter 21: Binary Trees.
Trees 7/14/2009.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Find in a linked list? first last 7  4  3  8 NULL
Trees CMSC 202, Version 5/02.
Recursion (Continued)
Tree Rotations and AVL Trees
CMSC 202 Trees.
Announcements Prelim 1 on Tuesday! A4 will be posted today
Trees, part 2 Lecture 13 CS2110 – Spring 2019
CSC 143 Java Trees.
CSC 143 Binary Search Trees.
Chapter 20: Binary Trees.
Tree.
Trees.
CS 106B Lecture 20: Binary Search Trees Wednesday, May 17, 2017
Presentation transcript:

DISCUSSION OF SOME QUESTIONS ON PRELIM 2 Lecture 23 CS2110 – Spring 2015

About prelim 2 Mean: Median: 78 The few programming problems (recursion) were not done so well, will discuss in a minute. Pick up prelim in handback room 216 Gates. Regrade request? Fill out form. Please wait until you see solutions, which we expect to post on the Piazza tonight. 2 Please do not asking for your course grade based on what you have done so far. We can’t answer that now. Too many other things to do. Prelims are important. Most people do very well on assignments. Prelims show mastery of material

A8 available today Due date: Tuesday, 5 May (last day of class) We may allow them until 7-8 May, with very small penalty. But we don’t guarantee that yet. As soon as possible after A8 deadline, we complete grading, figure out tentative course grade, and make it available. You choose to take final or not. Taking it may lower as well as raise grade (does not happen often). Final optional: Sunday 17 May 3

Recursion 4 It was heartbreaking to see so many people not getting recursion problems correct. These were relatively easy. Let’s try one last time to get across how to write recursive methods. To do these problems well, you have to: Read specification Deal with base cases Use the recursive definition of the data structure Keep things simple –don’t complicate Draw diagrams, pictures. Changing a pattern of thinking requires consciously applying good strategies, principles

Reversing a BST 5 Consider trees whose nodes, of class Node, contain 3 fields: value: the value at this node. Type is some class left: left subtree (null if empty) right:right subtree (null if empty) BST created using > instead of < for comparison, so the tree got put in kind of backward. E.g. inorder traversal of one BST should have been (1, 3, 4, 5, 6, 9) but was (9, 6, 5, 4, 3, 1) Don’t change field value

Reverse a BST /** Precondition: t is a binary search tree. * If t != null, change as many nodes of t as necessary * to reverse the inorder traversal of the tree. */ public static void reverse(Node t) { if (t = null) return; 6 Estimate: over 25% people missed this base case. Why? 1.Did not read the specification. 2.Did not think about base case.

Reverse a BST /** Precondition: t is a binary search tree. * If t != null, change as many nodes of t as necessary * to reverse the inorder traversal of the tree. */ public static void reverse(Node t) { if (t = null) return; if (t.left == null && t.right == null) {…} if (t.left.left …) {…} 7 Complicates matters. Doesn’t use the recursive definition of the data structure properly. root

Reverse a BST /** Precondition: t is a binary search tree. * If t != null, change as many nodes of t as necessary * to reverse the inorder traversal of the tree. */ public static void reverse(Node t) { if (t = null) return; 8 Tree is either null or root tree When viewing a tree as a recursive structure, like this, the code that is processing the root - --the whole tree--- should not look into the contents of the subtrees. They are just trees. Writing a recursive tree method? Draw this tree!

Reverse a BST /** Precondition: t is a binary search tree. * If t != null, change as many nodes of t as necessary * to reverse the inorder traversal of the tree. */ public static void reverse(Node t) { if (t = null) return; Swap t.left and t.right; reverse(t.left); reverse(t.right); 9 Tree is either null or root tree Writing a recursive tree method? Draw this tree!

/** Return true iff tree t equals tree s –meaning they have the same shape and same values in their corresponding nodes. */ public static boolean equals(Node s, Node t) { Complicated code that looks at s.left.value, s.right.value, s.left.left, etc Tree equality 10 Again, many people didn’t handle the base case: At least one of s and t being null. root But then you are viewing the tree like this instead of in terms of the recursive definition of the tree.

Tree equality /** Return true iff tree t equals tree s –meaning they have the same shape and same values in their corresponding nodes. */ public static boolean equals(Node s, Node t) { if (s == null || t == null) return s == t; return s.value == t.value && equals(s.left, t.left) && equals(s.right, t.right); 11 What is the base case? At least one of s and t is null root A tree is empty (null) or root s t

Reverse values in a doubly linked list /** Reverse the values in the nodes. * First and last point to first and last nodes to be reversed */ public static void rev(Node first, Node last) 12 first last objects of class Node. Draw only what is necessary for understanding

Reverse values in a doubly linked list /** Reverse the values in the nodes. * First and last point to first and last nodes to be reversed */ public static void rev(Node first, Node last) if (first == last) return; first last Base cases: (list is empty) and 5 firstlast

Reverse values in a doubly linked list /** Reverse the values in the nodes. * First and last point to first and last nodes to be reversed */ public static void rev(Node first, Node last) if (first == last) return; Swap first.value and last.value; if (first.next == last) return; rev(first.next, last.prev); first last Be careful. If list has only two values, reversing is done

Recursion over a data structure 15 This is not a matter of learning facts. It is a matter of how you approach a new problem, how you think about it, develop a solution. To do such problems well, you have to: Read specification Deal with base cases Use the recursive definition of the data structure Keep things simple –don’t complicate Draw diagrams, pictures. Changing a pattern of thinking requires consciously applying good strategies, principles