void bfs()
{
if(root==NULL)
{
cout<<"\nEMPTY
LIST";
return;
}
node **queue = new node*[count(root)];
int front=0;
int rear=0;
queue[front]=root;
node *pnode;
while(front<=rear)
{
pnode=queue[front++];
cout<<" "<<pnode->data;
if(pnode->left!=NULL)
queue[++rear]=pnode->left;
if(pnode->right!=NULL)
queue[++rear]=pnode->right;
}
}
void dfs()
{
if(root==NULL)
{
cout<<"\nEMPTY
LIST";
return;
}
node **stack = new node*[count(root)];
int top=0;
stack[top]=root;
node *pnode;
while(top>=0)
{
pnode=stack[top--];
cout<<" "<<pnode->data;
if(pnode->left!=NULL)
stack[++top]=pnode->left;
if(pnode->right!=NULL)
stack[++top]=pnode->right;
}
}
No comments:
Post a Comment