Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 10: Data Structures II
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advanced Data Structures
CS 171: Introduction to Computer Science II
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Grade 12 Computer Studies HG
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Data Structures Systems Programming. 22 Data Structures  Queues –Queuing System Models –Queue Data Structures –A Queue Example  Trees –Binary Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data.
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Programming Practice 3 - Dynamic Data Structure
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Trees Chapter 15.
12 C Data Structures.
12 C Data Structures.
Chapter 22 Custom Generic Data Structures
Cse 373 April 26th – Exam Review.
Chapter 17 Object-Oriented Data Structures
Chapter 20: Binary Trees.
Chapter 10: Non-linear Data Structures
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Chapter 20: Binary Trees.
Presentation transcript:

Data Structures Systems Programming

Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue Data Structures –A Queue Example  Trees –Binary Trees –Tree Traversals Inorder, preorder, postorderInorder, preorder, postorder  Stacks A Stack ExampleA Stack Example  Hashing  Static Hashing  Linear probing  Chaining

Systems Programming: Data Structures 3 3 Simple Queuing Model Arrivals Queue Server

Systems Programming: Data Structures 4 4 Router Node packet Outgoing Link Router Buffer Server

Systems Programming: Data Structures 5 5 Performance Metrics (General Definitions)   Utilization :: the percentage of time a device is busy servicing a “customer”.   Throughput :: the number of jobs processed by the “system” per unit time.   Response time :: the time required to receive a response to a request (round-trip time).   Delay :: the time to traverse from one end to the other in a system.

Systems Programming: Data Structures Queues   Queues – –Are very common structures in operating systems and computer networks. – –The abstraction in system queues is of customers queued with one customer in service. – –Customers are placed in the queue First-In, first-Out (FIFO). – –Customers are removed from the head of the queue structure. When modeling a server, the customer at the head of the queue is in service. – –Customers are inserted at the back (or tail) of the queue. – –Queues can model either finite of infinite buffer space.

Systems Programming: Data Structures 7 7 Round Robin Queuing

Systems Programming: Data Structures Queues   Queues – –When modeling a finite buffer, when the buffer is full, an arriving customer is dropped (normally from the tail). This is known as a drop-tail queue.   Queue data structure operations: – –Insert or enqueue – –Remove or dequeue

Systems Programming: Data Structures 9 9 Fig Queue Graphical Representation  2007 Pearson Ed -All rights reserved. Pointers go from head towards the tail

Systems Programming: Data Structures 10 Systems Programming: Data Structures 10 Fig Enqueue Operation  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 11 Systems Programming: Data Structures 11 Fig Dequeue Operation  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 12 Systems Programming: Data Structures 12 Each node in the queue contains a data element and a pointer to the next node Note that unlike linked lists and stacks, queues keep track of the tail node as well as the head Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 13 Systems Programming: Data Structures 13 Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 14 Systems Programming: Data Structures 14 Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 15 Systems Programming: Data Structures 15 To insert a node into the queue, memory must first be allocated for that node Queue nodes are always inserted at the tail, so there is no need to search for the node’s place If the queue is empty, the inserted node becomes the new head in addition to the new tail Inserted node becomes the new tail Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 16 Systems Programming: Data Structures 16 Queue nodes are always removed from the head, so there is no need to search for the node’s place Free the memory of the removed node Second node becomes the new head If the removed node is the only node in the queue, it is the tail as well as the head of the queue, so tailPtr must be set to NULL Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 17 Systems Programming: Data Structures 17 Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 18 Systems Programming: Data Structures 18 Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 19 Systems Programming: Data Structures 19 Fig Processing a Queue  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures Trees  two  Tree nodes contain two or more links. – – All other data structures considered thus far have only contained one link.   Binary trees –All nodes contain two links. None, one, or both of which may be NULL –root node –The root node is the first node in a tree. –child –Each link in the root node refers to a child. –leaf node –A node with no children is called a leaf node. – –A node can only be inserted as a leaf node in a binary search tree (i.e., a tree without duplicates.)  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 21 Fig Binary Tree Representation  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 22 Common Programming Error 12.8   Not setting to NULL the links in leaf nodes of a tree can lead to runtime errors.   Remember, since tree traversals are normally recursive, this error can be ‘deeply embedded’.  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures Trees   Binary trees –left subtree –The key value for nodes in the left subtree are less than the key value in the parent. –right subtree –The key value for nodes in the right subtree are greater than the key value in the parent. – –This data structure facilitates duplicate elimination! – –Fast searches - for a balanced tree, maximum of log 2 n comparisons.  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 24 Fig Binary search tree  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 25 The Three Standard Tree Traversals 1. Inorder traversal:  ascending order  visit the nodes in ascending order by key value. 1.1 Traverse the left subtree with an inorder traversal 1.2 Visit the node (process the value in the node,i.e., print the node value). 1.3 Traverse the right subtree with an inorder traversal.

Systems Programming: Data Structures Preorder traversal: 2.1 Visit the node (process the value in the node, i.e., print the node value). 2.2 Traverse the left subtree with a preorder traversal. 2.3 Traverse the right subtree with a preorder traversal. The Three Standard Tree Traversals

Systems Programming: Data Structures Postorder traversal: 3.1 Traverse the left subtree with a postorder traversal. 3.2 Traverse the right subtree with a postorder traversal. 3.3 Visit the node (process the value in the node, i.e., print the node value). The Three Standard Tree Traversals

Systems Programming: Data Structures 28 Each node in the tree contains a data element and a pointer to the left and right child nodes Tree Example data  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 29 Tree Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 30 To insert a node into the tree, memory must first be allocated for that node If the inserted node’s data is less than the current node’s, the program will attempt to insert the node at the current node’s left child.  2007 Pearson Ed -All rights reserved. Tree Example

Systems Programming: Data Structures 31 If the inserted node’s data is greater than the current node’s, the program will attempt to insert the node at the current node’s right child. The inorder traversal calls an inorder traversal on the node’s left child, then prints the node itself, then calls an inorder traversal on the right child.  2007 Pearson Ed -All rights reserved. Tree Example

Systems Programming: Data Structures 32 The preorder traversal prints the node itself, then calls a preorder traversal on the node’s left child, then calls a preorder traversal on the right child. The postorder traversal calls an postorder traversal on the node’s left child, then calls an postorder traversal on the right child, then prints the node itself.  2007 Pearson Ed -All rights reserved. Tree Example

Systems Programming: Data Structures 33 Systems Programming: Data Structures Stacks   Stack – –New nodes are added and removed only at the top of the stack. – –The classical analogy is a dishes stacker found in restaurants. –LIFO –Last-in, first-out (LIFO) devices. – –The bottom of stack is indicated by a link member to NULL. – –Essentially, a constrained version of a linked list. – –Stacks are important in computer languages because it is the data structure for calling and returning from function or subroutine calls.  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 34 Systems Programming: Data Structures Stacks   Stack operations   push – –Adds a new node to the top of the stack.   pop – –Removes a node from the top of the stack. – –Stores the popped value. – –Returns true if pop was successful.  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 35 Systems Programming: Data Structures 35 Fig Push Fig Push Operation  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 36 Systems Programming: Data Structures 36  2007 Pearson Ed -All rights reserved. Fig Pop Fig Pop Operation

Systems Programming: Data Structures 37 Systems Programming: Data Structures 37 Each node in the stack contains a data element and a pointer to the next node A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 38 Systems Programming: Data Structures 38 A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 39 Systems Programming: Data Structures 39 To insert a node into the stack, memory must first be allocated for that node A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 40 Systems Programming: Data Structures 40 Stack nodes are always inserted at the top, so there is no need to search for the node’s place Inserted node becomes the new top Stack nodes are always removed from the top, so there is no need to search for the node’s place Second node becomes the new top Free the memory of the popped node A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 41 Systems Programming: Data Structures 41 A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 42 Systems Programming: Data Structures 42 A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 43 Systems Programming: Data Structures 43 A Stack Example  2007 Pearson Ed -All rights reserved.

Systems Programming: Data Structures 44 Systems Programming: Data Structures 44 Hashing   Two classic examples of the use of hashing are:   {old example}  name-attribute  Building a symbol table for a compiler, whereby a symbol table is similar to a dictionary, but with a set of name-attribute pairs.   Standard operations on any symbol table are: 1. name is in the table. 1. Determine if the name is in the table. 2. Retrieve the attributes of that name. 3. Modify the attributes of that name. 4. Insert a new name and its attributes in the table.

Systems Programming: Data Structures 45 Systems Programming: Data Structures 45 Tracking Flows in a Router Node packet Outgoing Link Router Buffer Server {new example}

Systems Programming: Data Structures 46 Static Hashing   In static hashing, identifiers (names)   Are stored in a fixed size table called a hash table.   A hash function f(x) is used to determine the location of an identifier x in the table.   The hash table is stored into sequential memory locations that are partitioned into b buckets. Each bucket has s slots.

Systems Programming: Data Structures 47 Hash Table

Systems Programming: Data Structures 48 Hash Function   A good hash function is easy to compute and minimizes bucket collisions (i.e., it should be unbiased).  uniformly distribute  A good hash function hashes x such that x has an equal chance of hashing into any of the b buckets.Namely, the hash function should uniformly distribute the symbols into the b buckets.   Examples of hash function are: mid-square, division f(x) = x % M and folding.

Systems Programming: Data Structures 49 Hash Table Details   We need to initialize table where all slots are empty. Void init_table (element ht[]) { int i; for (i =0; I < TABLE-SIZE; i++) ht[i].key[0] = NULL; }

Systems Programming: Data Structures 50 Hash Table with Linear Probing There are four possibilities after hashing x into a table bucket: The bucket contains x. {x is in the table already} The bucket is empty The bucket contains a nonempty entry other than x. {a bucket collision}. Here we either examine next slot or the next bucket We have exhausted all table memory and have wrapped around to the ‘home bucket’.

Systems Programming: Data Structures 51 Hash Table with Linear Probing   After running for awhile identifiers will tend to cluster and coalesce. Note, now the search time is likely to grow with each new addition to the symbol table.   Result = Bad Performance.   This is the motivation for implementing hash buckets using chaining ( each bucket is implemented as a linked list with a header node).

Systems Programming: Data Structures 52 Example of Chain insert into a hash table with a BAD hash function [0] -> add -> asp -> attitude [1] -> NULL [2] -> cat -> char -> cosine -> czar … [9] -> jack -> jumbo [10] -> king -> kinicki … [25] -> zac -> zebro -zits

Systems Programming: Data Structures 53 More Advanced Hashing   Dynamic Hashing   Double Hashing

Systems Programming: Data Structures 54 Systems Programming: Data Structures 54 Systems Programming: 54 Summary of Data Structures  Connected queuing computer systems with queuing data structures. Basic operations: enqueue and dequeue.  Introduced tree terminology and structures –Binary Trees –Tree Traversals Inorder, preorder, postorderInorder, preorder, postorder  Stacks A Stack ExampleA Stack Example  A quick look at Hashing including: –Static Hashing –Hash functions –Linear probing –Chaining (linked lists)