Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
PRESENTATION
NULL
INSERTION CASES
• INSERTION AT THE BEGINNING OF THE LIST
void insert_begin(int i)
{
node *ptr=new node;
ptr->info=i;
ptr->prev=NULL;
if(start==NULL)
{
start=ptr;
start->next=NULL;
}
else
{
ptr->next=start;
start->prev=ptr;
start=ptr;
}
}
• I N S E RT I O N AT T H E E N D O F T H E L I S T
void insert_end(int i)
{
node *ptr=new node;
ptr->info=i;
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
start->prev=NULL;
}
else
{
node *temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
ptr->prev=temp;
}
}
• INSERTION AT A SPECIFIED POSITION
void insert_middle(int i)
{
node *ptr=new node;
ptr->info=i;
int loc,k=0;
cout<<"enter element after which you wish to insert"<<endl;
cin>>loc;
node *temp=start;
while(temp!=NULL)
{
if(temp->info==loc)
{
temp->next->prev=ptr;
ptr->next=temp->next;
ptr->prev=temp;
temp->next=ptr;
k=1;
break;
}
else
temp=temp->next;
}
if(k==0)
cout<<"incorrect location";
DELETION CASES
• DELETION AT THE BEGINNING OF THE LIST
void delete_begin()
{
if(start==NULL)
cout<<"deletion not possible"<<endl;
else
{
start=start->next;
start->prev=NULL;
}
}
• D E L E T I O N AT T H E E N D O F T H E L I S T
void delete_end()
{
if(start==NULL)
cout<<"deletion not possible"<<endl;
else
{
node *temp=start;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
}
• DELETION FROM A SPECIFIED POSITION
void delete_middle()
{
int l,k=0;
cout<<"enter location after which you want to delete the element"<<endl;
cin>>l;
node *temp=start;
while(temp->next!=NULL)
{
if(temp->info==l)
{
k=1;
node *p=temp->next->next;
temp->next=p;
p->prev=temp;
break;
}
else
temp=temp->next;
}
if(k==0)
cout<<"incorrect location"<<endl;
}
ALGORITHM FOR CONVERSION OF INFIX TO
POSTFIX EXPRESSION USING STACK
Let Q be any infix expression and we have to convert it to postfix expression P.
For this the following procedure will be followed.
1. Push left parenthesis onto STACK and add right parenthesis at the end of Q.
2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until
the STACK is empty.
3. If an operand is encountered add it to P.
4. If a left parenthesis is encountered push it onto the STACK.
5. If an operator is encountered, then
• Repeatedly pop from STACK and add to P each operator which has same
precedence as or higher precedence than the operator encountered.
• Push the encountered operator onto the STACK.
6. If a right parenthesis is encountered, then
• Repeatedly pop from the STACK and add to P each operator
until a left parenthesis is encountered.
• Remove the left parenthesis and do not add it to P.
7. Exit
EXAMPLE OF INFIX TO POSTFIX CONVERSION :
Infix Expression: A+((B+C)+(D+E)*F)/G
Scanned Symbols Stack Expression P
(
A ( A
+ (+ A
( (+( A
( (+(( A
B (+(( AB
+ (+((+ AB
C (+((+ ABC
) (+( ABC+
+ (+(+ ABC+
( (+(+( ABC+
D (+(+( ABC+D
+ (+(+(+ ABC+D
E (+(+(+ ABC+DE
) (+(+ ABC+DE+
* (+(+* ABC+DE+
F (+(+* ABC+DE+F
) (+ ABC+DE+F*+
/ (+/ ABC+DE+F*+
G (+/ ABC+DE+F*+G
) ABC+DE+F*+G/+
Expression P : A B C + D E + F * + G / +
ALGORITHMS FOR PREORDER AND
POSTORDER TRAVERSAL
• PREORDER TRAVERSAL :
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.
Algorithm
void preorder( struct BinaryTreeNode *root )
{
if ( root! = NULL )
{
cout << root -> num;
preorder( root -> left );
preorder( root -> right );
}
}
EXAMPLE OF PREORDER TRAVERSAL :