 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory.

Slides:



Advertisements
Similar presentations
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Advertisements

C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
1 Linked List (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
 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.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
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.
Templates Zhen Jiang West Chester University
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
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.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 19 – Data Structures Outline 19.1 Introduction 19.2 Self-Referential Classes 19.3 Dynamic Memory.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
 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.
Final Exam –Date: Aug 27 th –Time: 9:00am – 12:00pm –Location: TEL 0014.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 20 November 10, 2009.
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
 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.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
Chapter 20 Custom Templatized Data Structures
Chapter 22 - C++ Templates
Data Structure By Amee Trivedi.
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
Chapter 19 – Data Structures
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Review & Lab assignments
20.5 Stacks Upcoming program Create stack from list
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Pointers & Dynamic Data Structures
template< class T > class Stack { public:
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Chapter 22 - C++ Templates
Chapter 22 - C++ Templates
Chapter 20 - Data Structures
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
21 Data Structures.
Presentation transcript:

 2000 Deitel & Associates, Inc. All rights reserved. Chapter 15 – Data Structures Outline 15.1Introduction 15.2Self-Referential Classes 15.3Dynamic Memory Allocation 15.4Linked Lists 15.5Stacks 15.6Queues 15.7Trees

 2000 Deitel & Associates, Inc. All rights reserved Introduction dynamic data structures - grow and shrink during execution Linked lists - insertions and removals made anywhere Stacks - insertions and removals made only at top of stack Queues - insertions made at the back and removals made from the front Binary trees - high-speed searching and sorting of data and efficient elimination of duplicate data items

 2000 Deitel & Associates, Inc. All rights reserved Self-Referential Classes self-referential class –class that contains a pointer to a class object of the same type –can be linked together to form useful data structures such as lists, queues, stacks and trees –terminated with a NULL pointer ( 0 ) Two self-referential class objects linked together 1015 NULL pointer (points to nothing) Data member and pointer

 2000 Deitel & Associates, Inc. All rights reserved Self-Referential Classes (II) class Node { public: Node( int ); void setData( int ); int getData() const; void setNextPtr( Node * ); const Node *getNextPtr() const; private: int data; Node *nextPtr; }; nextPtr - points to an object of type Node –referred to as a link – ties one Node to another Node

 2000 Deitel & Associates, Inc. All rights reserved Dynamic Memory Allocation dynamic memory allocation –obtain and release memory during execution new –takes an argument and returns a pointer to object being allocated Node *newPtr = new Node( 10 ); allocates sizeof( Node ) bytes runs the Node constructor and stores pointer to that memory in newPtr throws a bad_alloc exception if no memory is available delete –calls Node destructor and deallocates memory allocated with new –delete newPtr; newPtr is not deleted; the space newPtr points to is deallocated

 2000 Deitel & Associates, 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 of the list –subsequent nodes are accessed via the link-pointer member –the link pointer in the last node is set to null to mark the list’s end Use a linked list instead of an array when –the number of data elements is unpredictable –the list needs to be sorted

 2000 Deitel & Associates, Inc. All rights reserved Linked Lists (II) Types of linked lists: –singly linked list begins with a pointer to the first node terminates with a null pointer only traversed in one direction –circular, singly linked pointer in the last node points back to the first node –doubly linked list two “start pointers”- first element and last element each node has a forward pointer and a backward pointer allows traversals both forwards and backwards –circular, doubly linked list forward pointer of the last node points to the first node and backward pointer of the first node points to the last node

 2000 Deitel & Associates, Inc. All rights reserved. Outline Creating a Linked List 1. Class definition 1.1 Function definitions 1// Fig. 15.3: listnd.h 2// ListNode template definition 3#ifndef LISTND_H 4#define LISTND_H 5 6template class List; // forward declaration 7 8template 9class ListNode { 10 friend class List ; // make List a friend 11public: 12 ListNode( const NODETYPE & ); // constructor 13 NODETYPE getData() const; // return data in the node 14private: 15 NODETYPE data; // data 16 ListNode *nextPtr; // next node in the list 17}; 18 19// Constructor 20template 21ListNode ::ListNode( const NODETYPE &info ) 22 : data( info ), nextPtr( 0 ) { } 23 24// Return a copy of the data in the node 25template 26NODETYPE ListNode ::getData() const { return data; } 27 28#endif Nodes contain data and a pointer

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Class definition 1.2 Function definitions 1.3 Constructor 29// Fig. 15.3: list.h 30// Template List class definition 31#ifndef LIST_H 32#define LIST_H 33 34#include 35#include 36#include "listnd.h" 37 38using std::cout; 39 40template 41class List { 42public: 43 List(); // constructor 44 ~List(); // destructor 45 void insertAtFront( const NODETYPE & ); 46 void insertAtBack( const NODETYPE & ); 47 bool removeFromFront( NODETYPE & ); 48 bool removeFromBack( NODETYPE & ); 49 bool isEmpty() const; 50 void print() const; 51private: 52 ListNode *firstPtr; // pointer to first node 53 ListNode *lastPtr; // pointer to last node // Utility function to allocate a new node 56 ListNode *getNewNode( const NODETYPE & ); 57}; 58 59// Default constructor 60template 61List ::List() : firstPtr( 0 ), lastPtr( 0 ) { } List has two pointers and can create new nodes

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.4 Destructor 1.5 insertAtFront 62 63// Destructor 64template 65List ::~List() 66{ 67 if ( !isEmpty() ) { // List is not empty 68 cout << "Destroying nodes...\n"; ListNode *currentPtr = firstPtr, *tempPtr; while ( currentPtr != 0 ) { // delete remaining nodes 73 tempPtr = currentPtr; 74 cout data << '\n'; 75 currentPtr = currentPtr->nextPtr; 76 delete tempPtr; 77 } 78 } cout << "All nodes destroyed\n\n"; 81} 82 83// Insert a node at the front of the list 84template 85void List ::insertAtFront( const NODETYPE &value ) 86{ 87 ListNode *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty 90 firstPtr = lastPtr = newPtr; 91 else { // List is not empty 92 newPtr->nextPtr = firstPtr; 93 firstPtr = newPtr; 94 } 95} Destructor destroys the linked list and nodes Sets the new node to point to what firstPtr points to, then sets firstPtr to the new node

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.6 insertAtBack 1.7 removeFromFront 96 97// Insert a node at the back of the list 98template 99void List ::insertAtBack( const NODETYPE &value ) 100{ 101 ListNode *newPtr = getNewNode( value ); if ( isEmpty() ) // List is empty 104 firstPtr = lastPtr = newPtr; 105 else { // List is not empty 106 lastPtr->nextPtr = newPtr; 107 lastPtr = newPtr; 108 } 109} // Delete a node from the front of the list 112template 113bool List ::removeFromFront( NODETYPE &value ) 114{ 115 if ( isEmpty() ) // List is empty 116 return false; // delete unsuccessful 117 else { 118 ListNode *tempPtr = firstPtr; if ( firstPtr == lastPtr ) 121 firstPtr = lastPtr = 0; 122 else 123 firstPtr = firstPtr->nextPtr; value = tempPtr->data; // data being removed 126 delete tempPtr; 127 return true; // delete successful 128 } 129} The last node points to the new node, then lastPtr points to the new node. Move firstPtr to second node and delete first node.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.7 removeFromBack 1.8 isEmpty // Delete a node from the back of the list 132template 133bool List ::removeFromBack( NODETYPE &value ) 134{ 135 if ( isEmpty() ) 136 return false; // delete unsuccessful 137 else { 138 ListNode *tempPtr = lastPtr; if ( firstPtr == lastPtr ) 141 firstPtr = lastPtr = 0; 142 else { 143 ListNode *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) 146 currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; 149 currentPtr->nextPtr = 0; 150 } value = tempPtr->data; 153 delete tempPtr; 154 return true; // delete successful 155 } 156} // Is the List empty? 159template 160bool List ::isEmpty() const 161 { return firstPtr == 0; } // return a pointer to a newly allocated node Change lastPtr to the second to last node and delete the last node

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.9 getNewNode 1.10 print 164template 165ListNode *List ::getNewNode( 166 const NODETYPE &value ) 167{ 168 ListNode *ptr = 169 new ListNode ( value ); 170 assert( ptr != 0 ); 171 return ptr; 172} // Display the contents of the List 175template 176void List ::print() const 177{ 178 if ( isEmpty() ) { 179 cout << "The list is empty\n\n"; 180 return; 181 } ListNode *currentPtr = firstPtr; cout << "The list is: "; while ( currentPtr != 0 ) { 188 cout data << ' '; 189 currentPtr = currentPtr->nextPtr; 190 } cout << "\n\n"; 193} #endif Create a new node and return a pointer to it. Walk through list and print node values.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Function definition 196// Fig. 15.3: fig15_03.cpp 197// List class test 198#include 199#include "list.h" using std::cin; 202using std::endl; // Function to test an integer List 205template 206void testList( List &listObject, const char *type ) 207{ 208 cout << "Testing a List of " << type << " values\n"; instructions(); 211 int choice; 212 T value; do { 215 cout << "? "; 216 cin >> choice; switch ( choice ) { 219 case 1: 220 cout << "Enter " << type << ": "; 221 cin >> value; 222 listObject.insertAtFront( value ); 223 listObject.print(); 224 break; 225 case 2: 226 cout << "Enter " << type << ": "; 227 cin >> value;

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.1 Function definition 228 listObject.insertAtBack( value ); 229 listObject.print(); 230 break; 231 case 3: 232 if ( listObject.removeFromFront( value ) ) 233 cout << value << " removed from list\n"; listObject.print(); 236 break; 237 case 4: 238 if ( listObject.removeFromBack( value ) ) 239 cout << value << " removed from list\n"; listObject.print(); 242 break; 243 } 244 } while ( choice != 5 ); cout << "End list test\n\n"; 247} void instructions() 250{ 251 cout << "Enter one of the following:\n" 252 << " 1 to insert at beginning of list\n" 253 << " 2 to insert at end of list\n" 254 << " 3 to delete from beginning of list\n" 255 << " 4 to delete from end of list\n" 256 << " 5 to end list processing\n"; 257} 258 Choices correspond to the switch statement

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Initialize objects 2. Function calls 259int main() 260{ 261 List integerList; 262 testList( integerList, "integer" ); // test integerList List doubleList; 265 testList( doubleList, "double" ); // test doubleList return 0; 268} Use templates to create an integer list and a double list.

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output Testing a List of integer values Enter one of the following: 1 to insert at beginning of list 2 to insert at end of list 3 to delete from beginning of list 4 to delete from end of list 5 to end list processing ? 1 Enter integer: 1 The list is: 1 ? 1 Enter integer: 2 The list is: 2 1 ? 2 Enter integer: 3 The list is: ? 2 Enter integer: 4 The list is: ? 3 2 removed from list The list is: ? 3 1 removed from list The list is: 3 4

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output ? 4 4 removed from list The list is: 3 ? 4 3 removed from list The list is empty ? 5 End list test Testing a List of double values Enter one of the following: 1 to insert at beginning of list 2 to insert at end of list 3 to delete from beginning of list 4 to delete from end of list 5 to end list processing ? 1 Enter double: 1.1 The list is: 1.1 ? 1 Enter double: 2.2 The list is: ? 2 Enter double: 3.3 The list is: ? 2 Enter double: 4.4 The list is:

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output ? removed from list The list is: ? removed from list The list is: ? removed from list The list is: 3.3 ? removed from list The list is empty ? 5 End list test All nodes destroyed All nodes destroyed

 2000 Deitel & Associates, Inc. All rights reserved Stacks stack – new nodes can be added and removed only at the top –similar to a pile of dishes –last-in, first-out (LIFO) –Bottom of stack indicated by a link member to null –constrained version of a linked list push –adds a new node to the top of the stack pop –removes a node from the top –stores the popped value –returns true if pop was successful

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Member functions Load header 1.1 Initialize objects 2. Modify objects 3. Output 1// Fig. 15.9: stack.h 2// Stack class template definition 3// Derived from class List 4#ifndef STACK_H 5#define STACK_H 6 7#include "list.h" 8 9template 10class Stack : private List { 11public: 12 void push( const STACKTYPE &d ) { insertAtFront( d ); } 13 bool pop( STACKTYPE &d ) { return removeFromFront( d ); } 14 bool isStackEmpty() const { return isEmpty(); } 15 void printStack() const { print(); } 16}; 17 18#endif 19// Fig. 15.9: fig15_09.cpp 20// Driver to test the template Stack class 21#include 22#include "stack.h" 23 24using std::endl; 25 26int main() 27{ 28 Stack intStack; 29 int popInteger, i; 30 cout << "processing an integer Stack" << endl; for ( i = 0; i < 4; i++ ) { 33 intStack.push( i ); Notice the functions Stack has: insertAtFront ( push ) and removeFromFront ( pop ) processing an integer Stack

 2000 Deitel & Associates, Inc. All rights reserved. Outline 3. Output 34 intStack.printStack(); 35 } while ( !intStack.isStackEmpty() ) { 38 intStack.pop( popInteger ); 39 cout << popInteger << " popped from stack" << endl; 40 intStack.printStack(); 41 } Stack doubleStack; 44 double val = 1.1, popdouble; 45 cout << "processing a double Stack" << endl; for ( i = 0; i < 4; i++ ) { 48 doubleStack.push( val ); 49 doubleStack.printStack(); 50 val += 1.1; 51 } while ( !doubleStack.isStackEmpty() ) { 54 doubleStack.pop( popdouble ); 55 cout << popdouble << " popped from stack" << endl; 56 doubleStack.printStack(); 57 } 58 return 0; 59} The list is: 0 The list is: 1 0 The list is: The list is: popped from stack The list is: popped from stack The list is: popped from stack The list is: 0 0 popped from stack The list is empty processing a double Stack The list is: 1.1 The list is: The list is: The list is: popped from stack The list is: popped from stack The list is: popped from stack The list is: popped from stack The list is empty All nodes destroyed All nodes destroyed

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output processing an integer Stack The list is: 0 The list is: 1 0 The list is: The list is: popped from stack The list is: popped from stack The list is: popped from stack The list is: 0 0 popped from stack The list is empty processing a double Stack The list is: 1.1 The list is: The list is: The list is:

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output 4.4 popped from stack The list is: popped from stack The list is: popped from stack The list is: popped from stack The list is empty All nodes destroyed All nodes destroyed

 2000 Deitel & Associates, Inc. All rights reserved Queues queue – similar to a supermarket checkout line –first-in, first-out (FIFO) –nodes are removed only from the head –nodes are inserted only at the tail The insert and remove operations are known as enqueue and dequeue Useful in computing –Print spooling, packets in networks, file server requests

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Class definition 1.2 Function prototypes Load header 1.1 Initialize objects 2. Function calls 1// Fig : queue.h 2// Queue class template definition 3// Derived from class List 4#ifndef QUEUE_H 5#define QUEUE_H 6 7#include "list.h" 8 9template 10class Queue: private List { 11public: 12 void enqueue( const QUEUETYPE &d ) { insertAtBack( d ); } 13 bool dequeue( QUEUETYPE &d ) 14 { return removeFromFront( d ); } 15 bool isQueueEmpty() const { return isEmpty(); } 16 void printQueue() const { print(); } 17}; 18 19#endif 20// Fig : fig15_12.cpp 21// Driver to test the template Queue class 22#include 23#include "queue.h" 24 25using std::endl; 26 27int main() 28{ 29 Queue intQueue; 30 int dequeueInteger, i; 31 cout << "processing an integer Queue" << endl; for ( i = 0; i < 4; i++ ) { queue only has limited linked-list operations ( insertAtBack and removeFromFront ) processing an integer Queue

 2000 Deitel & Associates, Inc. All rights reserved. Outline 2. Function calls 3. Output 34 intQueue.enqueue( i ); 35 intQueue.printQueue(); 36 } while ( !intQueue.isQueueEmpty() ) { 39 intQueue.dequeue( dequeueInteger ); 40 cout << dequeueInteger << " dequeued" << endl; 41 intQueue.printQueue(); 42 } Queue doubleQueue; 45 double val = 1.1, dequeuedouble; cout << "processing a double Queue" << endl; for ( i = 0; i < 4; i++ ) { 50 doubleQueue.enqueue( val ); 51 doubleQueue.printQueue(); 52 val += 1.1; 53 } while ( !doubleQueue.isQueueEmpty() ) { 56 doubleQueue.dequeue( dequeuedouble ); 57 cout << dequeuedouble << " dequeued" << endl; 58 doubleQueue.printQueue(); 59 } return 0; 62} The list is: 0 The list is: 0 1 The list is: The list is: dequeued The list is: dequeued The list is: dequeued The list is: 3 3 dequeued The list is empty processing a double QueueThe list is: 1.1 The list is: The list is: The list is: dequeued The list is: dequeued The list is: dequeued The list is: dequeued The list is empty All nodes destroyed All nodes destroyed

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output processing an integer Queue The list is: 0 The list is: 0 1 The list is: The list is: dequeued The list is: dequeued The list is: dequeued The list is: 3 3 dequeued The list is empty processing a double Queue The list is: 1.1 The list is: The list is: The list is: dequeued The list is: dequeued The list is:

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output 3.3 dequeued The list is: dequeued The list is empty All nodes destroyed All nodes destroyed

 2000 Deitel & Associates, Inc. All rights reserved Trees Tree nodes contain two or more links – all other data structures we have discussed only contain one Binary trees –all nodes contain two links none, one, or both of which may be NULL –The root node is the first node in a tree. –Each link in the root node refers to a child –A node with no children is called a leaf node

 2000 Deitel & Associates, Inc. All rights reserved Trees (II) binary search tree –values in left subtree less than parent –values in right subtree greater than parent –facilitates duplicate elimination –fast searches - for a balanced tree, maximum of log n comparisons

 2000 Deitel & Associates, Inc. All rights reserved Trees (III) Tree traversals: –inorder traversal of a binary search tree prints the node values in ascending order 1. Traverse the left subtree with an inorder traversal. 2. Process the value in the node (i.e., print the node value). 3. Traverse the right subtree with an inorder traversal. –preorder traversal: 1. Process the value in the node. 2. Traverse the left subtree with a preorder traversal. 3. Traverse the right subtree with a preorder traversal. –postorder traversal: 1. Traverse the left subtree with a postorder traversal. 2. Traverse the right subtree with a postorder traversal. 3. Process the value in the node.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Class definition 1.1 Member functions 1.2 Member variables Load header 1// Fig : treenode.h 2// Definition of class TreeNode 3#ifndef TREENODE_H 4#define TREENODE_H 5 6template class Tree; // forward declaration 7 8template 9class TreeNode { 10 friend class Tree ; 11public: 12 TreeNode( const NODETYPE &d ) 13 : leftPtr( 0 ), data( d ), rightPtr( 0 ) { } 14 NODETYPE getData() const { return data; } 15private: 16 TreeNode *leftPtr; // pointer to left subtree 17 NODETYPE data; 18 TreeNode *rightPtr; // pointer to right subtree 19}; 20 21#endif 22// Fig : tree.h 23// Definition of template class Tree 24#ifndef TREE_H 25#define TREE_H 26 27#include 28#include 29#include "treenode.h" 30 31using std::endl; 32 33template Trees contain two pointers per node.

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.2 Class definition 1.3 Function prototypes 1.4 Function definitions 34class Tree { 35public: 36 Tree(); 37 void insertNode( const NODETYPE & ); 38 void preOrderTraversal() const; 39 void inOrderTraversal() const; 40 void postOrderTraversal() const; 41private: 42 TreeNode *rootPtr; // utility functions 45 void insertNodeHelper( 46 TreeNode **, const NODETYPE & ); 47 void preOrderHelper( TreeNode * ) const; 48 void inOrderHelper( TreeNode * ) const; 49 void postOrderHelper( TreeNode * ) const; 50}; 51 52template 53Tree ::Tree() { rootPtr = 0; } 54 55template 56void Tree ::insertNode( const NODETYPE &value ) 57 { insertNodeHelper( &rootPtr, value ); } 58 59// This function receives a pointer to a pointer so the 60// pointer can be modified. 61template 62void Tree ::insertNodeHelper( 63 TreeNode **ptr, const NODETYPE &value ) 64{

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.4 Function definitions 65 if ( *ptr == 0 ) { // tree is empty 66 *ptr = new TreeNode ( value ); 67 assert( *ptr != 0 ); 68 } 69 else // tree is not empty 70 if ( value data ) 71 insertNodeHelper( &( ( *ptr )->leftPtr ), value ); 72 else 73 if ( value > ( *ptr )->data ) 74 insertNodeHelper( &( ( *ptr )->rightPtr ), value ); 75 else 76 cout << value << " dup" << endl; 77} 78 79template 80void Tree ::preOrderTraversal() const 81 { preOrderHelper( rootPtr ); } 82 83template 84void Tree ::preOrderHelper( 85 TreeNode *ptr ) const 86{ 87 if ( ptr != 0 ) { 88 cout data << ' '; 89 preOrderHelper( ptr->leftPtr ); 90 preOrderHelper( ptr->rightPtr ); 91 } 92} 93 94template 95void Tree ::inOrderTraversal() const 96 { inOrderHelper( rootPtr ); } 97 Traversals are recursively defined

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1.4 Function definitions 98template 99void Tree ::inOrderHelper( 100 TreeNode *ptr ) const 101{ 102 if ( ptr != 0 ) { 103 inOrderHelper( ptr->leftPtr ); 104 cout data << ' '; 105 inOrderHelper( ptr->rightPtr ); 106 } 107} template 110void Tree ::postOrderTraversal() const 111 { postOrderHelper( rootPtr ); } template 114void Tree ::postOrderHelper( 115 TreeNode *ptr ) const 116{ 117 if ( ptr != 0 ) { 118 postOrderHelper( ptr->leftPtr ); 119 postOrderHelper( ptr->rightPtr ); 120 cout data << ' '; 121 } 122} #endif

 2000 Deitel & Associates, Inc. All rights reserved. Outline 1. Load header 1.1 Initialize object 2. Input values 3. Output 125// Fig : fig15_16.cpp 126// Driver to test class Tree 127#include 128#include 129#include "tree.h" using std::cout; 132using std::cin; 133using std::setiosflags; 134using std::ios; 135using std::setprecision; int main() 138{ 139 Tree intTree; 140 int intVal, i; cout << "Enter 10 integer values:\n"; 143 for ( i = 0; i < 10; i++ ) { 144 cin >> intVal; 145 intTree.insertNode( intVal ); 146 } cout << "\nPreorder traversal\n"; 149 intTree.preOrderTraversal(); cout << "\nInorder traversal\n"; 152 intTree.inOrderTraversal(); cout << "\nPostorder traversal\n"; 155 intTree.postOrderTraversal(); 156 Enter 10 integer values: Preorder traversal Inorder traversal Postorder traversal

 2000 Deitel & Associates, Inc. All rights reserved. Outline 157 Tree doubleTree; 158 double doubleVal; cout << "\n\n\nEnter 10 double values:\n" 161 << setiosflags( ios::fixed | ios::showpoint ) 162 << setprecision( 1 ); 163 for ( i = 0; i < 10; i++ ) { 164 cin >> doubleVal; 165 doubleTree.insertNode( doubleVal ); 166 } cout << "\nPreorder traversal\n"; 169 doubleTree.preOrderTraversal(); cout << "\nInorder traversal\n"; 172 doubleTree.inOrderTraversal(); cout << "\nPostorder traversal\n"; 175 doubleTree.postOrderTraversal(); return 0; 178} create a double tree 2. Input values 3. Output Enter 10 double values: Preorder traversal Inorder traversal Postorder traversal

 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output Enter 10 integer values: Preorder traversal Inorder traversal Postorder traversal Enter 10 double values: Preorder traversal Inorder traversal Postorder traversal