Discussion 2: PA1 Siarhei Vishniakou CSE 100 Summer 2014.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Chapter 12 Binary Search Trees
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Chapter 4: Trees Part II - AVL Tree
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
BST Data Structure A BST node contains: A BST contains
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Binary Search Trees Chapter 6.
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Binary Search Trees III Delete Chapter 6. 2 Objectives You will be able to write code to delete a node from a Binary Search Tree.
Tree.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
A Binary Search Tree Binary Search Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
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.
CS 361 – Chapter 3 Sorted dictionary ADT Implementation –Sorted array –Binary search tree.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
CSCS-200 Data Structure and Algorithms Lecture
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Reading data into sorted list Want to suck text file in and produce sorted list of the contents: Option 1 : read directly into array based list, sort afterwards.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
ENEE150 Discussion 13 Section 0101 Adam Wang.
Tonga Institute of Higher Education
Binary Search Trees Why this is a useful data structure. Terminology
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Chapter 21: Binary Trees.
Chapter 8 – Binary Search Tree
Chapter 16 Tree Implementations
Chapter 12: Binary Search Trees
Binary Trees, Binary Search Trees
Podcast Ch18d Title: Binary Search Tree Iterator
Yan Shi CS/SE 2630 Lecture Notes
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Presentation transcript:

Discussion 2: PA1 Siarhei Vishniakou CSE 100 Summer 2014

Files for PA1 BST.hpp – Represents entire binary search tree – Contains pointer to root node and a bunch of methods BSTIterator.hpp – Represents an iterator over the binary search tree BSTNode.hpp – Represents a single node of binary search tree – Contains pointers to left child, right child, and parent, and contains a Data object – Also has successor() method test_BST.cpp – Main script used for testing Makefile

Understanding the code Start with test_BST.cpp – See how the tree and iterators are being used Look at BSTIterator, BSTNode, BST Implementation (my recommendation) – BSTIterator, BST, BSTNode, in that order successor function is hardest in my opinion

Inorder traversal Elements of the BST should be accessed in increasing arithmetic order Insert order: – 3, 4, 1, 100, -33 Order should be – -33, 1, 3, 4,

Another example Insert order: – 30, 20, 70, 10, 50, 40, 60 Traversal in-order should be: – 10, 20, 30, 40, 50, 60,

BST.hpp insert, find : return iterator – Create a new iterator every time you return Code is almost identical!

Returning std::pair virtual std::pair insert(const Data& item) An std::pair provides a way to return two arguments from one function To return an std::pair: return std::make_pair(iterator(node), true);

Iterators An iterator object has: – A pointer to the object that it is iterating over – A ++ operator so that you can go on to next object That’s about it Doesn’t really add any new functionality Provides a consistent way to iterate over objects

Iterators There is no object that contains a collection of references to all of the created nodes BST: – Contains pointer to root node BSTNode: – Contains pointers to left, right children and parent BSTIterator: – Contains ++ method and a reference to current object

Iterators Example traversal with iterators: When ++it executes, successor() function is called on the current node Current node starts at the node with the minimum Data value b.end returns a null pointer cout << "simple traversal test:" << endl; for(BST ::iterator it = b.begin(); it != b.end(); ++it) { cout << *it << "," << endl; }

Iterators You don’t really NEED iterators Instead of,you could iterate over nodes directly by calling “successor” method until it returns a null pointer You would have to modify few return types and protection levels for some variables in this homework Iterators are a convenience feature cout << "simple traversal test:" << endl; for(BST ::iterator it = b.begin(); it != b.end(); ++it) { cout << *it << "," << endl; }

Iterators An iterator does not know – What is a root node – Where the iteration has been – How many more times you can iterate An iterator does know – What is the current object being examined – What should be the next object to be examined

Tips on homework test_BST is good, but write your own test code there as well Add lots of cout statements Example: just print out the tree, without any error checks Segmentation fault – most likely you are trying to access a null pointer Infinite loop – your tree traversal is incorrect – Add return statements to break up the function and see which piece of code causes it

gdb Use gdb if segmentation fault Turn off compiler optimizations: Change CXX flag from –O2 to –O0 in the Makefile

BSTIterator constructor Use keyword “this” to access the fields of current object This will allow you to distinguish between input argument and class field

Destructors I have not tested this, but this is how I would do it: Add a destructor for BSTNode – Inside, delete left and right children if exist* – According to Kacy, it’s OK to delete nullptr For clear method for BST: – Delete root node, reset size Destructor for BST: – Call “clear”

Updated: virtual? If destructor is virtual, then subclasses can override the destructor for proper cleanup In the case of BSTNode for the current homework, it does not matter if destructor is declared to be virtual If there are plans to inherit from BSTNode in future classes, however, the destructor should be declared virtual to allow override and therefore proper deletion of future subclass objects In short, yes, make it virtual in case there is a subclass

BST functions: insert Insert(new_data) If no root yet, insert at root Current = root While current.data not equal to new_data – If new_data < current.data If left of current is null – Insert here, return left of current (newly created) Else – Current = left of current – Similar for current.data < new_data Return current

BST functions: find Almost exactly like previous slide!

BST functions: begin Needs to return the smallest (<) element in the tree Return leftmost child, starting from the root node (probably a while loop)

BSTNode function: successor Few cases to consider Does it have a right child? – If so, keep taking lefts of it No right child: Has a parent? – If not, this is the max element If it is left child of parent, – parent is the successor If it is right child of parent: – Keep going up until hit a left child link

Return typename Why is the reason to use “typename”? What is the difference between line 150 and 151? Either one compiles and runs correctly for me I will post this question on piazza, perhaps other TA’s can clarify

Finally There are tons of resources online – Lecture notes from other schools – Applets and web apps for visualization – Even source code Use them! This is how you would be doing things at your job in the future