Traversal in Binary trees...

Friday 6 April 2012
,
Preorder traversal:

preorder(v)
if(v==NULL) then return
else visit(v)//generic computation
preorder(v.leftchild())
preorder(v.rightchild())

postorder(v)
if(v==NULL) then return
else postorder(v.leftchild())
postorder(v.rightchild())
visit(v)//generic computation

Use of post order traversal:
->computing arithmetic expression

Inorder traversal:When the node is visited between the left
and the right subtree(only in binary tree)

inOrder(v):
if(v==NULL)then return
else inOrder(v.leftChild())
visit(v)
inOrder(v.rightChild())

Another way is the Euler Tour traversal:

->generic traversal of the binary tree.
->Each case is a special case of this tree
->Each internal node is visited three times

->Given the preorder and the inorder traversal of a binary tree
we can find out the actual tree recursively.
->Similarly we can find the tree from post order and inorder
traversal mentioned.
->But a postorder and a preorder traversal is insufficient as
there can be two trees having the same preorder and post order
traversal
->however in case of binary tree we can make the tree given the preorder and the post order traversal


Next entries will be:
1. Program to print the tree given:
->postorder and inorder traversal
->preorder and inorder traversal
->postorder and preorder traversal in binary tree
2. To compute the number of possible trees given a particular
order of binary tree

Recursive Solution To The 8 queen Problem

Tuesday 3 April 2012
#include
#include
static int board[8][8];
static int arrpos[8][2];
int count=0;
int flag=0;
int check(int row,int column)
{
int i;
for(i=0;i<8;i++) { if((board[row][i]==1)&&(i!=column)) return 0; if((board[i][column]==1)&&(i!=row)) return 0; } int copyr=row-1,copyc=column+1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr-1; copyc=copyc+1; } copyr=row+1,copyc=column+1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr+1; copyc=copyc+1; } copyr=row-1,copyc=column-1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr-1; copyc=copyc-1; } copyr=row+1,copyc=column-1; while((copyr>=0)&&(copyc>=0)&&(copyr<=7)&&(copyc<=7)) { if((board[copyr][copyc]==1)) return 0; copyr=copyr+1; copyc=copyc-1; } return 1; } void printarray() { count++; printf("solution %d:",count); int i;//printing the answer for(i=0;i<8;i++) { printf("(%d,%d)",arrpos[i][0],arrpos[i][1]); } printf("\n"); //char c; //scanf("%c",&c); } void getpos(int row,int column,int i) { if(i==8) { printarray();//print answer int lr=arrpos[7][0]; int lc=arrpos[7][1]; board[lr][lc]=0; if (board[0][7]==1) { flag++; if(flag==4) exit(1); }//end program getpos(lr,lc+1,i-1);//start next } if((row<0)||(row>7)||(column>7)||(column<0))
{
int m=i-1;
board[row-1][arrpos[m][1]]=0;
getpos(row-1,arrpos[m][1]+1,i-1);//backtrack previous
}
int mm=check(row,column);
if(mm)
{
arrpos[i][0]=row;
arrpos[i][1]=column;
board[row][column]=1;
getpos(row+1,0,i+1);
//get next row
}
else if(column<7)
getpos(row,column+1,i);//get next place
else
{
int m=i-1;
board[row-1][arrpos[m][1]]=0;
getpos(row-1,arrpos[m][1]+1,i-1);//backtrack previous
}
}


int main()
{
printf("Taking the chessboard as 8x8 from (0,0) to (7,7) as co-ordinates\nSolutions are:\n");
getpos(0,0,0);
return 0;
}
Copyright @ 2013 code-craft. Designed by Templateism | MyBloggerLab