 2006 Pearson Education, Inc. All rights reserved. 1 21 Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Data Structures Michael J. Watts
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
 2009 Pearson Education, Inc. All rights reserved Data Structures Many slides modified by Prof. L. Lilien (even many without an explicit message).
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 20 - Data Structures Outline 20.1 Introduction 20.2 Self-Referential Classes 20.3 Dynamic Memory.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Helpful C++ Transitions
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Data Structures.
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.
Introduction to Data Structures Systems Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Data Structures Systems Programming. 22 Data Structures  Queues –Queuing System Models –Queue Data Structures –A Queue Example  Trees –Binary Trees.
 2008 Pearson Education, Inc. All rights reserved. 1 Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Introduction to Data Structures Systems Programming Concepts.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 17 - Data Structures Outline 17.1 Introduction 17.2 Self-Referential Classes 17.3 Dynamic Memory.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Templates Lecture 10 March 23, 2004.
Programming Practice 3 - Dynamic Data Structure
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Java How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
 2003 Prentice Hall, Inc. All rights reserved Stacks Upcoming program –Create stack from list insertAtFront, removeFromFront –Software reusability.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Chapter 20 Custom Templatized Data Structures
Data Structure By Amee Trivedi.
17 Data Structures.
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
12 C Data Structures.
12 C Data Structures.
Chapter 22 Custom Generic Data Structures
CISC181 Introduction to Computer Science Dr
Intro to Data Structures
Helpful C++ Transitions
Review & Lab assignments
20.5 Stacks Upcoming program Create stack from list
Chapter 20 - Data Structures
21 Data Structures.
Presentation transcript:

 2006 Pearson Education, Inc. All rights reserved Data Structures

 2006 Pearson Education, Inc. All rights reserved. 2 Much that I bound, I could not free; Much that I freed returned to me. — Lee Wilson Dodd ‘Will you walk a little faster?’ said a whiting to a snail, ‘There’s a porpoise close behind us, and he’s treading on my tail.’ — Lewis Carroll There is always room at the top. — Daniel Webster Push on — keep moving. — Thomas Morton I’ll turn over a new leaf. — Miguel de Cervantes

 2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  To form linked data structures using pointers, self- referential classes and recursion.  To create and manipulate dynamic data structures such as linked lists, queues, stacks and binary trees.  To use binary search trees for high-speed searching and sorting.  To understand various important applications of linked data structures.  To understand how to create reusable data structures with class templates, inheritance and composition.

 2006 Pearson Education, Inc. All rights reserved Introduction 21.2 Self-Referential Classes 21.3 Dynamic Memory Allocation and Data Structures 21.4 Linked Lists 21.5 Stacks 21.6 Queues 21.7 Trees 21.8 Wrap-Up

 2006 Pearson Education, Inc. All rights reserved Introduction Dynamic data structures – Grow and shrink during execution – Linked lists Collections of data items “lined up in a row” Insertions and removals are made anywhere – Stacks Insertions and removals are made only at top Important in compilers and operating systems – Queues Insertions are made at back, removals are made at front Represent waiting lines

 2006 Pearson Education, Inc. All rights reserved Introduction (Cont.) Dynamic data structures (Cont.) – Binary trees Facilitate – High-speed searching and sorting – Elimination of duplicates Used in – Representations of file system directories – Compilation of expressions into machine language

 2006 Pearson Education, Inc. All rights reserved Self-Referential Classes Self-referential class – Contains a pointer member that points to an object of the same class type – Example Class Node { … Node *nextPtr; }; – Pointer data member nextPtr is a link Can tie a Node to another Node

 2006 Pearson Education, Inc. All rights reserved. 8 Fig | Two self-referential class objects linked together.

 2006 Pearson Education, Inc. All rights reserved. 9 Common Programming Error 21.1 Not setting the link in the last node of a linked data structure to null ( 0 ) is a (possibly fatal) logic error.

 2006 Pearson Education, Inc. All rights reserved Dynamic Memory Allocation and Data Structures Dynamic memory allocation – Enables a program to obtain more memory at execution time That memory can be released when it is no longer needed – Limited by amount of physical or virtual memory Memory must be shared among many programs

 2006 Pearson Education, Inc. All rights reserved Dynamic Memory Allocation and Data Structures (Cont.) new operator – Dynamically allocates memory for an object – Takes as argument the type of the object being allocated – Returns pointer to the newly-allocated object – Example Node *newPtr = new Node( 10 ); – Allocates sizeof( Node ) bytes for new Node object – Calls Node constructor with argument 10 – Assigns address of new Node object to newPtr

 2006 Pearson Education, Inc. All rights reserved Dynamic Memory Allocation and Data Structures (Cont.) delete operator – Calls the destructor and deallocates memory for an object – Example delete newPtr; – Calls Node destructor for object pointed to by newPtr – Deallocates memory of object pointed to by newPtr – Does not delete pointer variable newPtr – If called on a null pointer Has no effect

 2006 Pearson Education, Inc. All rights reserved Linked Lists Linked list – Linear collection of self-referential class objects Called nodes Connected by pointer links – Accessed via a pointer to the first node Subsequent nodes are accessed via previous node’s link – By convention, link in last node is set to null pointer 0 – Additional nodes are dynamically allocated as necessary

 2006 Pearson Education, Inc. All rights reserved Linked Lists (Cont.) Linked list (Cont.) – Advantages over arrays Linked lists are dynamic – Length can increase or decrease as necessary Efficient insertion of new elements into a sorted list – Existing list elements do not need to be moved

 2006 Pearson Education, Inc. All rights reserved. 15 Performance Tip 21.1 An array can be declared to contain more elements than the number of items expected, but this can waste memory. Linked lists can provide better memory utilization in these situations. Linked lists allow the program to adapt at runtime. Note that class template vector (introduced in Section 7.11) implements a dynamically resizable array- based data structure.

 2006 Pearson Education, Inc. All rights reserved. 16 Performance Tip 21.2 Insertion and deletion in a sorted array can be time consuming—all the elements following the inserted or deleted element must be shifted appropriately. A linked list allows efficient insertion operations anywhere in the list.

 2006 Pearson Education, Inc. All rights reserved. 17 Performance Tip 21.3 The elements of an array are stored contiguously in memory. This allows immediate access to any array element, because the address of any element can be calculated directly based on its position relative to the beginning of the array. Linked lists do not afford such immediate “direct access” to their elements. So accessing individual elements in a linked list can be considerably more expensive than accessing individual elements in an array. The selection of a data structure is typically based on the performance of specific operations used by a program and the order in which the data items are maintained in the data structure. For example, it is typically more efficient to insert an item in a sorted linked list than a sorted array.

 2006 Pearson Education, Inc. All rights reserved. 18 Performance Tip 21.4 Using dynamic memory allocation (instead of fixed-size arrays) for data structures that grow and shrink at execution time can save memory. Keep in mind, however, that pointers occupy space and that dynamic memory allocation incurs the overhead of function calls.

 2006 Pearson Education, Inc. All rights reserved. 19 Fig | A graphical representation of a list.

 2006 Pearson Education, Inc. All rights reserved. 20 Outline Listnode.h (1 of 2) Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next ListNode object in the linked list Declare class List as a friend

 2006 Pearson Education, Inc. All rights reserved. 21 Outline Listnode.h (2 of 2)

 2006 Pearson Education, Inc. All rights reserved. 22 Outline List.h (1 of 7) private data members firsrtPtr (a pointer to the first ListNode in a List ) and lastPtr (a pointer to the last ListNode in a List )

 2006 Pearson Education, Inc. All rights reserved. 23 Outline List.h (2 of 7) Initialize both pointers to 0 (null) Ensure that all ListNode objects in a List object are destroyed when that List object is destroyed

 2006 Pearson Education, Inc. All rights reserved. 24 Outline List.h (3 of 7) Places a new node at the front of the list Use function getNewNode to allocate a new ListNode containing value and assign it to newPtr If the list is empty, then both firstPtr and lastPtr are set to newPtr Thread the new node into the list so that the new node points to the old first node and firstPtr points to the new node Places a new node at the back of the list Use function getNewNode to allocate a new listNode containing value and assign it to newPtr If the list is empty, then both firstPtr and lastPtr are set to newPtr Thread the new node into the list so that the old last node points to the new node and lastPtr points to the new node

 2006 Pearson Education, Inc. All rights reserved. 25 Outline List.h (4 of 7) Removes the front node of the list and copies the node value to the reference parameter Return false if an attempt is made to remove a node from an empty list Save a pointer to the first node, which will be removed If the list has only one element, leave the list empty Set firstPtr to point to the second node (the new first node) Copy the removed node ’ s data to reference parameter value delete the removed node

 2006 Pearson Education, Inc. All rights reserved. 26 Outline List.h (5 of 7) Removes the back node of the list and copies the node value to the reference parameter Return false if an attempt is made to remove a node from an empty list Save a pointer to the last node, which will be removed If the list has only one element, leave the list empty Assign currentPtr the address of the first node to prepare to “ walk the list ” “ Walk the list ” until currentPtr points to the node before the last node, which will be the new last node Make the currentPtr node the new last node Copy the removed node ’ s data to reference parameter value delete the removed node

 2006 Pearson Education, Inc. All rights reserved. 27 Outline List.h (6 of 7) Determine whether the List is empty Return a dynamically allocated ListNode object

 2006 Pearson Education, Inc. All rights reserved. 28 Outline List.h (7 of 7) Iterate through the list and output the value in each node

 2006 Pearson Education, Inc. All rights reserved. 29 Outline Fig21_05.cpp (1 of 6)

 2006 Pearson Education, Inc. All rights reserved. 30 Outline Fig21_05.cpp (2 of 6)

 2006 Pearson Education, Inc. All rights reserved. 31 Outline Fig21_05.cpp (3 of 6)

 2006 Pearson Education, Inc. All rights reserved. 32 Outline Fig21_05.cpp (4 of 6)

 2006 Pearson Education, Inc. All rights reserved. 33 Outline Fig21_05.cpp (5 of 6)

 2006 Pearson Education, Inc. All rights reserved. 34 Outline Fig21_05.cpp (6 of 6)

 2006 Pearson Education, Inc. All rights reserved. 35 Error-Prevention Tip 21.1 Assign null ( 0 ) to the link member of a new node. Pointers should be initialized before they are used.

 2006 Pearson Education, Inc. All rights reserved. 36 Fig | Operation insertAtFront represented graphically.

 2006 Pearson Education, Inc. All rights reserved. 37 Fig | Operation insertAtBack represented graphically.

 2006 Pearson Education, Inc. All rights reserved. 38 Fig | Operation removeFromFront represented graphically.

 2006 Pearson Education, Inc. All rights reserved. 39 Fig | Operation removeFromBack represented graphically.

 2006 Pearson Education, Inc. All rights reserved Linked Lists (Cont.) Linked list (Cont.) – Circular, singly linked list Pointer in last node points back to first node – Doubly linked list Each node has a link to next node and a link to previous node Two “start pointers” – One to first node, one to last node Allows traversals both forward and backward – Circular, doubly linked list Forward link of last node points back to first node Backward link of first node points to last node

 2006 Pearson Education, Inc. All rights reserved. 41 Fig | Circular, singly linked list.

 2006 Pearson Education, Inc. All rights reserved. 42 Fig | Doubly linked list.

 2006 Pearson Education, Inc. All rights reserved. 43 Fig | Circular, doubly linked list.

 2006 Pearson Education, Inc. All rights reserved Stacks Stack – Allows nodes to be added and removed only at the top Referred to as a last-in, first-out (LIFO) data structure – Can be implemented as a constrained linked list Link of the last node of the stack is set to null to indicate bottom of the stack – Manipulated using member functions push and pop push inserts new node at the top pop removes node from the top and retrieves its value

 2006 Pearson Education, Inc. All rights reserved Stacks (Cont.) Stack (Cont.) – Applications of stacks Function call stack – Stores return addresses for function calls – Stores values of automatic variables in function calls Used by compilers – Evaluating expressions – Generating machine-language code

 2006 Pearson Education, Inc. All rights reserved. 46 Outline Stack.h (1 of 2) Create a Stack class template through private inheritance of the List class template Perform push and pop by delegating to base- class member functions insertAtFront and removeFromFront, respectively

 2006 Pearson Education, Inc. All rights reserved. 47 Outline Stack.h (2 of 2) Member functions isStackEmpty and printStack delegate to base-class member functions isEmpty and print, respectively

 2006 Pearson Education, Inc. All rights reserved. 48 Outline Fig21_14.cpp (1 of 3) Push integers 0 through 2 onto intStack Pop integers 2 through 0 off intStack

 2006 Pearson Education, Inc. All rights reserved. 49 Outline Fig21_14.cpp (2 of 3) Push values 1.1, 2.2 and 3.3 onto doubleStack Pop values 3.3, 2.2 and 1.1 off doubleStack

 2006 Pearson Education, Inc. All rights reserved. 50 Outline Fig21_14.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 51 Outline Stack composition.h (1 of 2)

 2006 Pearson Education, Inc. All rights reserved. 52 Outline Stack composition.h (2 of 2) This implementation of the Stack class template contains a List object called stackList

 2006 Pearson Education, Inc. All rights reserved Queues Queue – First item in line is serviced first, others wait in line Nodes removed only from head and inserted only at tail Referred to as a first-in, first-out (FIFO) data structure – Insert and remove operations are enqueue and dequeue – Applications of queues Waiting lines for – Processor usage – Printing jobs – Packets at a router – File system requests

 2006 Pearson Education, Inc. All rights reserved. 54 Outline Queue.h (1 of 2) Create a Queue class template through private inheritance of the List class template Perform enqueue and dequeue by delegating to base-class member functions insertAtBack and removeFromFront, respectively

 2006 Pearson Education, Inc. All rights reserved. 55 Outline Queue.h (2 of 2) Member functions isQueueEmpty and printQueue delegate to base-class member functions isEmpty and print, respectively

 2006 Pearson Education, Inc. All rights reserved. 56 Outline Fig21_17.cpp (1 of 3) Enqueue integers 0 through 2 to intQueue Dequeue integers 0 through 2 from intQueue

 2006 Pearson Education, Inc. All rights reserved. 57 Outline Fig21_17.cpp (2 of 3) Enqueue values 1.1, 2.2 and 3.3 to doubleQueue Dequeue values 1.1, 2.2 and 3.3 from doubleQueue

 2006 Pearson Education, Inc. All rights reserved. 58 Outline Fig21_17.cpp (2 of 3)

 2006 Pearson Education, Inc. All rights reserved Trees Tree – Nonlinear, two-dimensional data structure – Tree nodes contain two or more links Binary tree nodes contain exactly two links

 2006 Pearson Education, Inc. All rights reserved Trees (Cont.) Tree (Cont.) – Terminology Root node – First node in a tree Child node – Node linked to by another node (its parent) – In a binary tree, there is a left child and a right child Subtree – Tree defined by treating a child node as the root of its own tree Siblings – Multiple children of a single parent node Leaf node – Node with no children

 2006 Pearson Education, Inc. All rights reserved. 61 Fig | A graphical representation of a binary tree.

 2006 Pearson Education, Inc. All rights reserved Trees (Cont.) Tree (Cont.) – Binary search tree Values in any left subtree are less than value in its parent node Values in any right subtree are greater than value in its parent node Can be recursively traversed in three ways – Inorder Left subtree, then current node, then right subtree – Preorder Current node, then left subtree, then right subtree – Postorder Left subtree, then right subtree, then current node

 2006 Pearson Education, Inc. All rights reserved. 63 Fig | A binary search tree.

 2006 Pearson Education, Inc. All rights reserved. 64 Outline Treenode.h (1 of 2) Declare Tree as TreeNode ’ s friend Initialize this node to be a leaf node with data value d

 2006 Pearson Education, Inc. All rights reserved. 65 Outline Treenode.h (2 of 2) Pointers leftPtr and rightPtr point to the node ’ s left and right subtrees, respectively

 2006 Pearson Education, Inc. All rights reserved. 66 Outline Tree.h (1 of 6)

 2006 Pearson Education, Inc. All rights reserved. 67 Outline Tree.h (2 of 6) Call utility function insertNodeHelper to recursively insert a node into the tree Initialize rootPtr to zero to indicate that the tree is initially empty

 2006 Pearson Education, Inc. All rights reserved. 68 Outline Tree.h (3 of 6) A new TreeNode is created, initialized and inserted in the tree Recursively call insertNodeHelper with the address of the pointer to the appropriate binary search subtree If the value to be inserted is identical to the data value in the root node, do not insert the duplicate value into the tree Parameter ptr is a pointer to a pointer to a TreeNode

 2006 Pearson Education, Inc. All rights reserved. 69 Outline Tree.h (4 of 6) Process the value in the node, traverse the left subtree, traverse the right subtree

 2006 Pearson Education, Inc. All rights reserved. 70 Outline Tree.h (5 of 6) Traverse the left subtree, process the value in the node, traverse the right subtree

 2006 Pearson Education, Inc. All rights reserved. 71 Outline Tree.h (6 of 6) Traverse the left subtree, traverse the right subtree, process the value in the node

 2006 Pearson Education, Inc. All rights reserved. 72 Outline Fig21_22.cpp (1 of 3) Instantiate integer tree intTree of type Tree Insert int values into the binary tree Perform traversal of intTree

 2006 Pearson Education, Inc. All rights reserved. 73 Outline Fig21_22.cpp (2 of 3) Instantiate floating-point tree doubleTree of type Tree Insert double values into the binary tree Perform traversal of doubleTree

 2006 Pearson Education, Inc. All rights reserved. 74 Outline Fig21_22.cpp (3 of 3)

 2006 Pearson Education, Inc. All rights reserved. 75 Fig | A binary search tree.

 2006 Pearson Education, Inc. All rights reserved Trees (Cont.) Tree (Cont.) – Applications of binary search trees Duplicate elimination – Inserting duplicate will follow same path as original – Duplicate can be discarded when compared with orignal Searching (in a balanced binary search tree) – Has O(log n) runtime Each comparison of a node to search key eliminates half the nodes Maximum of log 2 n comparisons are required Sorting (binary tree sort) – Inorder traversal of a binary search tree results in processing the values in ascending order