Traversing Trees with Iterators
|
|
- Ashlyn Patterson
- 6 years ago
- Views:
Transcription
1 Steven J. Zeil June 25, 2013 Contents 1 Iterating over Trees begin() operator Iterators using Parent Pointers Basic operations
2 2.2 begin() and end() operator Implementing operator Threads 32 CS361 2
3 The recursive traversal algorithms work well for implementing tree-based ADT member functions, but if we are trying to hide the trees inside some ADT (e.g., std::set), we may need to provide iterators for walking though the contents of the tree. Iterators for tree-based data structures can be more complicated than those for linear structures. CS361 3
4 For arrays (and vectors and deques and other array-like structures) and linked lists, a single pointer can implement an iterator: 3 Given the current position, it is easy to move forward to the next element. For anything but a singly-linked list, we can also easily move backwards. 1 Iterating over Trees Adams Baker Chen current current CS361 4
5 But look at this tree, and suppose that you were implementing tree iterators as a single pointer. Let s see if we can 30 "think" our way through the process of traversing this tree, one step at a time, without needing to keep a whole stack of 20 unfinished recursive calls around. It s not immediately obvious what our data structure for storing our "current position" (i.e., an iterator) will be. We might suspect that a pointer to a tree node will be part or 40 whole of that data structure, in only because that worked for us with iterators over linked lists. With that in mind,... Question: How would you implement begin()? CS361 5
6 1.1 begin() 30 We find the begin() position by starting from the root and working our way down, always taking left children, until we come to a node with no left child That wasn t so hard. Careful, now. Question: How would you implement end()? CS361 6
7 Probably by returning a null pointer. It s tempting to guess that you could do much the same as for begin(), this time seeking out the rightmost node. But that would leave you pointing to the last node in the tree, and end(), for any container, is supposed to denote the position after the last element in the container operator++ CS361 7
8 30 Now it get s trickier. Suppose you are still trying to implement iterators using a single pointer, you have one such pointer named current as shown in the figure Question: How would you implement ++current? current CS361 8
9 You can t, not with just a pointer to the node and all the nodes pointing only to their children. The only place you can go within this tree is down, and there is no down from our current position. In a binary tree, to do operator++. current We need to know not only where we are, but also how we got here. One way is to do that is to implement the iterator as a stack of pointers containing the path to the current node. In essence, we would use the stack to simulate the activation stack during a recursive traversal, but that s pretty clumsy. Iterators tend to get assigned (copied) a lot, and we d really like that to be an O(1) operation. Having to copy an entire stack of pointers just isn t very attractive. CS361 9
10 CS361 10
11 2 Iterators using Parent Pointers template <typename T> class stnode { public: // stnode is used to implement the binary search tree class // making the data public simplifies building the We class can make functions the task of creating tree T nodevalue; // node data stnode<t> *left, *right, *parent; iterators much easier if we redesign the tree nodes to // child pointers and pointer to the node s parent add pointers from each node to its // constructor parent. stnode (const T& item, stnode<t> *lptr = NULL, stnode<t> *rptr = NULL, stnode<t> *pptr = NULL): nodevalue(item), left(lptr), right(rptr), parent(pptr) {} }; CS361 11
12 These nodes are then used to implement a tree class, which keeps track of the root of our tree in a data member. template <typename T> class stree { public: typedef stree_iterator<t> iterator; typedef stree_const_iterator<t> const_iterator; stree(); // constructor. initialize root to NULL and size to 0. iterator find(const T& item); // search for item. if found, return an iterator pointing CS361 12
13 // at it in the tree; otherwise, return end() const_iterator find(const T& item) const; // constant version. iterator begin(); // return an iterator pointing to the first item // inorder const_iterator begin() const; // constant version iterator end(); // return an iterator pointing just past the end of // the tree data const_iterator end() const; // constant version private: CS361 13
14 stnode<t> *root;. stnode<t> *findnode(const T& item) const; // search for item in the tree. if it is in the tree, // return a pointer to its node; otherwise, return NULL. // used by find() and erase() friend class stree_iterator<t>; friend class stree_const_iterator<t>; // allow the iterator classes to access the private section // of stree }; 2.1 Basic operations Here s the basic declaration for an iterator to do in-order traversals. template <typename T> class s t r e e _ i t e r a t o r CS361 14
15 { friend class stree <T>; friend class stree_const_iterator <T>; public : / / constructor s t r e e _ i t e r a t o r ( ) { } / / comparison operators. j u s t compare node pointers bool operator== ( const s t r e e _ i t e r a t o r& rhs ) const ; bool operator!= ( const s t r e e _ i t e r a t o r& rhs ) const ; / / dereference operator. return a reference to / / the value pointed to by nodeptr T& operator * ( ) const ; / / preincrement. move forward to next l a r g e r value CS361 15
16 s t r e e _ i t e r a t o r& operator++ ( ) ; / / postincrement s t r e e _ i t e r a t o r operator++ ( int ) ; / / predecrement. move backward to l a r g e s t value < current value s t r e e _ i t e r a t o r& operator ( ) ; / / postdecrement s t r e e _ i t e r a t o r operator ( int ) ; private : / / nodeptr i s the current location in the t r e e. we can move / / f r e e l y about the t r e e using l e f t, right, and parent. / / t r e e i s the address of the s t r e e object associated / / with t h i s i t e r a t o r. i t i s used only to access the / / root pointer, which i s needed f o r ++ and / / when the i t e r a t o r value i s end ( ) stnode<t> * nodeptr ; CS361 16
17 stree <T> * tree ; / / used to construct an i t e r a t o r return value from / / an stnode pointer s t r e e _ i t e r a t o r ( stnode<t> *p, stree <T> * t ) : nodeptr (p ), tree ( t ) { } } ; You will note that the public interface is pretty much a standard iterator. The private section declares a pair of pointers. One points to the three that we are walking through. The other points to the node denoting our current position within that tree. 2.2 begin() and end() As noted earlier, begin() works by finding the leftmost node in the tree, and end() uses a null pointer. CS361 17
18 template <typename T> class stree { public:. iterator begin(); // return an iterator pointing to the first item // inorder iterator end(); // return an iterator pointing just past the end of // the tree data. private: stnode<t> *root; // pointer to tree root CS361 18
19 . }; int treesize; // number of elements in the tree. template <typename T> typename stree<t>::iterator stree<t>::begin() { stnode<t> *curr = root; // if the tree is not empty, the first node // inorder is the farthest node left from root if (curr!= nullptr) while (curr->left!= nullptr) curr = curr->left; CS361 19
20 } // build return value using private constructor return iterator(curr, this); template <typename T> typename stree<t>::iterator stree<t>::end() { // end indicated by an iterator with nullptr stnode pointer return iterator(nullptr, this); } 2.3 operator++ CS361 20
21 A Before trying to write the code for this iterator s operator++, let s try to figure out just what it should do. D B E C F G Question: Suppose that we are currently at node E. What is the in-order successor (the node that comes next during an in-order traversal) of E? CS361 21
22 A G is the in-order successor of E. (If you answered F, remember that in an in-order traversal, we visit a node only after visiting all of its left descendents and before visiting any of its right descendents. Since we re at E, we must have already visited F.) D B E C F G The previous example suggests that a node s in-order successor tends to be among its right descendents. Let s explore that idea further. Question: Suppose that we are currently at node A. What is the in-order successor (the node that comes next during an in-order traversal) of A? CS361 22
23 F is the in-order successor of A. If we are at A during an in-order traversal, we have already visited all of A s left descendents. So the answer has to be C or one of its descendents. It s tempting to B pick C because it s only one step away from A. But, remember, during an in-order traversal, we visit a node only after visiting all of its left descendents and before D visiting any of its right descendents. We have not yet visited C s left descendents. So have to run down from F C to the left as far as we can go. This suggests that, if a node has any right descendents, we should Take a step down to the right, then Run as far down to the left as we can. A E C G You can see how this would take us from A to F. But that "step to the right, then run left" procedure raises a new question. What happens if we are at a node with no right descendents? CS361 23
24 Question: Suppose that we are currently at node C. What is the in-order successor of C? CS361 24
25 C does not have an in-order successor. C is actually the final node in an in-order traversal. After C is only end(). OK, that s an interesting special case, but it doesn t make clear what should happen in the more general case where we have no right child. D B A E C Question: What is the in-order successor of F? F G CS361 25
26 A E is the in-order successor of F. So, when we have no right child, we may need to move back up in the tree. D B E C Question: What is the in-order successor of G? F G CS361 26
27 C is the in-order successor of G. Why did we move up two steps in the tree this time, A when from F we only moved up one step? The answer lies in whether we moved back up over a left-child edge or a right-child edge. If we move up over a right-child edge, we re returning B C to a node that has already had all of its descendents, left and right, visited. So we must have already visited D E this node as well, otherwise we would never have made it into its right descendents. F G If we move up over a left-child edge, we re returning to a node that has already had all of its left descendents visited but none of its right descendents. That s the definition of when we want to visit a node during an in-order traversal, so it s time to visit this node. So, if a node has no right child, we move up in the tree (following the parent pointers) until we move back over a left edge. Then we stop. Notice that, applying this procedure to C, we would move up to A (right edge), then try to move up again to A s parent. But since A is the tree root, it s parent CS361 27
28 pointer will be null, which is our signal that C has no in-order successor Implementing operator++ To summarize, If the current node has a non-null right child, Take a step down to the right Then run down to the left as far as possible If the current node has a null right child, move up the tree until we have moved over a left child link With that in mind, the operator++ code should be easily :-) understood. template <typename T> s t r e e _ i t e r a t o r <T>& s t r e e _ i t e r a t o r <T > : : operator++ ( ) { CS361 28
29 stnode<t> *p ; i f ( nodeptr == nullptr ) { / / ++ from end ( ). get the root of the t r e e nodeptr = tree >root ; / / error! ++ requested for an empty t r e e i f ( nodeptr == nullptr ) throw underflowerror ( " stree i t e r a t o r operator++ ( ) : tree empty" ) ; / / move to the smallest value in the tree, / / which i s the f i r s t node inorder while ( nodeptr > l e f t!= nullptr ) { nodeptr = nodeptr > l e f t ; } } else i f ( nodeptr >r i g h t!= nullptr ) CS361 29
30 { / / successor i s the f u r t hest l e f t node of / / right subtree nodeptr = nodeptr >r i g h t ; while ( nodeptr > l e f t!= nullptr ) { nodeptr = nodeptr > l e f t ; } } else { / / have already processed the l e f t subtree, and / / there i s no r i g h t subtree. move up the tree, / / looking for a parent for which nodeptr i s a l e f t child, / / stopping i f the parent becomes NULL. a non NULL parent / / i s the successor. i f parent i s NULL, the original node / / was the l a s t node inorder, and i t s successor / / i s the end of the l i s t p = nodeptr >parent ; while (p!= nullptr && nodeptr == p >r i g h t ) CS361 30
31 { } nodeptr = p ; p = p >parent ; } / / i f we were previously at the right most node in / / the tree, nodeptr = nullptr, and the i t e r a t o r s p e c i f i e s / / the end of the l i s t nodeptr = p ; return * this ; } You can run these iterator algorithms here. A similar process of analysis would eventually lead us to an implementation of operator--. CS361 31
32 3 Threads Another approach to supporting iteration is threading: Threaded trees replace all null right pointers by a thread (pointer) to that node s in-order successor. 10 need a boolean flag to tell if the right current pointer is a child or a thread Can also thread the null left pointers to allow operator-- CS361 32
Traversing Trees with Iterators
Steven J. Zeil June 25, 2013 Contents 1 Iterating over Trees 3 1.1 begin()................................................................ 5 1.2 operator++..............................................................
More informationthe pointer range [first, last) into the tree
1 #ifndef BINARY_SEARCH_TREE_CLASS 2 #define BINARY_SEARCH_TREE_CLASS 3 4 #ifndef NULL 5 #include 6 #endif // NULL 7 8 #include // for setw() 9 #include // for format conversion
More informationBinary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...
Steven J. Zeil July 11, 2013 Contents 1 Definition: Binary Search Trees 2 1.1 The Binary Search Tree ADT.................................................... 3 2 Implementing Binary Search Trees 7 2.1 Searching
More informationAdvanced Data Structures and Algorithms
Advanced Data Structures and Algorithms CS 361 - Fall 2013 Lec. #07: Trees & Binary Search Trees Tamer Nadeem Dept. of Computer Science Class Objective/Overview Familiarize with Trees and Related Terminologies
More informationTitle Description Participants Textbook
Podcast Ch18d Title: Binary Search Tree Iterator Description: Additional operations first and last; the BST iterator Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook:
More informationTutorial AVL TREES. arra[5] = {1,2,3,4,5} arrb[8] = {20,30,80,40,10,60,50,70} FIGURE 1 Equivalent Binary Search and AVL Trees. arra = {1, 2, 3, 4, 5}
1 Tutorial AVL TREES Binary search trees are designed for efficient access to data. In some cases, however, a binary search tree is degenerate or "almost degenerate" with most of the n elements descending
More informationChapter 5. Binary Trees
Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children
More informationTrees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.
Chapter 6 Trees In a hash table, the items are not stored in any particular order in the table. This is fine for implementing Sets and Maps, since for those abstract data types, the only thing that matters
More informationChapter 20: Binary Trees
Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other
More informationTree Travsersals and BST Iterators
Tree Travsersals and BST Iterators PIC 10B May 25, 2016 PIC 10B Tree Travsersals and BST Iterators May 25, 2016 1 / 17 Overview of Lecture 1 Sorting a BST 2 In-Order Travsersal 3 Pre-Order Traversal 4
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 informationa 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 informationWe have the pointers reference the next node in an inorder traversal; called threads
Leaning Objective: In this Module you will be learning the following: Threaded Binary Tree Introduction: Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked
More informationComputer Science 210 Data Structures Siena College Fall Topic Notes: Trees
Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,
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 informationAlgorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs
Algorithms in Systems Engineering ISE 172 Lecture 16 Dr. Ted Ralphs ISE 172 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms
More informationCS302 - Data Structures using C++
CS302 - Data Structures using C++ Topic: Implementation Kostas Alexis s Definition: A (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction
More informationCSC212. Data Structure. Lecture 13 Trees and Tree Traversals. Instructor: Prof. George Wolberg Department of Computer Science City College of New York
CSC212 Data Structure Lecture 13 Trees and Tree Traversals Instructor: Prof. George Wolberg Department of Computer Science City College of New York Motivation Linear structures arrays dynamic arrays linked
More informationTrees, Part 1: Unbalanced Trees
Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more
More informationBinary Trees and Binary Search Trees
Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe
More informationComputational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs
Computational Optimization ISE 407 Lecture 16 Dr. Ted Ralphs ISE 407 Lecture 16 1 References for Today s Lecture Required reading Sections 6.5-6.7 References CLRS Chapter 22 R. Sedgewick, Algorithms in
More informationBinary Search Trees. See Section 11.1 of the text.
Binary Search Trees See Section 11.1 of the text. Consider the following Binary Search Tree 17 This tree has a nice property: for every node, all of the nodes in its left subtree have values less than
More informationECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211
ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B 2011-02-15/13:30-14:50 MC-4021/RCH-211 Instructions: There are 63 marks. It will be marked out of 55. No aides. Turn off all electronic media
More informationBinary Trees. Height 1
Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.
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 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 informationCSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I
Review from Lecture 16 CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.
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 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 informationCSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)
CSE100 Advanced Data Structures Lecture 4 (Based on Paul Kube course materials) Lecture 4 Binary search trees Toward a binary search tree implementation using C++ templates Reading: Weiss Ch 4, sections
More informationCSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I
CSCI-1200 Data Structures Spring 2018 Lecture 16 Trees, Part I Review from Lecture 15 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.
More informationCSE 12 Abstract Syntax Trees
CSE 12 Abstract Syntax Trees Compilers and Interpreters Parse Trees and Abstract Syntax Trees (AST's) Creating and Evaluating AST's The Table ADT and Symbol Tables 16 Using Algorithms and Data Structures
More informationBinary Trees
Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what
More informationSCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad
SCJ2013 Data Structure & Algorithms Binary Search Tree Nor Bahiah Hj Ahmad Binary Search Tree A binary search tree has the following properties: For every node n in the tree Value of n is greater than
More informationMULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR
STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 07 / 15 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? Binary trees implementation Binary search trees Michael Eckmann - Skidmore College
More informationCSE 100: C++ TEMPLATES AND ITERATORS
1 CSE 100: C++ TEMPLATES AND ITERATORS 2 Announcements Look out for the extra weekend section More on git and C++ (iterators) Live demo by your friendly tutors Not mandatory but it will be fun Bring your
More informationA set of nodes (or vertices) with a single starting point
Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of
More informationLecture 34. Wednesday, April 6 CS 215 Fundamentals of Programming II - Lecture 34 1
Lecture 34 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture33/*.* In order to compile these files, also need bintree.h from last class. Project 7 posted. Due next week Friday, but
More informationAbstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department
Abstract Data Structures IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational
More informationAn abstract tree stores data that is hierarchically ordered. Operations that may be performed on an abstract tree include:
4.2 Abstract Trees Having introduced the tree data structure, we will step back and consider an Abstract Tree that stores a hierarchical ordering. 4.2.1 Description An abstract tree stores data that is
More informationCSE 100: C++ TEMPLATES AND ITERATORS
CSE 100: C++ TEMPLATES AND ITERATORS Announcements iclickers: Please register at ted.ucsd.edu. Start ASAP!! For PA1 (Due next week). 10/6 grading and 10/8 regrading How is Assignment 1 going? A. I haven
More informationCSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I
CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I Review from Lectures 17 Maps containing more complicated values. Example: index mapping words to the text line numbers on which they appear.
More informationBinary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree
0/2/ Preview Binary Tree Tree Binary Tree Property functions In-order walk Pre-order walk Post-order walk Search Tree Insert an element to the Tree Delete an element form the Tree A binary tree is a tree
More informationCSE 100: C++ TEMPLATES AND ITERATORS
CSE 100: C++ TEMPLATES AND ITERATORS Announcements Gradesource and clickers: We ll be making one more pass for unregistered clickers tonight, but after that you ll be on your own How is Assignment 1 going?
More informationTree traversals and binary trees
Tree traversals and binary trees Comp Sci 1575 Data Structures Valgrind Execute valgrind followed by any flags you might want, and then your typical way to launch at the command line in Linux. Assuming
More informationLecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson
Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement
More informationFinal exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)
Review Final exam Final exam will be 12 problems, drop any 2 Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers) 2 hours exam time, so 12 min per problem (midterm 2 had
More informationBinary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management
Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from
More informationBinary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals
Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the
More informationTitle Description Participants Textbook
Podcast Ch18c Title: BST delete operation Description: Deleting a leaf; deleting a node with one nonnull child; deleting a node with two nonnull children Participants: Barry Kurtz (instructor); John Helfert
More informationGraphs ADT and Traversing
Steven J. Zeil July 31, 2013 Contents 1 A Graph ADT 3 1.1 Vertex...................................... 4 1.2 Edge....................................... 7 1.3 DiGraph....................................
More informationTrees. Carlos Moreno uwaterloo.ca EIT https://ece.uwaterloo.ca/~cmoreno/ece250
Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250 Today's class: We'll discuss one possible implementation for trees (the general type of trees) We'll look at tree
More informationMID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.
MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions
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 informationTREES Lecture 10 CS2110 Spring2014
TREES Lecture 10 CS2110 Spring2014 Readings and Homework 2 Textbook, Chapter 23, 24 Homework: A thought problem (draw pictures!) Suppose you use trees to represent student schedules. For each student there
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 informationCSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III
CSCI-1200 Data Structures Spring 2018 Lecture 18 Trees, Part III Review from Lecture 16 & 17 Overview of the ds set implementation begin, find, destroy_tree, insert In-order, pre-order, and post-order
More informationTest 1: CPS 100. Owen Astrachan. February 23, 2000
Test 1: CPS 100 Owen Astrachan February 23, 2000 Name: Login: Honor code acknowledgment (signature) Problem 1 value 19 pts. grade Problem 2 24 pts. Problem 3 8 pts. Problem 4 9 pts. Problem 5 6 pts. TOTAL:
More information2. (5 Points) When calculating a combination (n choose k) the recursive definition is n k =1 if
COSC 320 Exam 1 Key Spring 2011 Part 1: Recursion 1. (5 Points) Write a recursive function for calculating the factorial of a non-negative integer. long factorial(long n) if (n == 0) else return n * factorial(n-1);
More informationHash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell
Hash Tables CS 311 Data Structures and Algorithms Lecture Slides Wednesday, April 22, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees
More informationCSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III
CSCI-1200 Data Structures Spring 2015 Lecture 20 Trees, Part III Review from Lecture 18 & 19 Overview of the ds set implementation begin, find, destroy_tree, insert In-order, pre-order, and post-order
More informationTrees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file.
Large Trees 1 Trees can be used to store entire records from a database, serving as an in-memory representation of the collection of records in a file. Trees can also be used to store indices of the collection
More informationCS 206 Introduction to Computer Science II
CS 206 Introduction to Computer Science II 02 / 24 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? Trees binary trees two ideas for representing them in code traversals start binary
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 informationSection 1: True / False (2 points each, 30 pts total)
Section 1: True / False (2 points each, 30 pts total) Circle the word TRUE or the word FALSE. If neither is circled, both are circled, or it impossible to tell which is circled, your answer will be considered
More informationfrom 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 informationDiscussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let
More informationLinked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4
Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 4 1 2 Coding for Linked Lists 8 2.1 Traversing a Linked List............................... 12 2.2 Searching a Linked List................................
More informationAdvanced Java Concepts Unit 5: Trees. Notes and Exercises
Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will
More informationDynamic Data Structures
Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To
More informationLecture 23: Binary Search Trees
Lecture 23: Binary Search Trees CS 62 Fall 2017 Kim Bruce & Alexandra Papoutsaki 1 BST A binary tree is a binary search tree iff it is empty or if the value of every node is both greater than or equal
More informationTREES Lecture 12 CS2110 Spring 2018
TREES Lecture 12 CS2110 Spring 2018 Important Announcements 2 A4 is out now and due two weeks from today. Have fun, and start early! Data Structures 3 There are different ways of storing data, called data
More informationMARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012
MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)
More informationCSE100. Advanced Data Structures. Lecture 13. (Based on Paul Kube course materials)
CSE100 Advanced Data Structures Lecture 13 (Based on Paul Kube course materials) CSE 100 Priority Queues in Huffman s algorithm Heaps and Priority Queues Time and space costs of coding with Huffman codes
More informationCS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux
CS 231 Data Structures and Algorithms Fall 2018 Recursion and Binary Trees Lecture 21 October 24, 2018 Prof. Zadia Codabux 1 Agenda ArrayQueue.java Recursion Binary Tree Terminologies Traversal 2 Administrative
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 informationBinary Search Tree (3A) Young Won Lim 6/4/18
Binary Search Tree (A) /4/1 Copyright (c) 2015-201 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2
More informationGarbage Collection: recycling unused memory
Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an
More information- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013
CS106B Spring 2013 Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions Based on handouts by Eric Roberts and Jerry Cain Problem One: Reversing a Queue One way to reverse the queue is to keep
More informationLinked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3
Steven J. Zeil July 31, 2013 Contents 1 Linked Lists: the Basics 3 2 Coding for Linked Lists 7 2.1 Traversing a Linked List........................... 10 2.2 Searching a Linked List............................
More informationCSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III
CSCI-1200 Data Structures Fall 2018 Lecture 19 Trees, Part III Review from Lecture 17 & 18 Overview of the ds set implementation begin, find, destroy_tree, insert In-order, pre-order, and post-order traversal;
More informationBinary Search Tree (3A) Young Won Lim 6/2/18
Binary Search Tree (A) /2/1 Copyright (c) 2015-201 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2
More informationINF2220: algorithms and data structures Series 1
Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:
More informationFORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE
TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)
More informationRecursion. Contents. Steven Zeil. November 25, Recursion 2. 2 Example: Compressing a Picture 4. 3 Example: Calculator 5
Steven Zeil November 25, 2013 Contents 1 Recursion 2 2 Example: Compressing a Picture 4 3 Example: Calculator 5 1 1 Recursion Recursion A function is recursive if it calls itself or calls some other function
More informationUpcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:
Upcoming ACM Events Linux Crash Course Date: Oct. 2nd and 3rd Time: 1:15 pm - 3:15 pm Location: UW1-210 (10/02) and UW1-221 (10/03) Weekly Crack the Coding Interview Date: Weekly Fridays from Oct. 5th
More informationTREES Lecture 12 CS2110 Fall 2016
TREES Lecture 12 CS2110 Fall 2016 Prelim 1 tonight! 2 5:30 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal 5:30 prelim (not the quiet room) and whose last names
More informationChapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1
Chapter 12: Searching: Binary Trees and Hash Tables Exercises 12.1 1. 4, 6 2. 4, 1, 2, 3 3. 4, 6, 5 4. 4, 1, 0 5. 4, 6, 7, 8 6. template linearsearch(elementtype x[], ElementType
More informationSTUDENT LESSON AB30 Binary Search Trees
STUDENT LESSON AB30 Binary Search Trees Java Curriculum for AP Computer Science, Student Lesson AB30 1 STUDENT LESSON AB30 Binary Search Trees INTRODUCTION: A binary tree is a different kind of data structure
More informationCS302 - 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 informationMulti-Way Search Tree
Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) Rule: Number of children = 1
More informationCSCI Trees. Mark Redekopp David Kempe
CSCI 104 2-3 Trees Mark Redekopp David Kempe Trees & Maps/Sets C++ STL "maps" and "sets" use binary search trees internally to store their keys (and values) that can grow or contract as needed This allows
More informationCMSC351 - Fall 2014, Homework #2
CMSC351 - Fall 2014, Homework #2 Due: October 8th at the start of class Name: Section: Grades depend on neatness and clarity. Write your answers with enough detail about your approach and concepts used,
More informationLECTURE 11 TREE TRAVERSALS
DATA STRUCTURES AND ALGORITHMS LECTURE 11 TREE TRAVERSALS IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD BACKGROUND All the objects stored in an array or linked list can be accessed sequentially
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 informationBST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams
Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root
More information// a stack is printed from bottom (leftmost) to top (rightmost) System.out.println(stk);
CompSci 100 Test 2 Spring 2011 Prof. Rodger April 14, 2011 Some common recurrence relations T(n) = T(n/2) +O(1) O(log n) T(n) = T(n/2) +O(n) O(n) T(n) = 2T(n/2) +O(1) O(n) T(n) = 2T(n/2) +O(n) O(n log
More informationDiscussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:
Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked
More information