data structures slides

93
2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved. Outline Data Structures and Algorithm ITM 107 Introduction Correct Program DS and its types Abstract Data Types Elizabeth Sherly - Faculty Raj Mathew - Faculty Associate Smitha Rani - Project Associate

Upload: prasad-borole

Post on 13-Apr-2015

106 views

Category:

Documents


0 download

DESCRIPTION

data structures

TRANSCRIPT

Page 1: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Data Structures and Algorithm –

ITM 107

IntroductionCorrect ProgramDS and its typesAbstract Data Types

Elizabeth Sherly - Faculty

Raj Mathew - Faculty Associate

Smitha Rani - Project Associate

Page 2: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Objective of the Course• DSA mainly focuses on the fundamentals of

Data Structures and Algorithms and how to use these concepts in problem solving.

• The course aims to find the appropriate abstraction to solve complex problems using appropriate data structures like stacks, queue, lists, trees etc.

• Great emphasis is given in the analysis of algorithms mainly in search and sort algorithms.

• The problem solving paradigms is very important in this course, so a deep knowledge in programming is essential in this course.

• The program can be written mainly in C++, however some exercises will be given in JAVA also. Use Solaris Lab

Page 3: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Pre requisite

• Basic Knowledge in C/C++/JAVA

• Discrete Mathematics

• Problem solving Logics and basic Algorithms

Page 4: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

References• Data Structures using C and C++

– Y Langsam, M J Augenstein, A. M Tenenbaum

• “Data Structures and Algorithms in C++”, Thomson Learning 2000

– Adam Drozdek• Data Structures and Algorithms in JAVA by

Adam Drosdek, Vikas Publishing House, New Delhi, 2000.

• The C++ Programming Language, Second Edition, Addison-Wesley Publishing 1995

– Stroustrup B,

Page 5: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Websites

• http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/ds_ToC.html– John Morris,

Electrical and Electronic Engineering,University of Western Australia

Deitel & Associates Web site

Page 6: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Good Programs

• There are a number of facets to good programs: they must a. run correctly b.run efficiently c.be easy to read and understand d.be easy to debug and e.be easy to modify. What does correct mean?We need to have some formal notion of the

meaning of correct: "run in accordance with the specifications".

Page 7: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Some definitions • correct

• A correct program runs in accordance with its specifications

• algorithm • A precisely specified procedure for solving a problem.

• abstraction • high level views of objects or functions which enable us to

forget about the low level details and concentrate on the problem at hand.

• An abstract data type is a data structure and a collection of functions or procedures which operate on the data structure.

Page 8: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Data Structures

DS includes

• Logical or mathematical description of the structure

and Implementation of the structure on a computer

• Quantitative analysis of the structure, which includes

determining the amount of memory needed to store

the structure and the time required to process the

structure.

Page 9: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 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

Page 10: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

ADTs Collection• ADT is a data structure and a set of operations which can

be performed on it. – A class in object-oriented design is an ADT

• The pre-conditions define a state of the program which the client guarantees will be true before calling any method,

• post-conditions define the state of the program that the object's method will guarantee to create for you when it returns.

• create Create a new collection

• add Add an item to a collection

• delete Delete an item from a collection find Find an item matching some criterion in the collection

• destroy Destroy the collection

Page 11: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Example of ADT:Rational Number

• Abstract typedef<integer,integer>RATIONAL• Condition RATIONAL[1] !=0;• /* Operator Defn*/• Abstract RATIONAL makerational(a,b)• int a,b;• Precondition b!=0;• Postcondition makerational[0]= =a;• makerational[1]= =a;• Abstract RATIONAL add(a,b)• RATIONAL a,b;• Postcondition add[1]= =a[1]*b[1];• add[0]= =a[0]*b[1]+b[0]*a[1];• Abstract RATIONAL mult(a,b)• RATIONAL a,b;• Postcondition mult[0]= =a[0]*b[0];• mult[1]= =a[1]*b[1];

Page 12: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

The Array as an ADT• abstract typedef <<eltype, ub>> ARRTYPE(ub,eltype)

• Condition type(ub)==int;

• Abstract eltype extract(a,i)

• ARRTYPE(ub,eltype)a;

• int i;

• precondition 0<=i<ub;

• postcondition extract==ai

• Abstract store(a,i,elt)

• ARRTYPE(ub,eltype)a;

• int i;

• eltype elt;

• precondition 0<=i<ub;

• postcondition a[i]==elt;

Page 13: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Linked Lists

• linked list – linear collection of self-referential class objects, called nodes,

connected by pointer links– ach "element" has two parts: the value itself ("item", or "data")

and the reference ("next", or "link") – 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

A linked list is a very flexible dynamic data structure: items may be added to it or deleted from it at will.

• http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/lists.html

• Use a linked list instead of an array when– the number of data elements is unpredictable – the list needs to be sorted

Page 14: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Representation of Linked List in Memory

• 9

1

2

3

4

5

6

7

Start

I 2

I 7

I3

T 1

M 6

Kn

Traversing a Linked List

PTR

Page 15: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Inserting and Removing nodes from a list

p=getnode();

info(p) =x;

next(p)=start;

start =p;

p= start;

start=next(p);

x= info(p)

Insertion of a node in between

Insafter(p,x){

q=getnode();

info(q)=x;

next(q)=next(p);

next(p)=q;}

Removal

delafter(p,x)

{ q=next(p);

x=info(q));

next(p)=next(q);

freenode(q);}

struct node {

int info;

struct node *next;}:

typedef struct node *NODEPTR;

NODEPTR getnode()

{

NODEPTR p;

p=new (struct node);

return(p);}

Void freenode(NODEPTR p)

{

Free(p);

}

Page 16: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 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

Page 17: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Circular List

Circular List Application Josephus Problem

read(n);

read(name);

while(name !=END) {

inset name on the circular list;}

while(ther is more than one node in th elist) {

count through n-1 nodes on the list;

Print the name in the nth node;

Delete the n node; } end of while

Print the name of only node in the list

Insertion is similar to linear list, del is slightly changed

Circular list with Header Node

Make a flag marking as a header

An external pointer pts to the header node

Page 18: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

head

tail

left left left

Doubly linked List

struct node {

int info;

struct node *left,*right;

};

Typedef struct node *NODEPTR;

right

If p be a pointer

Left(right(p))=p=right(left(p))

Page 19: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

delete(p,x)

Void delete(NODEPTR p, int x) {

NODEPTR q,r;

(p==NULL){

cout<<“void deletion”;

return; }

x=p->info;

q=p->left;

r=p->right;

q->right=r;

r->left=q;

freenode(p);

return;

Page 20: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

void insertright(NODEPTR p, int x){

If(p==NULL) {

cout <<“void insertion”;

return; }

q=getnode();

q->info=x;

r=p->right;

r->left=q;

q->right=r;

q->left=p;

p->right=q;

return;

}

Page 21: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 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

6 template< class NODETYPE > class List; // forward declaration

7

8 template<class NODETYPE>

9 class ListNode {

10 friend class List< NODETYPE >; // make List a friend

11 public:

12 ListNode( const NODETYPE & ); // constructor

13 NODETYPE getData() const; // return data in the node

14 private:

15 NODETYPE data; // data

16 ListNode< NODETYPE > *nextPtr; // next node in the list

17 };

18

19 // Constructor

20 template<class NODETYPE>

21 ListNode< NODETYPE >::ListNode( const NODETYPE &info )

22 : data( info ), nextPtr( 0 ) { }

23

24 // Return a copy of the data in the node

25 template< class NODETYPE >

26 NODETYPE ListNode< NODETYPE >::getData() const { return data; }

27

28 #endif

Nodes contain data and a pointer

Page 22: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 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.h30 // Template List class definition31 #ifndef LIST_H32 #define LIST_H3334 #include <iostream>35 #include <cassert>36 #include "listnd.h"3738 using std::cout;3940 template< class NODETYPE >41 class List {42 public:43 List(); // constructor44 ~List(); // destructor45 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;51 private:52 ListNode< NODETYPE > *firstPtr; // pointer to first node53 ListNode< NODETYPE > *lastPtr; // pointer to last node5455 // Utility function to allocate a new node56 ListNode< NODETYPE > *getNewNode( const NODETYPE & );57 };5859 // Default constructor60 template< class NODETYPE >61 List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { }

List has two pointers and can create new nodes

Page 23: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.4 Destructor

1.5 insertAtFront

6263 // Destructor64 template< class NODETYPE >65 List< NODETYPE >::~List()66 {67 if ( !isEmpty() ) { // List is not empty68 cout << "Destroying nodes ...\n";6970 ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;7172 while ( currentPtr != 0 ) { // delete remaining nodes73 tempPtr = currentPtr;74 cout << tempPtr->data << '\n';75 currentPtr = currentPtr->nextPtr;76 delete tempPtr;77 }78 }7980 cout << "All nodes destroyed\n\n";81 }8283 // Insert a node at the front of the list84 template< class NODETYPE >85 void List< NODETYPE >::insertAtFront( const NODETYPE &value )86 {87 ListNode< NODETYPE > *newPtr = getNewNode( value );8889 if ( isEmpty() ) // List is empty90 firstPtr = lastPtr = newPtr;91 else { // List is not empty92 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

Page 24: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.6 insertAtBack

1.7 removeFromFront

9697 // Insert a node at the back of the list98 template< class NODETYPE >99 void List< NODETYPE >::insertAtBack( const NODETYPE &value )100{101 ListNode< NODETYPE > *newPtr = getNewNode( value );102103 if ( isEmpty() ) // List is empty104 firstPtr = lastPtr = newPtr;105 else { // List is not empty106 lastPtr->nextPtr = newPtr;107 lastPtr = newPtr;108 }109}110111// Delete a node from the front of the list112template< class NODETYPE >113bool List< NODETYPE >::removeFromFront( NODETYPE &value )114{115 if ( isEmpty() ) // List is empty116 return false; // delete unsuccessful117 else {118 ListNode< NODETYPE > *tempPtr = firstPtr;119120 if ( firstPtr == lastPtr )121 firstPtr = lastPtr = 0;122 else123 firstPtr = firstPtr->nextPtr;124125 value = tempPtr->data; // data being removed126 delete tempPtr;127 return true; // delete successful128 }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.

Page 25: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.7 removeFromBack

1.8 isEmpty

130131// Delete a node from the back of the list132template< class NODETYPE >133bool List< NODETYPE >::removeFromBack( NODETYPE &value )134{135 if ( isEmpty() )136 return false; // delete unsuccessful137 else {138 ListNode< NODETYPE > *tempPtr = lastPtr;139140 if ( firstPtr == lastPtr )141 firstPtr = lastPtr = 0;142 else {143 ListNode< NODETYPE > *currentPtr = firstPtr;144145 while ( currentPtr->nextPtr != lastPtr )146 currentPtr = currentPtr->nextPtr;147148 lastPtr = currentPtr;149 currentPtr->nextPtr = 0;150 }151152 value = tempPtr->data;153 delete tempPtr;154 return true; // delete successful155 }156}157158// Is the List empty?159template< class NODETYPE > 160bool List< NODETYPE >::isEmpty() const 161 { return firstPtr == 0; }162163// return a pointer to a newly allocated node

Change lastPtr to the second to last node and delete the last node

Page 26: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.9 getNewNode

1.10 print

164template< class NODETYPE >165ListNode< NODETYPE > *List< NODETYPE >::getNewNode( 166 const NODETYPE &value )167{168 ListNode< NODETYPE > *ptr = 169 new ListNode< NODETYPE >( value );170 assert( ptr != 0 );171 return ptr;172}173174// Display the contents of the List175template< class NODETYPE >176void List< NODETYPE >::print() const177{178 if ( isEmpty() ) {179 cout << "The list is empty\n\n";180 return;181 }182183 ListNode< NODETYPE > *currentPtr = firstPtr;184185 cout << "The list is: ";186187 while ( currentPtr != 0 ) {188 cout << currentPtr->data << ' ';189 currentPtr = currentPtr->nextPtr;190 }191192 cout << "\n\n";193}194195#endif

Create a new node and return a pointer to it.

Walk through list and print node values.

Page 27: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load header

1.1 Function definition

196// Fig. 15.3: fig15_03.cpp197// List class test198#include <iostream>199#include "list.h"200201using std::cin;202using std::endl;203204// Function to test an integer List205template< class T >206void testList( List< T > &listObject, const char *type )207{208 cout << "Testing a List of " << type << " values\n";209210 instructions();211 int choice;212 T value;213214 do {215 cout << "? ";216 cin >> choice;217218 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;

Page 28: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 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";

234

235 listObject.print();

236 break;

237 case 4:

238 if ( listObject.removeFromBack( value ) )

239 cout << value << " removed from list\n";

240

241 listObject.print();

242 break;

243 }

244 } while ( choice != 5 );

245

246 cout << "End list test\n\n";

247}

248

249void 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

Page 29: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Initialize objects

2. Function calls

259int main()

260{

261 List< int > integerList;

262 testList( integerList, "integer" ); // test integerList

263

264 List< double > doubleList;

265 testList( doubleList, "double" ); // test doubleList

266

267 return 0;

268}

Use templates to create an integer list and a double list.

Page 30: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

Testing a List of integer valuesEnter 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? 1Enter integer: 1The list is: 1 ? 1Enter integer: 2The list is: 2 1 ? 2Enter integer: 3The list is: 2 1 3 ? 2Enter integer: 4The list is: 2 1 3 4 ? 32 removed from listThe list is: 1 3 4 ? 31 removed from listThe list is: 3 4

Page 31: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

? 44 removed from listThe list is: 3 ? 43 removed from listThe list is empty ? 5End list test

Testing a List of double valuesEnter 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? 1Enter double: 1.1The list is: 1.1 ? 1Enter double: 2.2The list is: 2.2 1.1 ? 2Enter double: 3.3The list is: 2.2 1.1 3.3 ? 2Enter double: 4.4The list is: 2.2 1.1 3.3 4.4

Page 32: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

? 32.2 removed from listThe list is: 1.1 3.3 4.4 ? 31.1 removed from listThe list is: 3.3 4.4 ? 44.4 removed from listThe list is: 3.3 ? 43.3 removed from listThe list is empty ? 5End list test All nodes destroyed All nodes destroyed

Page 33: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Stacks

• A stack is an ordered collection of items, generally implemented with only two principle operations, called Push and Pop.

• stack – new nodes can be added and removed only at the top

• Push adds an item to a stack• Pop extracts the most recently

pushed item from the stack– similar to a pile of dishes– last-in, first-out (LIFO) – Bottom of stack indicated by a link member to null – stores the popped value– constrained version of a linked list

Page 34: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Stack -RelevanceStacks appear in computer programs

•Key to call / return in functions & procedures

•Stack frame allows recursive calls

•Call: push stack frame

•Return: pop stack frame

•Stack frame

•Return address * Function Arguments

•Local variables

Page 35: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Define a Stack

#define STACKSIZE 100

Struct Stack {

int top;

int items[STACKSIZE];

};

Struct Stack s;

Initialize the stack to an empty stack then s.top=-1

Stack can be implemented using array or linked list.

Page 36: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Stack Operations Void push( Stack *s, int x)

- add item to the top of the stack# define Stacksize 100;

void push(Stack s, int x)

{ if ( s->top==Stacksize-1) {

cout <<“Stack overflow”;

exit(1) }

else s->items[++(s->top)]=x;

return; }

int pop( Stack s ) - remove an item from the top of the stackint pop(stack *s)

{if (empty(s)) { cout <<“Stack underflow”; int empty(stack *s)

exit(1); } { if (s->top ==-1) return(TRUE)

return(s->items[s->top--]); } else return (FALSE);

}

Page 37: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

STACK FRAMESThe data structure containing all the data (arguments, local variables, return address, etc) needed each time a procedure or function is called.

When any procedure or function is called, a number of words – the stack frame - is pushed onto a program stack. When the procedure or function returns, this frame of data is popped off the stack. More useful in recursive programmes

Program stack after executing a pair of mutually

recursive functions:

function f(int x, int y)

{ int a; if ( term_cond )

return ...; a = .....; return g(a); }

function g(int z)

{ int p,q; p = ...; q = ...; return f(p,q); }

Page 38: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

POLISH NOTATIONS• Infix, Prefix and Postfix

• If the operator is placed in between operand, it is called infix notation Ex. A+B (A+B)*C A+(B*C)

• prefix notations – operators preceded the operands

• Ex. +AB

• (A+B)*C = [+AB]*C = *+ABC

• A+(B*C) = A+[*BC) = +A*BC

• (A+B)/(C-D)= [+AB]/[-CD] = /+AB-CD

• Postfix- Operator symbol is placed after two operanda

• Ex. A+B= AB+

• (A+B)*C= [AB+]*C= AB+C*

• A+(B*C) = A+[BC*]=ABC*+

Page 39: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Exercises• A$B*C-D+E/F/(G+H)• ((A+B)*C-(D-E))$(F+G)• A-B/(C*D$E)• (A+B$D)/(E-F)+G• A*(B+D)/E-f*(G+H/K)

Page 40: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Evaluation of Expressions• Consider the following infix expression

• A+(B*C-(D/E$F)*G)*H

• Convert into Postfix using Stack

• A +(B* C –(D/E $ F)*G) *H

A+(B*C-(D/E$F)*G)*H

(( +( + (( + (( + ( *( + ( *( + ( -( + (- (( + ( - (( + ( - ( /( + ( - ( /( + (- ( /$( + ( - ( / $( + ( -( + ( - *( + ( - *( +( + * ( + *

AAAA BA BA B CA B C *A B C *A B C * DA B C * DA B C * D EA B C * D EA B C * D E FA B C * D E F $ /A B C * D E F $ / GA B C * D E F $ / G * -A B C * D E F $ / G * -A B C * D E F $ / G * - HA B C * D E F $ / G * - H * +

Page 41: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

EXERCISE

• 6 2 3 + - 3 8 2 / + * 2 $ 3 +

symb op1 Op2 value Stk value

623+-382/+*2$3+

2666683117749

355552477223

511114777494952

66 26 2 36 51 1 31 3 81 3 8 21 3 41 77 7 2 4949 352

Page 42: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load header

1.1 Member functions

-----------------------

1. Load header

1.1 Initialize objects

2. Modify objects

3. Output

1 // Fig. 15.9: stack.h2 // Stack class template definition3 // Derived from class List4 #ifndef STACK_H5 #define STACK_H67 #include "list.h"89 template< class STACKTYPE >10 class Stack : private List< STACKTYPE > {11 public: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 };1718 #endif19 // Fig. 15.9: fig15_09.cpp

20 // Driver to test the template Stack class

21 #include <iostream>

22 #include "stack.h"

23

24 using std::endl;

25

26 int main()

27 {

28 Stack< int > intStack;

29 int popInteger, i;

30 cout << "processing an integer Stack" << endl;

31

32 for ( i = 0; i < 4; i++ ) {

33 intStack.push( i );

Notice the functions Stack has: insertAtFront (push) and removeFromFront (pop)

processing an integer Stack

Page 43: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

3. Output

34 intStack.printStack();

35 }

36

37 while ( !intStack.isStackEmpty() ) {

38 intStack.pop( popInteger );

39 cout << popInteger << " popped from stack" << endl;

40 intStack.printStack();

41 }

42

43 Stack< double > doubleStack;

44 double val = 1.1, popdouble;

45 cout << "processing a double Stack" << endl;

46

47 for ( i = 0; i < 4; i++ ) {

48 doubleStack.push( val );

49 doubleStack.printStack();

50 val += 1.1;

51 }

52

53 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: 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

4.4 popped from stack

The list is: 3.3 2.2 1.1

 

3.3 popped from stack

The list is: 2.2 1.1

 

2.2 popped from stack

The list is: 1.1

 

1.1 popped from stack

The list is emptyAll nodes destroyed

 

All nodes destroyed

Page 44: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

processing an integer StackThe list is: 0  The list is: 1 0  The list is: 2 1 0  The list is: 3 2 1 0  3 popped from stackThe list is: 2 1 0  2 popped from stackThe list is: 1 0  1 popped from stackThe list is: 0  0 popped from stackThe list is empty processing a double StackThe 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

Page 45: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

4.4 popped from stackThe list is: 3.3 2.2 1.1  3.3 popped from stackThe list is: 2.2 1.1  2.2 popped from stackThe list is: 1.1  1.1 popped from stackThe list is empty All nodes destroyed All nodes destroyed

Page 46: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

• Write a stack.h programme with all basic operations of Stack.

• Write a programme to input a set of integers, and double values using Stack operation. The values has to be pushed and poped.

• Write a programme to evaluate a postfix expression

• Write a programme to convert an infix to Prefix and

postfix.

Page 47: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Quicksort – An Application of STACK

• Quick sort is an example of Divide and Conquer algorithm• Suppose A has the following set of elements• 44 33 11 55 77 90 40 60 99 22 88 66• (33 11 40 22) 44 (55 77 90 60 99 88 66)• (11 22 ) 33 (40) 44 (55 77 90 60 99 88 66)• 11 (22) 33 40 44 (55 77 90 60 99 88 66)• 11 22 33 40 44 55 (77 90 60 99 88 66)• 11 22 33 40 44 55 (60 66) 77 (90 88 99)• 11 22 33 40 44 55 60 66 77 (90 88 99)• 11 22 33 40 44 55 60 66 77 (88) 90 (99)• 11 22 33 40 44 55 60 66 77 88 90 99• http://ciips.ee.uwa.edu.au/~morris/Year2/PLDS210/

qsort.html

Page 48: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

The algorithm consists of two steps

1. Partition the list

2. Sort the list

Recursive Algorithm

Let x be an array, n number of elts To be sorted. Choose an element a within the arraySay a=x[0]The elemts of x are are partitioned so that a is place in the Position j holding the following condition1. Each of the elements from 0 to j-1 is less than or equal to a2. Each of the elements from j+1 to n-1 is greater than or equal to a

Page 49: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Algorithm

If (lb>=ub)

return

partition(x,lb,ub,j)

// partition the elements of the sub array

Putting x[lb] in x[j]

1. Check x[i] <=x[j] for lb <=i<j

2. Check x[i] >=x[j] for j<i<=ub

quick(x,lb,j-1)

// recursively sort the sub array between lb and j-1

quick(x,lb,j-1)

//recursively sort the sub array between j+1and ub

Page 50: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Using Stack• Step 1: Repeatedly increase the pointer left by one

position until x[left]>a• Step 2: Repeatedly decrease the pointer right by

one position until x[right] <=a• Step 3: if right >left, interchange x[left] with

x[right]• The process is repeated until step 3 fails.• Interchange x[right] with x[lb]=a, whose position

is sought, and set j to right.

Page 51: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Quicksort Algorithm – Using Stack

Partition PartVoid partition (int x[], int lb, int ub, int *p)

{

Int a, left, temp, right;

a=x[lb]; right = ub; left =lb;

While(left <up) {

While x[left] <= a and left <ub)

Left ++;

While (x[right]>a){ right--;

If (left<right){

//interchange x[left] and x[right] }

}

X[lb]=x[up];

X[up]=a;

*p=up} //end partition

Sort partHere push the ib and ub into stack

And pops them from the stack

Page 52: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Analysing an Algorithm

• Simple statement sequence

s1; s2; …. ; sk

– O(1) as long as k is constant• Simple loops

for(i=0;i<n;i++) { s; } where s is O(1)– Time complexity is n O(1) or O(n)

• Nested loops for(i=0;i<n;i++) for(j=0;j<n;j++) { s; }

– Complexity is n O(n) or O(n2)

This part isO(n)

Page 53: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Analysing an Algorithm

• Loop index depends on outer loop index for(j=0;j<n;j++) for(k=0;k<j;k++){ s; }

– Inner loop executed

• 1, 2, 3, …., n times Complexity O(n2)

Complexity O(n2)

n

i =i=1

n(n+1)2

Distinguish this case -where the iteration count increases (decreases) by a

constant O(nk)from the previous one -where it changes by a factor

O(log n)

Page 54: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Efficiency of Quick sort

• Worse case o(n2) –Which is the worse case

• Average case n(logn)

• Prove ??

Page 55: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

The order of quick sort is nlogn

• Assume that the file size n is a power of 2, say n=2m

• m= log2m

• Assume that proper position for the pivot always turns out to the exact middle of the subarray.

• There are n comparisons in the first sweep and the file is split into two subfiles of n/2. For each file there are n/2 comparisons, and then four files each of size n/4 are formed and so on. Finally there are m subfiles of size 1.

• N+ 2*n/2+4*n/4+8*n/8+…..m*n/n

• N+n+n+… n(mterms)

• Thus the total number of comparison is O(n*m)

• Or O(nlogn)

• Which is relatively efficient.

• In the worse case

The no of comparison is n+(n-1)+(n-2)+…+2+1=(n+1)n/2~O(n2/2)

Page 56: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

RECURSION• Very useful technique

– Definition of mathematical functions– Definition of data structures

• Recursive structures are naturally processed by recursive functions!

• Recursively defined functions– factorial– Fibonacci– GCD by Euclid’s algorithm– Fourier Transform– Games

• Towers of Hanoi• Chess

Page 57: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Factorial

• N!= n (n-1) (n-2) ------- 3,2,1

• If n=0, then N!=1

• If(n>0) n!= n.(n-1)!

• int fact (int n)

• { return(n==0)?1:n*fact(n-1);

• }

Page 58: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

fib( n ) = if ( n = 0 ) then 1 else if ( n = 1 ) then 1 else fib(n-1) + fib(n-2)

int fib( n ) {if ( n < 2 ) return 1;else return fib(n-1) + fib(n-2);}

•Fibonacci Numbers

1 1 2 3 5 8 13 ---------

Page 59: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Towers of Hanoi

Three pegs labeled A, B,C

In A, there are finite number of disks with decreasing size

Objective- move the disks from A to C using B as an auxiliary.

The rules of the game are as follows

• Only one disk may be moved at a time.

• At no time can a larger disk be placed on a smaller disk

A B C

Page 60: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

If n=3

A C A B C B A C B A B C A C

For n disks

1. Move the top n-1 disks from peg A to peg B

2. Move the top disk from peg A to C

3. Move the top n-1 disks from peg B to peg C

Tower(N-1, BEG, END, AUX)

Tower(1,BEG, AUX, END)

Tower(N-1,AUX,BEG,END)

Page 61: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Tower(4,A,B,C) A C

Tower(3, A,C,B)

Tower(3, B,A,C)

A B

Tower (2, A,B,C)

Tower(2, C,A,B)

B C

Tower(2, A,B,C)

Tower(2, B,C,A)

C B

Tower(1,C,B,A)

Tower(1,A,C,B)

Page 62: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Algorithm• Tower(N,BEG,AUX,END)

• If N=1, then BEG END

• return

• Tower(N-1, BEG,END,AUX) // Move N-1 disks from BEG to AUX

• BEG END

• Tower(N-1, AUX, BEG,END)

• return

Page 63: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

15.6 Queues

• A queue is an orderd collection of items from which items may be deleted at one end(front) and into which items are inserted at the other end(rear).

• queue – similar to a supermarket checkout line– first-in, first-out (FIFO)

– nodes are removed only from the head (front)

– nodes are inserted only at the tail(rear)

• The insert and remove operations are known as enqueue and dequeue

Useful in computing– Print spooling, packets in networks, file server requests

Three operations in the queue are

Insert(q,x) x=remove(q) empty(q)

Page 64: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Implementation of Queue

• Array implementation#define MAXQUEUE 100

struct queue{

int items[MAXQUEUE];

int front,rear;

}q;

0

1

2

3

4

q.front=0

q.rear=-1

q.items

0

1

2

3

4

q.items

AB

C

q.front=0

q.rear=2

0

1

2

3

4

q.items

C q.front=q.rear=2

Queue is empty whenever q.rear<q.front

The number of elts in a queue is q-rear-q.front+1

Page 65: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Insert operation• void insert(struct queue *pq, int x)

• {

• /* make room for new elt*/

• if(pq->rear= =MAXQUEUE-1)

• pq->rear=0;

• else

• (pq->rear)++;

• /* check for overflow */

• if(pq->rear==pq->front){

• printf(“Queue overflow”);

• exit(1); }

• pq->item[pq->rear]=x;

• return; }

Page 66: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Remove Operation• int remove(struct queue *pq)

• {

• if(empty(pq) ){ int empty(struct queue *pq)

printf(“Queue underflow”); {

• exit(1); } return((pq->front = =pq->rear)

• if(pq->front= = MAXQUEUE-1) ? TRUE:FALSE); }

• pq->front=0;

• else

• (pq->front)++;

• return (pq->items[pq->front]);

• }

Page 67: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Priority Queue• A priority queue is a collection of elements such

that each element is assigned a priority that order in which elts are deleted or inserted in priority

basis.

Page 68: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load header

1.1 Class definition

1.2 Function prototypes

----------------------

1. Load header

1.1 Initialize objects

2. Function calls

1 // Fig. 15.12: queue.h

2 // Queue class template definition3 // Derived from class List4 #ifndef QUEUE_H5 #define QUEUE_H67 #include "list.h"89 template< class QUEUETYPE >10 class Queue: private List< QUEUETYPE > {11 public: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 };1819 #endif20 // Fig. 15.12: fig15_12.cpp21 // Driver to test the template Queue class22 #include <iostream>23 #include "queue.h"2425 using std::endl;2627 int main()28 {29 Queue< int > intQueue;30 int dequeueInteger, i;31 cout << "processing an integer Queue" << endl;3233 for ( i = 0; i < 4; i++ ) {

queue only has limited linked-list operations (insertAtBack and removeFromFront)

processing an integer Queue

Page 69: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

2. Function calls

3. Output

34 intQueue.enqueue( i );

35 intQueue.printQueue();

36 }

37

38 while ( !intQueue.isQueueEmpty() ) {

39 intQueue.dequeue( dequeueInteger );

40 cout << dequeueInteger << " dequeued" << endl;

41 intQueue.printQueue();

42 }

43

44 Queue< double > doubleQueue;

45 double val = 1.1, dequeuedouble;

46

47 cout << "processing a double Queue" << endl;

48

49 for ( i = 0; i < 4; i++ ) {

50 doubleQueue.enqueue( val );

51 doubleQueue.printQueue();

52 val += 1.1;

53 }

54

55 while ( !doubleQueue.isQueueEmpty() ) {

56 doubleQueue.dequeue( dequeuedouble );

57 cout << dequeuedouble << " dequeued" << endl;

58 doubleQueue.printQueue();

59 }

60

61 return 0;

62 }

The list is: 0

 

The list is: 0 1

 

The list is: 0 1 2

 

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 QueueThe list is: 1.1

 

The list is: 1.1 2.2

 

The list is: 1.1 2.2 3.3

 

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

3.3 dequeuedThe list is: 4.4  4.4 dequeuedThe list is empty

All nodes destroyed

 

All nodes destroyed

Page 70: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

processing an integer QueueThe list is: 0  The list is: 0 1  The list is: 0 1 2  The list is: 0 1 2 3  0 dequeuedThe list is: 1 2 3  1 dequeuedThe list is: 2 3  2 dequeuedThe list is: 3  3 dequeuedThe list is empty processing a double QueueThe list is: 1.1  The list is: 1.1 2.2  The list is: 1.1 2.2 3.3  The list is: 1.1 2.2 3.3 4.4  1.1 dequeuedThe list is: 2.2 3.3 4.4  2.2 dequeuedThe list is: 3.3 4.4

Page 71: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

3.3 dequeuedThe list is: 4.4  4.4 dequeuedThe list is empty All nodes destroyed All nodes destroyed

Page 72: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

15.7 Trees

• Tree nodes contain two or more links– all other data structures we have discussed only contain one node

• 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

B

A D

C

Page 73: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Binary Trees

The simplest form of tree is a binary tree. A binary tree consists of

a. a node (called the root node) and b. left and right sub-trees.

Both the sub-trees are themselves binary trees. c. If a node has no sons, it’s called leaf

If every nonleaf node in a binary tree has nonempty left and right subtrees, called strictly binary tree.

A strictly binary tree with n leaves always contain 2n-1 nodes. The depth of a binary tree is the maximum level of any leaf in the

tree. The level of root starts from 0.A complete binary tree of depth d is the strictly binary tree whose all

leaves are at level d.

Page 74: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

A

B C

D E F

H I

Binary Tree

A

BC

D E

F G

Strictly Binary Tree

Page 75: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

A

B C

D E FG

A Complete Binary Tree of depth 3

A complete binary tree of depth d contains 2 d+1 –1 nodes

Page 76: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

/

- +

A B E

Representation of an Algebraic

Expression using Binary Tree

E= (A-B) /((C*D)+E)

*

C D

Page 77: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Operations in Binary Tree• Let p be a pointer to a node nd of a binary tree.

• Info(p) – contents of node nd.

• Left(p), right(p), father(p), brother(p) returns pointers to the left son of nd, right son nd, the father of nd, the brother of nd.

• Isleft(p),isright(p) – return true or false if nd is left or right son.

• If any subtree is free, then returns null

• If Tree has no value, then root will contain null value

B

A D

C

Page 78: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Binary Search Trees

• 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

47

25 77

11 43 65 93

68 7 17 31 44

2

Page 79: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

• In an ordered binary tree,

1. the keys of all the nodes in the left sub-tree are less than that of the root,

2. the keys of all the nodes in the right sub-tree are greater than that of the root,

3. the left and right sub-trees are themselves ordered binary trees.

Searching an element in a treeThe number of comparisons can be reduced to half.

Btree.cpp

Page 80: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Tree Traversal

• Tree traversals: the process of visiting each node

in the tree exactly once.– inorder traversal of a binary search tree prints the

node values in ascending order1. Traverse the left subtree with an inorder traversal.2. Process the value in the node 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.

Page 81: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Example A

B

FD E

C

Preorder

ABDECF

Inorder

DBEACF

Postorder

DEBFCA

A

B

D E

F

C

GH

J K

L

Page 82: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Class definition

1.1 Member functions

1.2 Member variables

-----------------------

1. Load header

1 // Fig. 15.16: treenode.h2 // Definition of class TreeNode3 #ifndef TREENODE_H4 #define TREENODE_H56 template< class NODETYPE > class Tree; // forward declaration78 template< class NODETYPE >9 class TreeNode {10 friend class Tree< NODETYPE >;11 public:12 TreeNode( const NODETYPE &d ) 13 : leftPtr( 0 ), data( d ), rightPtr( 0 ) { }14 NODETYPE getData() const { return data; }15 private:16 TreeNode< NODETYPE > *leftPtr; // pointer to left subtree17 NODETYPE data;18 TreeNode< NODETYPE > *rightPtr; // pointer to right subtree19 };2021 #endif22 // 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

33 template< class NODETYPE >

Trees contain two pointers per node.

Page 83: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.2 Class definition

1.3 Function prototypes

1.4 Function definitions

34 class Tree {

35 public:

36 Tree();

37 void insertNode( const NODETYPE & );

38 void preOrderTraversal() const;

39 void inOrderTraversal() const;

40 void postOrderTraversal() const;

41 private:

42 TreeNode< NODETYPE > *rootPtr;

43

44 // utility functions

45 void insertNodeHelper(

46 TreeNode< NODETYPE > **, const NODETYPE & );

47 void preOrderHelper( TreeNode< NODETYPE > * ) const;

48 void inOrderHelper( TreeNode< NODETYPE > * ) const;

49 void postOrderHelper( TreeNode< NODETYPE > * ) const;

50 };

51

52 template< class NODETYPE >

53 Tree< NODETYPE >::Tree() { rootPtr = 0; }

54

55 template< class NODETYPE >

56 void Tree< NODETYPE >::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.

61 template< class NODETYPE >

62 void Tree< NODETYPE >::insertNodeHelper(

63 TreeNode< NODETYPE > **ptr, const NODETYPE &value )

64 {

Page 84: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.4 Function definitions

65 if ( *ptr == 0 ) { // tree is empty66 *ptr = new TreeNode< NODETYPE >( value );67 assert( *ptr != 0 );68 }69 else // tree is not empty70 if ( value < ( *ptr )->data )71 insertNodeHelper( &( ( *ptr )->leftPtr ), value );72 else73 if ( value > ( *ptr )->data )74 insertNodeHelper( &( ( *ptr )->rightPtr ), value );75 else76 cout << value << " dup" << endl;77 }7879 template< class NODETYPE > 80 void Tree< NODETYPE >::preOrderTraversal() const81 { preOrderHelper( rootPtr ); }8283 template< class NODETYPE >84 void Tree< NODETYPE >::preOrderHelper( 85 TreeNode< NODETYPE > *ptr ) const86 {87 if ( ptr != 0 ) {88 cout << ptr->data << ' ';89 preOrderHelper( ptr->leftPtr );90 preOrderHelper( ptr->rightPtr );91 }92 }9394 template< class NODETYPE >95 void Tree< NODETYPE >::inOrderTraversal() const96 { inOrderHelper( rootPtr ); }97

Traversals are recursively defined

Page 85: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1.4 Function definitions

98 template< class NODETYPE >

99 void Tree< NODETYPE >::inOrderHelper(

100 TreeNode< NODETYPE > *ptr ) const

101{

102 if ( ptr != 0 ) {

103 inOrderHelper( ptr->leftPtr );

104 cout << ptr->data << ' ';

105 inOrderHelper( ptr->rightPtr );

106 }

107}

108

109template< class NODETYPE >

110void Tree< NODETYPE >::postOrderTraversal() const

111 { postOrderHelper( rootPtr ); }

112

113template< class NODETYPE >

114void Tree< NODETYPE >::postOrderHelper(

115 TreeNode< NODETYPE > *ptr ) const

116{

117 if ( ptr != 0 ) {

118 postOrderHelper( ptr->leftPtr );

119 postOrderHelper( ptr->rightPtr );

120 cout << ptr->data << ' ';

121 }

122}

123

124#endif

Page 86: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

1. Load header

1.1 Initialize object

2. Input values

3. Output

125// Fig. 15.16: fig15_16.cpp126// Driver to test class Tree127#include <iostream>128#include <iomanip>129#include "tree.h"130131using std::cout;132using std::cin;133using std::setiosflags;134using std::ios;135using std::setprecision;136137int main()138{139 Tree< int > intTree;140 int intVal, i;141142 cout << "Enter 10 integer values:\n";143 for ( i = 0; i < 10; i++ ) {144 cin >> intVal;145 intTree.insertNode( intVal );146 }147148 cout << "\nPreorder traversal\n";149 intTree.preOrderTraversal();150151 cout << "\nInorder traversal\n";152 intTree.inOrderTraversal();153154 cout << "\nPostorder traversal\n";155 intTree.postOrderTraversal();156

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

Page 87: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline157 Tree< double > doubleTree;

158 double doubleVal;

159

160 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 }

167

168 cout << "\nPreorder traversal\n";

169 doubleTree.preOrderTraversal();

170

171 cout << "\nInorder traversal\n";

172 doubleTree.inOrderTraversal();

173

174 cout << "\nPostorder traversal\n";

175 doubleTree.postOrderTraversal();

176

177 return 0;

178}

create a double tree

2. Input values

3. OutputEnter 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

Page 88: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Outline

Program Output

Enter 10 integer values:50 25 75 12 33 67 88 6 13 68 Preorder traversal50 25 12 6 13 33 75 67 68 88Inorder traversal6 12 13 25 33 50 67 68 75 88Postorder traversal6 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 traversal39.2 16.5 3.3 1.1 4.4 82.7 65.2 90.8 89.5 92.5Inorder traversal1.1 3.3 4.4 16.5 39.2 65.2 82.7 89.5 90.8 92.5Postorder traversal1.1 4.4 3.3 16.5 65.2 89.5 92.5 90.8 82.7 39.2

Page 89: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

HEAP• Suppose T is a complete Binary Tree. The largest element

in T appears in the root.

• A particular kind of Binary tree, has two properties– The value of each node is not less than the values stored in each of its

children

– The tree is perfectly balanced, and the leaves in the last level are all in the left most position.

10

8

2 3

7

6

10

2

8 3

7

6

10

8

23

6

7

Heap nonheap nonheap

Page 90: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Building a Heap• 44 30 50 22 60 55 77 55

44

30

50

30 44

50

30 44

22

60

50

22 30

Page 91: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Inserting an element in a Heap• Suppose an item is inserted in a heap tree

– First adjoin item at the end of tree so that Tree is still a complete tree, not necessarily a heap

– Adjust the item to appropriate place to become a heap

8

2 3 6

7

Add 9 to the Tree 10

5

9

The path is

10 8 2 9

Compare 9 & 2

Interchange 9 & 2

Compare 9 and 8

Interchange

Page 92: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Deleting root from Heap• Assign root R to some variable• Replace the deleted node R by the last node of H

so that H is still a complete tree, but not necessarily a heap

• Re heap the elements

Heap sort

Build a heap out of elements of A

Repeatedly remove the root elements of H

Page 93: Data Structures Slides

2000 Deitel & Associates, Inc. All rights reserved. 2000 Deitel & Associates, Inc. All rights reserved.

Heaps as Priority Queue• A heap is an excellent way to implement a priority

Queue.• The highest priority item is at the root and is

trivially extracted. • Insertion and deletion take O(log n) operations.