DATA STRUCTURE AND ALGORITHM USING PYTHON
|
|
- Corey Garrett
- 5 years ago
- Views:
Transcription
1 DATA STRUCTURE AND ALGORITHM USING PYTHON Advanced Data Structure and File Manipulation Peter Lo
2 Linear Structure Queue, Stack, Linked List and Tree 2
3 Queue A queue is a line of people or things waiting to be handled, usually in sequential order starting at the beginning or top of the line or sequence. In computer technology, a queue is a sequence of work objects that are waiting to be processed. 3
4 Queue (Cont.) Queue has two main operations in data structure: Enqueue: append an element to the tail of the queue Dequeue: remove an element from the head of the queue With a queue you remove the item least recently added (First-In First-Out / FIFO) 4
5 Implement Queue by List It s possible to use a regular list as a queue but this is not ideal from a performance perspective. Lists are quite slow for this purpose because inserting or deleting an element at the beginning requires shifting all of the other elements by one. Using a list as a makeshift queue in Python is not recommend unless you re dealing with a small number of elements only. 5
6 Implement Queue by List 6
7 Implement Queue by collections.deque The deque class implements a double-ended queue that supports adding and removing elements from either end. Python s deque objects are implemented as doubly-linked lists which gives them excellent performance for enqueuing and dequeuing elements, but poor performance for randomly accessing elements in the middle of the queue. Because deques support adding and removing elements from either end equally well, they can serve both as queues and as stacks. collections.deque is a great default choice if you re looking for a queue data structure in Python s standard library. 7
8 Implement Queue by collections.deque 8
9 Implement Queue by queue.queue This queue implementation in the Python standard library is synchronized and provides locking semantics to support multiple concurrent producers and consumers. The queue module contains several other classes implementing multi-producer, multi-consumer queues that are useful for parallel computing. Depending on your use case the locking semantics might be helpful, or just incur unneeded overhead. In this case you d be better off with using collections.deque as a general purpose queue. 9
10 Implement Queue by queue.queue 10
11 Stack A Stack is an ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. This end is commonly referred to as the top. The end opposite the top is known as the base 11
12 Stack (Cont.) Stack has two main operations in data structure: Push: append an element on top of the stack Pop: remove an element from the top of the stack With a stack you remove the item most recently added (Last-In First-Out / LIFO). 12
13 Implement Stack by List Python s built-in list type makes a decent stack data structure as it supports push and pop operations in amortized time. Python s lists are implemented as dynamic arrays internally which means they occasional need to resize the storage space for elements stored in them when elements are added or removed. The list over-allocates its backing storage so that not every push or pop requires resizing and you get an amortized time complexity for these operations. The downside is that this makes their performance less consistent than the stable inserts and deletes provided by a linked list based implementation. On the other hand lists do provide fast time random access to elements on the stack which can be an added benefit. 13
14 Implement Stack by List 14
15 Implement Stack by collections.deque The deque class implements a double-ended queue that supports adding and removing elements from either end in non-amortized time. Because deques support adding and removing elements from either end equally well, they can serve both as queues and as stacks. Python s deque objects are implemented as doubly-linked lists which gives them excellent and consistent performance for inserting and deleting elements, but poor performance for randomly accessing elements in the middle of the stack. collections.deque is a great choice if you re looking for a stack data structure in Python s standard library with the performance characteristics of a linked-list implementation. 15
16 Implement Stack by collections.deque 16
17 Implement Stack by queue.lifoqueue This stack implementation in the Python standard library is synchronized and provides locking semantics to support multiple concurrent producers and consumers. The queue module contains several other classes implementing multi-producer, multi-consumer queues that are useful for parallel computing. Depending on your use case the locking semantics might be helpful, or just incur unneeded overhead. In this case you d be better off with using a list or a deque as a general purpose stack. 17
18 Implement Stack by queue.lifoqueue 18
19 Linked List A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer. A linked list consists of nodes Each node consists of a value to another node and a pointer 19
20 Why use Linked Lists? Advantage Disadvantage A linked list saves memory Linear look up time It only allocates the memory required for values to be stored. Linked list nodes can live anywhere in the memory. Each linked list node can be flexibly moved to a different address. When looking for a value in a linked list, you have to start from the beginning of chain, and check one element at a time for a value you re looking for. 20
21 Node Class The basic building block for linked list implementation is node. Each node object must hold at least two pieces of information: The node must contain the list item itself. We call this the data field of the node. Each node must hold a reference to the next node. To construct a node, you need to supply the initial data value for the node. The Node class also includes the usual methods to access and modify the data and the next reference 21
22 Node Class (Cont.) The special Python reference value None play an important role in the Node class and linked list. A reference to None will denote the fact that there is no next node. Note in the constructor that a node is initially created with next set to None. Since this is sometimes referred to as grounding the node, we will use the standard ground symbol to denote a reference that is referring to None. It is always a good idea to explicitly assign None to your initial next reference values 22
23 Linked List Class The linked list will be built from a collection of nodes, each linked to the next by explicit references. Once we find the first node, each item after that can be found by successively following the next links. The Linked List class must maintain a reference to the first node. Note that each list object will maintain a single reference to the head of the list. Initially when we construct a list, there are no items. 23
24 Add Node to Linked List Each item of the list must reside in a node object. Then creates a new node and places the item as its data. Now we must complete the process by linking the new node into the existing structure. 1. Changes the next reference of the new node to refer to the old first node of the list. Now that the rest of the list has been properly attached to the new node. 2. Modify the head of the list to refer to the new node. 24
25 Add Node to Linked List (Cont.) Line 2 creates a new node and places the item as its data. Now we must complete the process by linking the new node into the existing structure. Line 3 changes the next reference of the new node to refer to the old first node of the list. Now that the rest of the list has been properly attached to the new node, we can modify the head of the list to refer to the new node. The assignment statement in line 4 sets the head of the list. 25
26 Linked List Traversal Linked List traversal is the process of systematically visiting each node. We use an external reference that starts at the first node in the list. As we visit each node, we move the reference to the next node by traversing the next reference. 26
27 Count the List Size To implement the size method, we need to traverse the linked list and keep a count of the number of nodes that occurred. The external reference is called current and is initialized to the head of the list in line 2. At the start of the process we have not seen any nodes so the count is set to 0. Lines 4 6 actually implement the traversal. As long as the current reference has not seen the end of the list (None), we move current along to the next node via the assignment statement in line 6. Again, the ability to compare a reference to None is very useful. Every time current moves to a new node, we add 1 to count. Finally, count gets returned after the iteration stops. 27
28 Print the List Node To implement the print method, we need to traverse the linked list and keep a count of the number of nodes that occurred. The external reference is called current and is initialized to the head of the list in line 2. Lines 3 5 actually implement the traversal. As long as the current reference has not seen the end of the list (None), we move current along to the next node via the assignment statement in line 5. Again, the ability to compare a reference to None is very useful. Every time current moves to a new node, we print the value. Finally, whole list printed out after the iteration stops. 28
29 Search in Linked List Searching for a value in a linked list implementation of an unordered list also uses the traversal technique. As we visit each node in the linked list we will ask whether the data stored there matches the item we are looking for. We may not have to traverse all the way to the end of the list. In fact, if we do get to the end of the list, that means that the item we are looking for must not be present. Also, if we do find the item, there is no need to continue. 29
30 Search in Linked List (Cont.) The traversal is initialized to start at the head of the list (line 2). We also use a Boolean variable called found to remember whether we have located the item we are searching for. Since we have not found the item at the start of the traversal, found can be set to False (line 3). The iteration in line 4 takes into account both conditions discussed above. As long as there are more nodes to visit and we have not found the item we are looking for, we continue to check the next node. The question in line 5 asks whether the data item is present in the current node. If so, found can be set to True. 30
31 Remove a Node in Linked List The remove method requires two logical steps. Traverse the list looking for the item that want to remove: starting with an external reference set to the head of the list, we traverse the links until we discover the item we are looking for. Remove the item once we find the item by modifying the link in the previous node so that it refers to the node that comes after current. 31
32 Remove a Node in Linked List (Cont.) Movement of previous and current as they progress down the list looking for the node containing the value
33 Remove a Node in Linked List (Cont.) Lines 2 3 assign initial values to the two references. Note that current starts out at the list head as in the other traversal examples. previous, however, is assumed to always travel one node behind current. For this reason, previous starts out with a value of None since there is no node before the head. The Boolean variable found will again be used to control the iteration. In lines 6 7 we ask whether the item stored in the current node is the item we wish to remove. If so, found can be set to True. If we do not find the item, previous and current must both be moved one node ahead. Again, the order of these two statements is crucial. previous must first be moved one node ahead to the location of current. At that point, current can be moved. This process is often referred to as inch-worming as previous must catch up to current before current moves ahead. 33
34 Tree Structure Binary Tree 34
35 Tree Structure In computer science, a tree is a data structure that is modeled after nature. Unlike trees in nature, the tree data structure is upside down: the root of the tree is on top. A tree consists of nodes and its connections are called edges. The bottom nodes are also named leaf nodes. A tree may not have a cycle. 35
36 Binary Tree A binary tree is a data structure where every node has at most two children (left and right child). The root of a tree is on top. Every node below has a node above known as the parent node. 36
37 Terminology Term Path Root Parent Child Leaf Subtree Level Key Explanation The sequence of nodes along the edges of a tree The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node. Any node except the root node has one edge upward to a node called parent. The node below a given node connected by its edge downward is called its child node. The node which does not have any child node is called the leaf node. Subtree represents the descendants of a node. Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on. Key represents a value of a node based on which a search operation is to be carried out for a node. 37
38 Types of Binary Trees There are three type of Binary Tree 38
39 Full Binary Tree A Binary Tree is full if every node has 0 or 2 children. Following are examples of full binary tree. We can also say a full binary tree is a binary tree in which all nodes except leaves have two children. In a Full Binary tree, number of leaf nodes is number of internal nodes plus 1: L = I + 1 Where L = Number of leaf nodes, I = Number of internal nodes 39
40 Complete Binary Tree A Binary Tree is complete if all levels are completely filled except possibly the last level and the last level has all keys as left as possible 40
41 Perfect Binary Tree A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at same level. A Perfect Binary Tree of height h (where height is number of nodes on path from root to leaf) has 2h 1 node. 41
42 Degenerate Tree A degenerate (or pathological) tree is where each parent node has only one associated child node. This means that performance-wise, the tree will behave like a linked list data structure 42
43 Binary Search Tree Binary Search Tree is a data structure that quickly allows us to maintain a sorted list of numbers. The properties that separates a binary search tree from a regular binary tree is It is called a binary tree because each tree node has maximum of two children. All nodes of left subtree are less than root node All nodes of right subtree are more than root node Both subtrees of each node are also BST (i.e. they have the above two properties) 43
44 Searching in BST A recursive algorithm to search for a key in a BST follows immediately from the recursive structure: If the tree is empty, we have a search miss If the search key is equal to the key at the root, we have a search hit. Otherwise, we search recursively in the appropriate subtree. 44
45 Example ex 45
46 Binary Tree Traversal Traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is no unique traversal. Depth-First Traversal Pre Order Traversal visit the parent first and then left and right children In Order Traversal visit the left child, then the parent and the right child Post Order Traversal visit left child, then the right child and then the parent Breadth-First Traversal Level Order Traversal visits nodes by levels from top to bottom and from left to right 46
47 Example Consider the following tree and its traversals: Pre Order: 8, 5, 9, 7, 1, 12, 2, 4, 11, 3 In Order: 9, 5, 1, 7, 2, 12, 8, 4, 3, 11 Post Order: 9, 1, 2, 12, 7, 5, 3, 11, 4, 8 Level Order: 8, 5, 4, 9, 7, 11, 1, 12, 3, 2 47
48 Create Node Class A new class named Node with 3 attributes is create to represent a tree node: Left node Right node Node s data When you create a node, both left and right node equal to None. Let s create a tree node containing the value 8. 48
49 Insert a Tree Node We need a method to help us populate our tree. This method takes the node s data as an argument and inserts a new node in the tree. 49
50 Example The inset method is called recursively as we are locating the place where to add the new node. Detail Algorithm: Left child for root node "8" is None, attach the new node "3" to it. Right child for root node "8" is None, attach the new node "10" to it. Left child for root node "8" is not None, go to node "3" Left child for root node "3" is None, attach the new node "1" to it. 50
51 Using binarytree Library binarytree ( is a Python library which provides a simple API to generate, visualize, inspect and manipulate binary trees. It allows you to skip the tedious work of setting up test data, and dive straight into practicing your algorithms. Heaps and Binary Search Tree (BST) are also supported. 51
52 Example 52
53 Example (Cont.) 53
54 File Manipulation Open and Read Text File 54
55 File There are two separate types of files that Python handles: Binary file Text files. Knowing the difference between the two is important because of how they are handled. 55
56 Binary File Most files that you use in your computer are binary files. For example, Microsoft Word.doc file is a binary file, even if it has text in it. Other examples of binary files include: Image files including.jpg,.png,.bmp,.gif, etc. Database files including.mdb,.frm, and.sqlite Documents including.doc,.xls,.pdf, and others. That s because these files all have requirements for special handling and require a specific type of software to open it. For example, you need Excel to open an.xls file 56
57 Text File A text file has no specific encoding and can be opened by a standard text editor without any special handling. Every text file must adhere to a set of rules: Text files have to be readable as is. They can contain a lot of special encoding, especially in HTML or other markup languages, but you ll still be able to tell what it says Data in a text file is organized by lines. In most cases, each line is a distinct element, whether it s a line of instruction or a command. Text files have some unseen character at the end of each line which lets the text editor know that there should be a new line. In Python, it is denoted by the \n. 57
58 Comma Separated Value (CSV) Text File A CSV file is a comma separated values file commonly used by spreadsheet programs such as Microsoft Excel. It contains plain text data sets separated by commas with each new line in the CSV file representing a new database row and each database row consisting of one or more fields separated by a comma. 58
59 Fixed-width Text File Data in a fixed-width text file is arranged in rows and columns, with one entry per row. Each column has a fixed width, specified in characters, which determines the maximum amount of data it can contain. No delimiters are used to separate the fields in the file. 59
60 Tab-Separated Values (TSV) Text File A Tab-Separated Values (TSV) file (also called tabdelimited file) is a simple text format for storing data in a tabular structure, e.g., database table or spreadsheet data, and a way of exchanging information between databases. Each record in the table is one line of the text file 60
61 Opening a File in Python In order to open a file for writing or use in Python, you must rely on the built-in open () function. The open() function takes two parameters; filename, and access_mode. The access_mode attribute of a file object tells you which mode a file was opened in. And the filename attribute tells you the name of the file that the file object has opened. 61
62 File Access Modes Mode r rb Function Open file for reading only. Starts reading from beginning of file. This default mode. Open a file for reading only in binary format. Starts reading from beginning of file. r+ Open file for reading and writing. File pointer placed at beginning of the file rb+ w wb Open a file for reading and writing in binary format. File pointer placed at beginning of the file Open file for writing only. File pointer placed at beginning of the file. Overwrites existing file and creates a new one if it does not exists. Same as w but opens in binary mode. w+ Same as w but also allows to read from file. wb+ a ab Same as wb but also allows to read from file. Open a file for appending. Starts writing at the end of file. Creates a new file if file does not exist. Same as a but in binary format. Creates a new file if file does not exist. a+ Same as a but also open for reading. ab+ Same as ab but also open for reading. 62
63 File Access Modes Summary Function r r+ w w+ a a+ Read Write Create new file if not exist Overwrite existing file Pointer place at beginning of file Point place at end of file 63
64 The File Object Attributes Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to file object: Attribute file.closed file.mode file.name Description Returns true if file is closed, false otherwise. Returns access mode with which file was opened Returns name of the file. 64
65 Closing a File in Python When we are done with operations to the file, we need to properly close the file. Closing a file will free up the resources that were tied with the file and is done using Python close() method. Python has a garbage collector to clean up unreferenced objects but, we must not rely on it to close the file. 65
66 Example 66
67 Exception Handling If an exception occurs when we are performing some operation with the file, the code exits without closing the file. 67
68 The try except Block Error handling in Python is done through the use of exceptions that are caught in try blocks and handled in except blocks. If an error is encountered, a try block code execution is stopped and transferred down to the except block. In addition to using an except block after the try block, you can also use the finally block. The code in the finally block will be executed regardless of whether an exception occurs. 68
69 The try except Block ( By using try... finally block, we are guaranteed that the file is properly closed even if an exception is raised, causing program flow to stop. 69
70 Writing to File The write() method writes any string to an open file. It is important to note that Python strings can have binary data and not just text. The write() method does not add a newline character ('\n') to the end of the string. Combine with file access mode, the target file can be overwrite or append 70
71 Example: Insert Data to a New File By using w in access file, the data will be written to a new file (or overwritten) Content in file Texting.txt 71
72 Example: Append Data to Existing File By using a in access file, the data will be appended to existing file Content in file Texting.txt 72
73 Reading from a File To read a file in Python, we must open the file in reading mode. There are several ways to read data from a file. Method read( ) readline( ) readlines( ) Description Return specified number of characters from the file. If omitted it will read the entire contents of the file. Return the next line of the file. Read all the lines as a list of strings in the file 73
74 Read File using readline() method If you want to read a file line by line, as opposed to pulling the content of the entire file at once, then you use the readline() function. 74
75 Read File using readlines() method When you use readlines() for reading the file or document line by line, it will separate each line and present the file in a readable format. 75
76 Read File using read() method If you need to extract a string that contains all characters in the file, you can use the read() method: 76
77 File Pointer Location We can change our current file cursor (position) using the seek() method. Similarly, the tell() method returns our current position (in number of bytes). 77
78 Parsing CSV Files The csv library provides functionality to both read from and write to CSV files. Designed to work out of the box with Excel-generated CSV files, it is easily adapted to work with a variety of CSV formats. The csv library contains objects and other code to read, write, and process data from and to CSV files. 78
79 Reading CSV Files with csv Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python s built-in open() function, which returns a file object. This is then passed to the reader, which does the heavy lifting. 79
80 Reading CSV Files into Dictionary with csv Rather than deal with a list of individual String elements, you can read CSV data directly into a dictionary as well. 80
81 Writing CSV Files with csv You can also write to a CSV file using a writer object and the.write_row() method: 81
82 Writing CSV File from Dictionary with csv Unlike DictReader, the fieldnames parameter is required when writing a dictionary. It also uses the keys in fieldnames to write out the first row as column names. 82
Data Structure. IBPS SO (IT- Officer) Exam 2017
Data Structure IBPS SO (IT- Officer) Exam 2017 Data Structure: In computer science, a data structure is a way of storing and organizing data in a computer s memory so that it can be used efficiently. Data
More informationData Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.
Data Structures Outline Introduction Linked Lists Stacks Queues Trees Introduction dynamic data structures - grow and shrink during execution Linked lists - insertions and removals made anywhere Stacks
More information12 Abstract Data Types
12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define
More informationCS 171: Introduction to Computer Science II. Binary Search Trees
CS 171: Introduction to Computer Science II Binary Search Trees Binary Search Trees Symbol table applications BST definitions and terminologies Search and insert Traversal Ordered operations Delete Symbol
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 informationCS301 - Data Structures Glossary By
CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm
More informationData Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012
Data Abstractions National Chiao Tung University Chun-Jen Tsai 05/23/2012 Concept of Data Structures How do we store some conceptual structure in a linear memory? For example, an organization chart: 2/32
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 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 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 informationAlgorithms and Data Structures
Algorithms and Data Structures PD Dr. rer. nat. habil. Ralf Peter Mundani Computation in Engineering / BGU Scientific Computing in Computer Science / INF Summer Term 2018 Part 2: Data Structures PD Dr.
More informationData Structures I: Linked Lists
Lab 4 Data Structures I: Linked Lists Lab Objective: Analyzing and manipulating data are essential skills in scientific computing. Storing, retrieving, and rearranging data take time. As a dataset grows,
More informationBasic Data Structures (Version 7) Name:
Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)
More information1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.
Unit 6: Binary Trees Part 1 Engineering 4892: Data Structures Faculty of Engineering & Applied Science Memorial University of Newfoundland July 11, 2011 1 Binary trees 1 Binary search trees Analysis of
More informationCourse Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU
Course Review for Midterm Exam 1 Cpt S 223 Fall 2011 1 Midterm Exam 1 When: Friday (10/14) 1:10-2pm Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & in-class
More informationIntroduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree
Chapter 4 Trees 2 Introduction for large input, even access time may be prohibitive we need data structures that exhibit running times closer to O(log N) binary search tree 3 Terminology recursive definition
More information4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING
4.1 COMPUTATIONAL THINKING AND PROBLEM-SOLVING 4.1.2 ALGORITHMS ALGORITHM An Algorithm is a procedure or formula for solving a problem. It is a step-by-step set of operations to be performed. It is almost
More informationSummer Final Exam Review Session August 5, 2009
15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding
More informationData structure and algorithm in Python
Data structure and algorithm in Python Stacks, Queues and Deques Xiaoping Zhang School of Mathematics and Statistics, Wuhan University Table of contents 1. Stacks 2. Queue 3. Double-Ended Queues 1 Stacks
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 informationFundamentals of Data Structure
Fundamentals of Data Structure Set-1 1. Which if the following is/are the levels of implementation of data structure A) Abstract level B) Application level C) Implementation level D) All of the above 2.
More informationChapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn
Chapter 5 Data Structures Algorithm Theory WS 06/ Fabian Kuhn Examples Dictionary: Operations: insert(key,value), delete(key), find(key) Implementations: Linked list: all operations take O(n) time (n:
More information9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology
Introduction Chapter 4 Trees for large input, even linear access time may be prohibitive we need data structures that exhibit average running times closer to O(log N) binary search tree 2 Terminology recursive
More informationOverview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap
Heapsort Submitted by : Hardik Parikh(hjp0608) Soujanya Soni (sxs3298) Overview of Presentation Heap Definition. Adding a Node. Removing a Node. Array Implementation. Analysis What is Heap? A Heap is a
More informationRecursive Data Structures and Grammars
Recursive Data Structures and Grammars Themes Recursive Description of Data Structures Grammars and Parsing Recursive Definitions of Properties of Data Structures Recursive Algorithms for Manipulating
More informationName 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 informationTables, Priority Queues, Heaps
Tables, Priority Queues, Heaps Table ADT purpose, implementations Priority Queue ADT variation on Table ADT Heaps purpose, implementation heapsort EECS 268 Programming II 1 Table ADT A table in generic
More informationLearning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT
CS: Algorithms and Data Structures Lecture # Mind Your Priority Queues Steve Wolfman 0W Learning Goals Provide examples of appropriate applications for priority queues. Describe efficient implementations
More informationCS24 Week 8 Lecture 1
CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes
More informationData Structures Question Bank Multiple Choice
Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution
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 informationCpt S 122 Data Structures. Course Review Midterm Exam # 1
Cpt S 122 Data Structures Course Review Midterm Exam # 1 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Midterm Exam 1 When: Friday (09/28) 12:10-1pm Where:
More informationCSE 373 APRIL 17 TH TREE BALANCE AND AVL
CSE 373 APRIL 17 TH TREE BALANCE AND AVL ASSORTED MINUTIAE HW3 due Wednesday Double check submissions Use binary search for SADict Midterm text Friday Review in Class on Wednesday Testing Advice Empty
More informationAlgorithms. Deleting from Red-Black Trees B-Trees
Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that
More informationCourse Review. Cpt S 223 Fall 2009
Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program
More informationCpt S 223 Fall Cpt S 223. School of EECS, WSU
Course Review Cpt S 223 Fall 2012 1 Final Exam When: Monday (December 10) 8 10 AM Where: in class (Sloan 150) Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes
More informationDATA STRUCTURES AND ALGORITHMS
LECTURE 11 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 10... Hash tables Separate chaining Coalesced chaining Open Addressing Today 1 Open addressing - review
More informationProgramming 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 informationDepartment of Computer Science and Technology
UNIT : Stack & Queue Short Questions 1 1 1 1 1 1 1 1 20) 2 What is the difference between Data and Information? Define Data, Information, and Data Structure. List the primitive data structure. List the
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 informationChapter 9 Abstract Data Types and Algorithms
Chapter Goals Chapter 9 Abstract Data Types and Algorithms Define an abstract data type and discuss its role in algorithm development Distinguish between a data type and a data structure Distinguish between
More informationIntroduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana
Introduction to Indexing 2 Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana Indexed Sequential Access Method We have seen that too small or too large an index (in other words too few or too
More informationFINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard
FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures
More informationCS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial
Week 7 General remarks Arrays, lists, pointers and 1 2 3 We conclude elementary data structures by discussing and implementing arrays, lists, and trees. Background information on pointers is provided (for
More informationCpt S 122 Data Structures. Data Structures
Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation
More informationStackless BVH Collision Detection for Physical Simulation
Stackless BVH Collision Detection for Physical Simulation Jesper Damkjær Department of Computer Science, University of Copenhagen Universitetsparken 1, DK-2100, Copenhagen Ø, Denmark damkjaer@diku.dk February
More informationComputer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving
In Computer Science, an abstract data type (ADT) is a mathematical model for a certain data type or structures. This doesn t mean that the ADTs cannot be programmed. But that we must first understand them
More informationCourse Review. Cpt S 223 Fall 2010
Course Review Cpt S 223 Fall 2010 1 Final Exam When: Thursday (12/16) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program
More informationCSE 373 OCTOBER 11 TH TRAVERSALS AND AVL
CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL MINUTIAE Feedback for P1p1 should have gone out before class Grades on canvas tonight Emails went to the student who submitted the assignment If you did not receive
More informationCOMP 103 RECAP-TODAY. Priority Queues and Heaps. Queues and Priority Queues 3 Queues: Oldest out first
COMP 0 Priority Queues and Heaps RECAP RECAP-TODAY Tree Structures (in particular Binary Search Trees (BST)) BSTs idea nice way to implement a Set, Bag, or Map TODAY Priority Queue = variation on Queue
More informationA. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS
Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization
More informationStacks, Queues and Hierarchical Collections
Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by
More information[ DATA STRUCTURES ] Fig. (1) : A Tree
[ DATA STRUCTURES ] Chapter - 07 : Trees A Tree is a non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several
More informationB-Trees and External Memory
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 and External Memory 1 1 (2, 4) Trees: Generalization of BSTs Each internal node
More informationLecture Notes for Advanced Algorithms
Lecture Notes for Advanced Algorithms Prof. Bernard Moret September 29, 2011 Notes prepared by Blanc, Eberle, and Jonnalagedda. 1 Average Case Analysis 1.1 Reminders on quicksort and tree sort We start
More informationData Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.
DATA STRUCTURES COMP 321 McGill University These slides are mainly compiled from the following resources. - Professor Jaehyun Park slides CS 97SI - Top-coder tutorials. - Programming Challenges book. Data
More informationTrees. Tree Structure Binary Tree Tree Traversals
Trees Tree Structure Binary Tree Tree Traversals The Tree Structure Consists of nodes and edges that organize data in a hierarchical fashion. nodes store the data elements. edges connect the nodes. The
More informationB-Trees and External Memory
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 B-Trees and External Memory 1 (2, 4) Trees: Generalization of BSTs Each internal
More informationCS8391-DATA STRUCTURES QUESTION BANK UNIT I
CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those
More informationQuestion Bank Subject: Advanced Data Structures Class: SE Computer
Question Bank Subject: Advanced Data Structures Class: SE Computer Question1: Write a non recursive pseudo code for post order traversal of binary tree Answer: Pseudo Code: 1. Push root into Stack_One.
More informationRed-Black trees are usually described as obeying the following rules :
Red-Black Trees As we have seen, the ideal Binary Search Tree has height approximately equal to log n, where n is the number of values stored in the tree. Such a BST guarantees that the maximum time for
More informationCS 1114: Implementing Search. Last time. ! Graph traversal. ! Two types of todo lists: ! Prof. Graeme Bailey.
CS 1114: Implementing Search! Prof. Graeme Bailey http://cs1114.cs.cornell.edu (notes modified from Noah Snavely, Spring 2009) Last time! Graph traversal 1 1 2 10 9 2 3 6 3 5 6 8 5 4 7 9 4 7 8 10! Two
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 informationData 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 informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today Introduction to Linked Lists Stacks and Queues using Linked Lists Next Time Iterative Algorithms on Linked Lists Reading:
More informationWhat we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14
What we already know chapter 14 Files and Exceptions II Files are bytes on disk. Two types, text and binary (we are working with text) open creates a connection between the disk contents and the program
More informationINSTITUTE OF AERONAUTICAL ENGINEERING
INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech
More informationECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.
ECE 22 Data Structures and Algorithms http://www.ecs.umass.edu/~polizzi/teaching/ece22/ Trees IV Lecture 2 Prof. Eric Polizzi Summary previous lectures Implementations BST 5 5 7 null 8 null null 7 null
More informationStacks, Queues and Hierarchical Collections. 2501ICT Logan
Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks
More informationAn Introduction to Trees
An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search
More informationChapter 8: Data Abstractions
Chapter 8: Data Abstractions Computer Science: An Overview Tenth Edition by J. Glenn Brookshear Presentation files modified by Farn Wang Copyright 28 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
More informationR10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May
www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the
More informationTREES cs2420 Introduction to Algorithms and Data Structures Spring 2015
TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due Thursday at midnight -asking for regrades through assignment 5 and midterm must be complete by
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 informationModule 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.
The Lecture Contains: Index structure Binary search tree (BST) B-tree B+-tree Order file:///c /Documents%20and%20Settings/iitkrana1/My%20Documents/Google%20Talk%20Received%20Files/ist_data/lecture13/13_1.htm[6/14/2012
More informationTree-Structured Indexes
Tree-Structured Indexes Chapter 9 Database Management Systems, R. Ramakrishnan and J. Gehrke 1 Introduction As for any index, 3 alternatives for data entries k*: ➀ Data record with key value k ➁
More informationAn AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time
B + -TREES MOTIVATION An AVL tree with N nodes is an excellent data structure for searching, indexing, etc. The Big-Oh analysis shows that most operations finish within O(log N) time The theoretical conclusion
More informationCourse Review for Finals. Cpt S 223 Fall 2008
Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,
More informationR13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)
SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)
More informationCSE 230 Intermediate Programming in C and C++
CSE 230 Intermediate Programming in C and C++ Structures and List Processing Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Self-referential Structure
More informationDATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305
Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n
More informationA6-R3: DATA STRUCTURE THROUGH C LANGUAGE
A6-R3: DATA STRUCTURE THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be answered in the TEAR-OFF
More informationCSC148H Week 3. Sadia Sharmin. May 24, /20
CSC148H Week 3 Sadia Sharmin May 24, 2017 1/20 Client vs. Developer I For the first couple of weeks, we have played the role of class designer I However, you are also often in the opposite role: when a
More informationChapter 8: Data Abstractions
Chapter 8: Data Abstractions Computer Science: An Overview Eleventh Edition by J. Glenn Brookshear Copyright 2012 Pearson Education, Inc. Chapter 8: Data Abstractions 8.1 Data Structure Fundamentals 8.2
More informationTrees. CSE 373 Data Structures
Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories
More informationProgramming 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 informationCOSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)
COSC 2007 Data Structures II Final Exam Thursday, April 13 th, 2006 This is a closed book and closed notes exam. There are total 3 parts. Please answer the questions in the provided space and use back
More informationExtra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University
Extra: B+ Trees CS1: Java Programming Colorado State University Slides by Wim Bohm and Russ Wakefield 1 Motivations Many times you want to minimize the disk accesses while doing a search. A binary search
More information3137 Data Structures and Algorithms in C++
3137 Data Structures and Algorithms in C++ Lecture 3 July 12 2006 Shlomo Hershkop 1 Announcements Homework 2 out tonight Please make sure you complete hw1 asap if you have issues, please contact me will
More informationLinear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013
Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every
More informationLecture 10 Notes Linked Lists
Lecture 10 Notes Linked Lists 15-122: Principles of Imperative Computation (Spring 2016) Frank Pfenning, Rob Simmons, André Platzer 1 Introduction In this lecture we discuss the use of linked lists to
More informationMIDTERM EXAMINATION Spring 2010 CS301- Data Structures
MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the
More informationDynamic Storage Allocation
6.172 Performance Engineering of Software Systems LECTURE 10 Dynamic Storage Allocation Charles E. Leiserson October 12, 2010 2010 Charles E. Leiserson 1 Stack Allocation Array and pointer A un Allocate
More information8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))
DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]
More informationDo you remember what is the most powerful tool for managing complexity?
Do you remember what is the most powerful tool for managing complexity? 2 Abstractions and more abstractions You are here 3 8.1 Three views of data Application or user level Logical or abstract level Implementation
More informationRevision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,
[1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n
More informationDDS Dynamic Search Trees
DDS Dynamic Search Trees 1 Data structures l A data structure models some abstract object. It implements a number of operations on this object, which usually can be classified into l creation and deletion
More informationTree-Structured Indexes
Introduction Tree-Structured Indexes Chapter 10 As for any index, 3 alternatives for data entries k*: Data record with key value k
More informationME/CS 132: Advanced Robotics: Navigation and Vision
ME/CS 132: Advanced Robotics: Navigation and Vision Lecture #5: Search Algorithm 1 Yoshiaki Kuwata 4/12/2011 Lecture Overview Introduction Label Correcting Algorithm Core idea Depth-first search Breadth-first
More informationQueues. ADT description Implementations. October 03, 2017 Cinda Heeren / Geoffrey Tien 1
Queues ADT description Implementations Cinda Heeren / Geoffrey Tien 1 Queues Assume that we want to store data for a print queue for a student printer Student ID Time File name The printer is to be assigned
More information