Chapter 15 Data Structures Outline 15 1 Introduction







































- Slides: 39
Chapter 15 – Data Structures Outline 15. 1 Introduction 15. 2 Self-Referential Classes 15. 3 Dynamic Memory Allocation 15. 4 Linked Lists 15. 5 Stacks 15. 6 Queues 15. 7 Trees 2000 Deitel & Associates, Inc. All rights reserved.
15. 1 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.
15. 2 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 15 Data member and pointer 2000 Deitel & Associates, Inc. All rights reserved. 10 NULL pointer (points to nothing)
15. 2 Self-Referential Classes (II) class Node { public: Node( int ); void set. Data( int ); int get. Data() const; void set. Next. Ptr( Node * ); const Node *get. Next. Ptr() const; private: int data; Node *next. Ptr; }; • next. Ptr - 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.
15. 3 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 *new. Ptr = new Node( 10 ); • allocates sizeof( Node ) bytes • runs the Node constructor and stores pointer to that memory in new. Ptr • throws a bad_alloc exception if no memory is available • delete – calls Node destructor and deallocates memory allocated with new – delete new. Ptr; • new. Ptr is not deleted; the space new. Ptr points to is deallocated 2000 Deitel & Associates, Inc. All rights reserved.
15. 4 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.
15. 4 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.
1 // Fig. 15. 3: listnd. h 2 // List. Node template definition 3 #ifndef LISTND_H 4 #define LISTND_H Outline 5 6 template< class NODETYPE > class List; // forward declaration 7 8 template<class NODETYPE> 9 class List. Node { 10 Creating a Linked List 1. Class definition friend class List< NODETYPE >; // make List a friend 11 public: 12 List. Node( const NODETYPE & ); // constructor 13 NODETYPE get. Data() const; // return data in the node 1. 1 Function definitions 14 private: 15 NODETYPE data; // data 16 List. Node< NODETYPE > *next. Ptr; // next node in the list 17 }; Nodes contain data and a pointer 18 19 // Constructor 20 template<class NODETYPE> 21 List. Node< NODETYPE >: : List. Node( const NODETYPE &info ) 22 : data( info ), next. Ptr( 0 ) { } 23 24 // Return a copy of the data in the node 25 template< class NODETYPE > 26 NODETYPE List. Node< NODETYPE >: : get. Data() const { return data; } 27 2000 Deitel & Associates, Inc. All rights reserved. 28 #endif
29 // Fig. 15. 3: list. h 30 // Template List class definition 31 #ifndef LIST_H 32 #define LIST_H 33 34 #include <iostream> 35 #include <cassert> 36 #include "listnd. h" 37 38 using std: : cout; 39 40 template< class NODETYPE > 41 class List { 42 public: 43 List(); // constructor 44 ~List(); // destructor 45 void insert. At. Front( const NODETYPE & ); 46 void insert. At. Back( const NODETYPE & ); List has two 47 bool remove. From. Front( NODETYPE & ); new nodes 48 bool remove. From. Back( NODETYPE & ); 49 bool is. Empty() const; 50 void print() const; 51 private: 52 List. Node< NODETYPE > *first. Ptr; // pointer to first node 53 List. Node< NODETYPE > *last. Ptr; // pointer to last node 54 55 // Utility function to allocate a new node 56 List. Node< NODETYPE > *get. New. Node( const NODETYPE & ); 57 }; 58 59 // Default constructor 60 template< class NODETYPE > 2000 Deitel & Associates, Inc. All rights reserved. 61 List< NODETYPE >: : List() : first. Ptr( 0 ), last. Ptr( 0 ) { } Outline 1. Load header 1. 1 Class definition 1. 2 Function definitions 1. 3 Constructor pointers and can create
62 63 // Destructor 64 template< class NODETYPE > 65 List< NODETYPE >: : ~List() 66 { 67 if ( !is. Empty() ) { // List is not empty 1. 4 Destructor 68 cout << "Destroying nodes. . . n"; 69 70 List. Node< NODETYPE > *current. Ptr = first. Ptr, *temp. Ptr; 1. 5 insert. At. Front 71 72 while ( current. Ptr != 0 ) { // delete remaining nodes 73 temp. Ptr = current. Ptr; 74 cout << temp. Ptr->data << 'n'; 75 current. Ptr = current. Ptr->next. Ptr; 76 delete temp. Ptr; 77 } Destructor destroys the linked list 78 } and nodes 79 80 cout << "All nodes destroyednn"; 81 } 82 83 // Insert a node at the front of the list 84 template< class NODETYPE > 85 void List< NODETYPE >: : insert. At. Front( const NODETYPE &value ) 86 { 87 List. Node< NODETYPE > *new. Ptr = get. New. Node( value ); 88 89 if ( is. Empty() ) // List is empty Sets the new node to point to 90 first. Ptr = last. Ptr = new. Ptr; 91 else { // List is not empty what first. Ptr points to, 92 new. Ptr->next. Ptr = first. Ptr; then sets first. Ptr to the 93 first. Ptr = new. Ptr; new node 94 } 2000 95 } Deitel & Associates, Inc. All rights reserved. Outline
96 97 // Insert a node at the back of the list 98 template< class NODETYPE > 99 void List< NODETYPE >: : insert. At. Back( const NODETYPE &value ) 100 { 101 List. Node< NODETYPE > *new. Ptr = get. New. Node( value ); 1. 6 insert. At. Back 102 103 if ( is. Empty() ) // List is empty 104 first. Ptr = last. Ptr = new. Ptr; 1. 7 remove. From. Front 105 else { // List is not empty 106 last. Ptr->next. Ptr = new. Ptr; The last node points to the new node, 107 last. Ptr = new. Ptr; then last. Ptr points to the new node. 108 } 109 } 110 111 // Delete a node from the front of the list 112 template< class NODETYPE > 113 bool List< NODETYPE >: : remove. From. Front( NODETYPE &value ) 114 { 115 if ( is. Empty() ) // List is empty 116 return false; // delete unsuccessful 117 else { 118 List. Node< NODETYPE > *temp. Ptr = first. Ptr; 119 Move first. Ptr to second node 120 if ( first. Ptr == last. Ptr ) and delete first node. 121 first. Ptr = last. Ptr = 0; 122 else 123 first. Ptr = first. Ptr->next. Ptr; 124 125 value = temp. Ptr->data; // data being removed 126 delete temp. Ptr; 127 return true; // delete successful 128 } 2000 129 } Deitel & Associates, Inc. All rights reserved. Outline
130 131 // Delete a node from the back of the list 132 template< class NODETYPE > 133 bool List< NODETYPE >: : remove. From. Back( NODETYPE &value ) 134 { 135 if ( is. Empty() ) 136 return false; // delete unsuccessful 137 else { 138 List. Node< NODETYPE > *temp. Ptr = last. Ptr; 139 140 if ( first. Ptr == last. Ptr ) 141 first. Ptr = last. Ptr = 0; 142 else { 143 List. Node< NODETYPE > *current. Ptr = first. Ptr; 144 145 while ( current. Ptr->next. Ptr != last. Ptr ) 146 current. Ptr = current. Ptr->next. Ptr; 147 148 last. Ptr = current. Ptr; 149 current. Ptr->next. Ptr = 0; 150 } 151 152 value = temp. Ptr->data; 153 delete temp. Ptr; 154 return true; // delete successful 155 } 156 } 157 158 // Is the List empty? 159 template< class NODETYPE > 160 bool List< NODETYPE >: : is. Empty() const 161 { return first. Ptr == 0; } 162 2000 Deitel & Associates, Inc. to All a rights reserved. 163 // return a pointer newly allocated node Outline 1. 7 remove. From. Back 1. 8 is. Empty Change last. Ptr to the second to last node and delete the last node
164 template< class NODETYPE > 165 List. Node< NODETYPE > *List< NODETYPE >: : get. New. Node( 166 const NODETYPE &value ) 167 { 168 List. Node< NODETYPE > *ptr = 169 new List. Node< NODETYPE >( value ); 1. 9 get. New. Node Create a new node and return a 170 assert( ptr != 0 ); 171 return ptr; pointer to it. 1. 10 print 172 } 173 174 // Display the contents of the List 175 template< class NODETYPE > 176 void List< NODETYPE >: : print() const 177 { 178 if ( is. Empty() ) { 179 cout << "The list is emptynn"; 180 return; 181 } 182 183 List. Node< NODETYPE > *current. Ptr = first. Ptr; 184 Walk through list and print node 185 cout << "The list is: "; 186 values. 187 while ( current. Ptr != 0 ) { 188 cout << current. Ptr->data << ' '; 189 current. Ptr = current. Ptr->next. Ptr; 190 } 191 192 cout << "nn"; 193 } 194 2000 Deitel & Associates, Inc. All rights reserved. 195 #endif Outline
196 // Fig. 15. 3: fig 15_03. cpp 197 // List class test 198 #include <iostream> 199 #include "list. h" 200 201 using std: : cin; 202 using std: : endl; 203 204 // Function to test an integer List 205 template< class T > 206 void test. List( List< T > &list. Object, const char *type ) 207 { 208 cout << "Testing a List of " << type << " valuesn"; 209 210 instructions(); 211 int choice; 212 T value; 213 214 do { 215 cout << "? "; 216 cin >> choice; 217 218 switch ( choice ) { 219 case 1: 220 cout << "Enter " << type << ": "; 221 cin >> value; 222 list. Object. insert. At. Front( value ); 223 list. Object. print(); 224 break; 225 case 2: 226 cout << "Enter " << type << ": "; 2000 Deitel & Associates, All rights reserved. 227 cin >>Inc. value; Outline 1. Load header 1. 1 Function definition
228 list. Object. insert. At. Back( value ); 229 list. Object. print(); 230 break; 231 case 3: 232 if ( list. Object. remove. From. Front( value ) ) 233 cout << value << " removed from listn"; 234 235 list. Object. print(); 236 break; 237 case 4: 238 if ( list. Object. remove. From. Back( value ) ) 239 cout << value << " removed from listn"; 240 241 list. Object. print(); 242 break; 243 } 244 } while ( choice != 5 ); 245 246 cout << "End list testnn"; 247 } 248 249 void instructions() 250 { 251 cout << "Enter one of the following: n" 252 << " 1 to insert at beginning of listn" 253 << " 2 to insert at end of listn" 254 << " 3 to delete from beginning of listn" 255 << " 4 to delete from end of listn" 256 << " 5 to end list processingn"; 257 } 2000 Deitel & Associates, Inc. All rights reserved. 258 Outline 1. 1 Function definition Choices correspond to the switch statement
259 int main() Outline 260 { 261 List< int > integer. List; 262 test. List( integer. List, "integer" ); // test integer. List 1. Initialize objects 264 List< double > double. List; 2. Function calls 265 test. List( double. List, "double" ); 263 // test double. List 266 267 return 0; 268 } 2000 Deitel & Associates, Inc. All rights reserved. Use templates to create an integer list and a double list.
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 1 3 ? 2 Enter integer: 4 The list is: 2 1 3 4 ? 3 2 removed from list The list is: 1 3 4 ? 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. 2 1. 1 ? 2 Enter double: 3. 3 The list is: 2. 2 1. 1 3. 3 ? 2 Enter double: 4. 4 The list is: 2. 2 1. 1 3. 3 4. 4 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output
? 3 2. 2 removed from list The list is: 1. 1 3. 3 4. 4 ? 3 1. 1 removed from list The list is: 3. 3 4. 4 ? 4 4. 4 removed from list The list is: 3. 3 ? 4 3. 3 removed from list The list is empty ? 5 End list test All nodes destroyed 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output
15. 5 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.
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 1. Load header 7 #include "list. h" 8 9 template< class STACKTYPE > 1. 1 Member functions 10 class Stack : private List< STACKTYPE > { 11 public: 12 void push( const STACKTYPE &d ) { insert. At. Front( d ); } -----------13 bool pop( STACKTYPE &d ) { return remove. From. Front( d ); } 14 bool is. Stack. Empty() const { return is. Empty(); } 15 void print. Stack() const { print(); } 1. Load header 16 }; Notice the functions Stack has: 17 18 #endif insert. At. Front andobjects 1. 1(push) Initialize 19 // Fig. 15. 9: fig 15_09. cpp remove. From. Front (pop) 20 // Driver to test the template Stack class 21 #include <iostream> 2. Modify objects 22 #include "stack. h" 23 3. Output 24 using std: : endl; 25 26 int main() 27 { 28 Stack< int > int. Stack; 29 int pop. Integer, i; processing an integer Stack 30 cout << "processing an integer Stack" << endl; 31 32 for ( i = 0; i < 4; i++ ) { 2000 Deitel & Associates, Inc. All rights reserved. 33 int. Stack. push( i ); Outline
34 35 int. Stack. print. Stack(); The list is: 0 36 37 The list is: 1 0 while ( !int. Stack. is. Stack. Empty() ) { 38 int. Stack. pop( pop. Integer ); 39 40 41 } The list is: 2 1 0 cout << pop. Integer << " popped from stack" << 3 endl; popped from stack int. Stack. print. Stack(); Theis: list The list 3 2 is: 1 02 1 0 42 3. Output 2 popped from stack 43 Stack< double > double. Stack; 44 double val = 1. 1, popdouble; 45 cout << "processing a double Stack" << endl; The list is: 1 0 46 47 for ( i = 0; i < 4; i++ ) { 48 double. Stack. push( val ); 49 double. Stack. print. Stack(); 50 val += 1. 1; 51 Outline } a stack double Stack 1 processing popped from The list 4. 4 is: popped 0 from stack The list is: 1. 1 The list is: 3. 3 2. 2 1. 1 0 popped from stack The list is: 2. 2 1. 1 The list 3. 3 is empty popped from stack } The list is: 2. 2 1. 1 The list is: 3. 3 2. 2 1. 1 while ( !double. Stack. is. Stack. Empty() ) { 2. 2 popped from stack The list is: 4. 4 3. 3 2. 2 1. 1 The list is: 1. 1 52 53 54 double. Stack. pop( popdouble ); 55 cout << popdouble << " popped from stack" << endl; 56 double. Stack. print. Stack(); 57 } 58 return 0; 2000 59 } Deitel & Associates, Inc. All rights reserved. All nodes destroyed 1. 1 popped from stack The list is empty
processing an integer Stack The list is: 0 Outline The list is: 1 0 The list is: 2 1 0 The list is: 3 2 1 0 3 popped from stack The list is: 2 1 0 2 popped from stack The list is: 1 0 1 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: 2. 2 1. 1 The list is: 3. 3 2. 2 1. 1 The list is: 4. 4 3. 3 2. 2 1. 1 2000 Deitel & Associates, Inc. All rights reserved. Program Output
4. 4 popped from stack The list is: 3. 3 2. 2 1. 1 Outline 3. 3 popped from stack The list is: 2. 2 1. 1 2. 2 popped from stack The list is: 1. 1 popped from stack The list is empty All nodes destroyed 2000 Deitel & Associates, Inc. All rights reserved. Program Output
15. 6 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.
1 // Fig. 15. 12: queue. h 2 // Queue class template definition 3 // Derived from class List 4 #ifndef QUEUE_H 5 #define QUEUE_H 6 1. Load header 7 #include "list. h" 8 9 template< class QUEUETYPE > 1. 1 Class definition 10 class Queue: private List< QUEUETYPE > { 11 public: 12 void enqueue( const QUEUETYPE &d ) { insert. At. Back( d ); } 1. 2 Function 13 bool dequeue( QUEUETYPE &d ) prototypes 14 { return remove. From. Front( d ); } 15 bool is. Queue. Empty() const { return is. Empty(); } 16 void print. Queue() const { print(); } -----------17 }; queue only has limited linked-list operations 18 (insert. At. Back and remove. From. Front) 19 #endif 1. Load header 20 // Fig. 15. 12: fig 15_12. cpp 21 // Driver to test the template Queue class 1. 1 Initialize objects 22 #include <iostream> 23 #include "queue. h" 24 2. Function calls 25 using std: : endl; 26 27 int main() 28 { 29 Queue< int > int. Queue; 30 int dequeue. Integer, i; processing an integer Queue 31 cout << "processing an integer Queue" << endl; 32 2000 for Deitel(& i. Associates, All i++ rights )reserved. 33 = 0; i Inc. < 4; { Outline
34 int. Queue. enqueue( i ); 35 int. Queue. print. Queue(); 36 The list is: 0 } The list is: 0 1 37 38 Outline while ( !int. Queue. is. Queue. Empty() ) { 40 0 dequeued 2. int. Queue. dequeue( dequeue. Integer ); The list is: 0 1 2 The list is: 1 2 3 cout << dequeue. Integer << " dequeued" << endl; 41 int. Queue. print. Queue(); 39 42 } The list is: 2 3 44 Queue< double > double. Queue; 45 double val = 1. 1, dequeuedouble; 2 dequeued 46 cout << "processing a double Queue" << endl; The list is: 3 processing The alist double is: Queue 1. 1 48 49 3 dequeued The list is: 1. 1 2. 2 The list is empty 1. 1 dequeued for ( i = 0; i < 4; i++ ) { 50 double. Queue. enqueue( val ); 51 double. Queue. print. Queue(); 52 val += 1. 1; 53 The is: list 1. 1 is: 2. 2 3. 3 4. 4 The list 2. 2 3. 3 } 2. 2 is: dequeued The list 1. 1 2. 2 3. 3 4. 4 54 55 while ( !double. Queue. is. Queue. Empty() ) { 56 double. Queue. dequeue( dequeuedouble ); 57 cout << dequeuedouble << " dequeued" << endl; 58 double. Queue. print. Queue(); 59 } All nodes destroyed 60 61 3. Output The list is: 1 0 dequeued 1 2 3 43 47 Function calls return 0; 2000 62 } Deitel & Associates, Inc. All rights reserved. All nodes destroyed The list is: 3. 3 4. 4 3. 3 dequeued The list is: 4. 4 dequeued The list is empty
processing an integer Queue The list is: 0 Outline The list is: 0 1 2 3 0 dequeued The list is: 1 2 3 1 dequeued The list is: 2 3 2 dequeued The list is: 3 3 dequeued The list is empty processing a double Queue The list is: 1. 1 2. 2 3. 3 4. 4 1. 1 dequeued The list is: 2. 2 3. 3 4. 4 2. 2 dequeued The list is: 3. 3 4. 4 2000 Deitel & Associates, Inc. All rights reserved. Program Output
3. 3 dequeued The list is: 4. 4 dequeued The list is empty All nodes destroyed 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output
15. 7 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, 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.
15. 7 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 2 n comparisons 47 25 77 11 43 7 17 31 44 2000 Deitel & Associates, Inc. All rights reserved. 65 68 93
15. 7 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.
1 // Fig. 15. 16: treenode. h 2 // Definition of class Tree. Node 3 #ifndef TREENODE_H 4 #define TREENODE_H 5 6 template< class NODETYPE > class Tree; // forward declaration 7 8 template< class NODETYPE > 9 class Tree. Node { 10 friend class Tree< NODETYPE >; 11 public: 12 Tree. Node( const NODETYPE &d ) 13 : left. Ptr( 0 ), data( d ), right. Ptr( 0 ) { } Trees contain 14 NODETYPE get. Data() const { return data; } 15 private: 16 Tree. Node< NODETYPE > *left. Ptr; // pointer to left subtree 17 NODETYPE data; 18 Tree. Node< NODETYPE > *right. Ptr; // pointer to right subtree 19 }; 20 21 #endif 22 // Fig. 15. 16: tree. h 23 // Definition of template class Tree 24 #ifndef TREE_H 25 #define TREE_H 26 27 #include <iostream> 28 #include <cassert> 29 #include "treenode. h" 30 31 using std: : endl; 32 2000 Deitel & Associates, Inc. All rights 33 template< class NODETYPE > reserved. Outline 1. Class definition 1. 1 Member functions 1. 2 Member variables two pointers per node. -----------1. Load header
34 class Tree { 35 public: 36 Tree(); 37 void insert. Node( const NODETYPE & ); 38 void pre. Order. Traversal() const; 39 void in. Order. Traversal() const; 40 void post. Order. Traversal() const; 41 private: 42 Tree. Node< NODETYPE > *root. Ptr; 43 44 // utility functions 45 void insert. Node. Helper( 46 Tree. Node< NODETYPE > **, const NODETYPE & ); 47 void pre. Order. Helper( Tree. Node< NODETYPE > * ) const; 48 void in. Order. Helper( Tree. Node< NODETYPE > * ) const; 49 void post. Order. Helper( Tree. Node< NODETYPE > * ) const; 50 }; 51 52 template< class NODETYPE > 53 Tree< NODETYPE >: : Tree() { root. Ptr = 0; } 54 55 template< class NODETYPE > 56 void Tree< NODETYPE >: : insert. Node( const NODETYPE &value ) 57 { insert. Node. Helper( &root. Ptr, value ); } 58 59 // This function receives a pointer to a pointer so the 60 // pointer can be modified. 61 template< class NODETYPE > 62 void Tree< NODETYPE >: : insert. Node. Helper( 63 Tree. Node< NODETYPE > **ptr, const NODETYPE &value ) 2000 64 { Deitel & Associates, Inc. All rights reserved. Outline 1. 2 Class definition 1. 3 Function prototypes 1. 4 Function definitions
65 if ( *ptr == 0 ) { // tree is empty 66 *ptr = new Tree. Node< NODETYPE >( value ); 67 assert( *ptr != 0 ); 68 } 69 else // tree is not empty 70 if ( value < ( *ptr )->data ) 71 insert. Node. Helper( &( ( *ptr )->left. Ptr ), value ); 72 else 73 if ( value > ( *ptr )->data ) 74 insert. Node. Helper( &( ( *ptr )->right. Ptr ), value ); 75 else 76 cout << value << " dup" << endl; 77 } 78 79 template< class NODETYPE > 80 void Tree< NODETYPE >: : pre. Order. Traversal() const 81 { pre. Order. Helper( root. Ptr ); } Traversals are 82 83 template< class NODETYPE > 84 void Tree< NODETYPE >: : pre. Order. Helper( 85 Tree. Node< NODETYPE > *ptr ) const 86 { 87 if ( ptr != 0 ) { 88 cout << ptr->data << ' '; 89 pre. Order. Helper( ptr->left. Ptr ); 90 pre. Order. Helper( ptr->right. Ptr ); 91 } 92 } 93 94 template< class NODETYPE > 95 void Tree< NODETYPE >: : in. Order. Traversal() const 96 { in. Order. Helper( root. Ptr ); } 2000 Deitel & Associates, Inc. All rights reserved. 97 Outline 1. 4 Function definitions recursively defined
98 template< class NODETYPE > 99 void Tree< NODETYPE >: : in. Order. Helper( 100 Outline Tree. Node< NODETYPE > *ptr ) const 101 { 102 if ( ptr != 0 ) { 103 in. Order. Helper( ptr->left. Ptr ); 104 cout << ptr->data << ' '; 105 in. Order. Helper( ptr->right. Ptr ); 106 } 107 } 108 109 template< class NODETYPE > 110 void Tree< NODETYPE >: : post. Order. Traversal() const 111 { post. Order. Helper( root. Ptr ); } 112 113 template< class NODETYPE > 114 void Tree< NODETYPE >: : post. Order. Helper( 115 Tree. Node< NODETYPE > *ptr ) const 116 { 117 if ( ptr != 0 ) { 118 post. Order. Helper( ptr->left. Ptr ); 119 post. Order. Helper( ptr->right. Ptr ); 120 cout << ptr->data << ' '; 121 } 122 } 123 2000 Deitel & Associates, Inc. All rights reserved. 124 #endif 1. 4 Function definitions
125 // Fig. 15. 16: fig 15_16. cpp 126 // Driver to test class Tree 127 #include <iostream> 128 #include <iomanip> 129 #include "tree. h" 130 1. Load header 131 using std: : cout; 132 using std: : cin; 1. 1 Initialize object 133 using std: : setiosflags; 134 using std: : ios; 135 using std: : setprecision; 2. Input values 136 137 int main() 138 { 3. Output 139 Tree< int > int. Tree; 140 int. Val, i; 141 142 cout << "Enter 10 integer values: n"; Enter 10 integer values: 143 for ( i = 0; i < 10; i++ ) { 50 25 75 12 33 67 88 6 13 68 144 cin >> int. Val; 145 int. Tree. insert. Node( int. Val ); Preorder traversal 146 } 147 50 25 12 6 13 33 75 67 68 88 148 cout << "n. Preorder traversaln"; 149 int. Tree. pre. Order. Traversal(); Inorder traversal 150 6 12 13 25 33 50 67 68 75 88 151 cout << "n. Inorder traversaln"; 152 int. Tree. in. Order. Traversal(); Postorder traversal 153 154 cout << "n. Postorder traversaln"; 6 13 12 33 25 68 67 88 75 50 155 int. Tree. post. Order. Traversal(); 2000 Deitel & Associates, Inc. All rights reserved. 156 Outline
157 Tree< double > double. Tree; 158 double. Val; create a double tree Outline 159 160 cout << "nnn. Enter 10 double values: n" 161 << setiosflags( ios: : fixed | ios: : showpoint ) 162 << setprecision( 1 ); 163 for ( i = 0; i < 10; i++ ) { Enter 10 double values: 3. Output 39. 2 16. 5 82. 7 3. 3 65. 2 90. 8 1. 1 4. 4 89. 5 92. 5 164 cin >> double. Val; 165 double. Tree. insert. Node( double. Val ); 166 2. Input values } 167 168 cout << "n. Preorder traversaln"; Preorder traversal 169 double. Tree. pre. Order. Traversal(); 39. 2 16. 5 3. 3 1. 1 4. 4 82. 7 65. 2 90. 8 89. 5 92. 5 170 171 cout << "n. Inorder traversaln"; 172 double. Tree. in. Order. Traversal(); Inorder traversal 1. 1 3. 3 4. 4 16. 5 39. 2 65. 2 82. 7 89. 5 90. 8 92. 5 173 174 cout << "n. Postorder traversaln"; 175 double. Tree. post. Order. Traversal(); 176 177 return 0; 178 } Deitel & Associates, Inc. All rights reserved. 2000 Postorder traversal 1. 1 4. 4 3. 3 16. 5 65. 2 89. 5 92. 5 90. 8 82. 7 39. 2
Enter 10 integer values: 50 25 75 12 33 67 88 6 13 68 Preorder traversal 50 25 12 6 13 33 75 67 68 88 Inorder traversal 6 12 13 25 33 50 67 68 75 88 Postorder traversal 6 13 12 33 25 68 67 88 75 50 Enter 10 double values: 39. 2 16. 5 82. 7 3. 3 65. 2 90. 8 1. 1 4. 4 89. 5 92. 5 Preorder traversal 39. 2 16. 5 3. 3 1. 1 4. 4 82. 7 65. 2 90. 8 89. 5 92. 5 Inorder traversal 1. 1 3. 3 4. 4 16. 5 39. 2 65. 2 82. 7 89. 5 90. 8 92. 5 Postorder traversal 1. 1 4. 4 3. 3 16. 5 65. 2 89. 5 92. 5 90. 8 82. 7 39. 2 2000 Deitel & Associates, Inc. All rights reserved. Outline Program Output