二叉树的广度搜索非递归 深度搜索递归和非递归
代码
// testdfs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
struct node {
int value;
struct node * left;
struct node * right;
};
struct node root;
struct stack{
int depth;
struct node * s[1024];
};
struct stack st;
struct queue
{
struct node * qu[1024];
int front;
int rear;
};
struct queue q;
struct node * insert(struct node * parent, int value, bool left )
{
struct node* child = (struct node *)malloc(sizeof(struct node));
if (child == NULL) return NULL;
child->value = value;
child->left = NULL;
child->right = NULL;
if (left)
{
parent->left = child;
}
else
{
parent->right = child;
}
return child;
}
void init_q()
{
q.front = -1;
q.rear = 0;
}
void enqueue( struct node * p )
{
q.qu[q.rear] = p;
q.rear++;
}
struct node * dequeue()
{
return q.qu[++q.front];
}
bool empty_q()
{
return ((q.front+1) != q.rear);
}
void push(struct node * p )
{
if( st.depth <1023 )
st.s[++st.depth] =p;
}
struct node * pop()
{
if( st.depth >= 0 )
return st.s[st.depth--];
return NULL;
}
bool empty()
{
return (st.depth >=0 ? 1:0);
}
void init()
{
bool left = true;
bool right = false;
struct node * p2 = insert(&root, 2, left );
struct node * p3 = insert(&root, 3, right);
struct node * p4 = insert(p2, 4, left);
struct node * p5 = insert(p2, 5, right);
struct node * p6 = insert(p3, 6, left);
struct node * p7 = insert(p3, 7, right);
}
void visit(struct node * p)
{
printf("%d ", p->value);
}
void ddfs(struct node * root)
{
visit( root );
if( root->left )
ddfs( root->left);
if ( root->right)
ddfs( root->right );
}
/*二叉树的深度搜索非递归 使用栈模拟*/
void dfs(struct node * root)
{
struct node * p = root;
while ( p || empty() )
{
if( p )
{
visit(p);
push( p );
}
if( p && p->left )
{
p = p->left;
}
else
{
p = pop();
p = p->right;
}
}
}
/*队列模拟 二叉树广度搜索*/
void bfs(struct node * root)
{
struct node * p = root;
enqueue(p);
while ( empty_q() )
{
p = dequeue();
visit(p);
if(p->left)
{
enqueue(p->left);
}
if(p->right)
{
enqueue(p->right);
}
}
}
int main(int argc, char* argv[])
{
root.value = 1;
root.left = NULL;
root.right = NULL;
st.depth = -1;
init();
init_q();
printf("ddfs\r\n");
ddfs( &root);
printf("\r\ndfs\r\n");
dfs( &root );
printf("\r\nbfs\r\n");
bfs( &root );
return 0;
}
//
#include "stdafx.h"
#include <stdlib.h>
struct node {
int value;
struct node * left;
struct node * right;
};
struct node root;
struct stack{
int depth;
struct node * s[1024];
};
struct stack st;
struct queue
{
struct node * qu[1024];
int front;
int rear;
};
struct queue q;
struct node * insert(struct node * parent, int value, bool left )
{
struct node* child = (struct node *)malloc(sizeof(struct node));
if (child == NULL) return NULL;
child->value = value;
child->left = NULL;
child->right = NULL;
if (left)
{
parent->left = child;
}
else
{
parent->right = child;
}
return child;
}
void init_q()
{
q.front = -1;
q.rear = 0;
}
void enqueue( struct node * p )
{
q.qu[q.rear] = p;
q.rear++;
}
struct node * dequeue()
{
return q.qu[++q.front];
}
bool empty_q()
{
return ((q.front+1) != q.rear);
}
void push(struct node * p )
{
if( st.depth <1023 )
st.s[++st.depth] =p;
}
struct node * pop()
{
if( st.depth >= 0 )
return st.s[st.depth--];
return NULL;
}
bool empty()
{
return (st.depth >=0 ? 1:0);
}
void init()
{
bool left = true;
bool right = false;
struct node * p2 = insert(&root, 2, left );
struct node * p3 = insert(&root, 3, right);
struct node * p4 = insert(p2, 4, left);
struct node * p5 = insert(p2, 5, right);
struct node * p6 = insert(p3, 6, left);
struct node * p7 = insert(p3, 7, right);
}
void visit(struct node * p)
{
printf("%d ", p->value);
}
void ddfs(struct node * root)
{
visit( root );
if( root->left )
ddfs( root->left);
if ( root->right)
ddfs( root->right );
}
/*二叉树的深度搜索非递归 使用栈模拟*/
void dfs(struct node * root)
{
struct node * p = root;
while ( p || empty() )
{
if( p )
{
visit(p);
push( p );
}
if( p && p->left )
{
p = p->left;
}
else
{
p = pop();
p = p->right;
}
}
}
/*队列模拟 二叉树广度搜索*/
void bfs(struct node * root)
{
struct node * p = root;
enqueue(p);
while ( empty_q() )
{
p = dequeue();
visit(p);
if(p->left)
{
enqueue(p->left);
}
if(p->right)
{
enqueue(p->right);
}
}
}
int main(int argc, char* argv[])
{
root.value = 1;
root.left = NULL;
root.right = NULL;
st.depth = -1;
init();
init_q();
printf("ddfs\r\n");
ddfs( &root);
printf("\r\ndfs\r\n");
dfs( &root );
printf("\r\nbfs\r\n");
bfs( &root );
return 0;
}
广度搜索: 使用队列模拟
深度搜索: 使用堆栈模拟