由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 分享:non-recursive breadth first search and depth first search algorithm in C
相关主题
一道面试题:Flatten a multilevel linked list请教一个二叉树镜像问题
Print a binary tree in level order but starting from leaf node up to root请教一道面试题
回馈本版,新鲜店面,新题新气象刚刚电面bloomberg,被问到一个没看到过的问题
BFS traverse O(1) space?DFS用stack不用递归的话怎么color node?
一道google面经难题白痴问题:TreeNode 里面有指向 parent 的指针么?
今天的一道电面题,有点意思那个skiplist的题谁来给谢谢
谁能写个trie的框架?palantir 面经
一道面试题合并两个排序好的链表, 优解?
相关话题的讨论汇总
话题: node话题: struct话题: size话题: search话题: printf
进入JobHunting版参与讨论
1 (共1页)
f*****y
发帖数: 444
1
I am preparing algorithm, wrote the following algorithm. Share with you. Not
sure whether it is a duplicate post or not.
//---- Breadth Fast Search algorithm ---
// circular queue
int head = 0;
int tail = 0;
#define Q_EMPTY (head==tail)
#define Q_FULL ((tail-head+1)%SIZE==0)
#define SIZE 10
struct Node* q[SIZE];
void put(struct Node* x)
{
if (x==NULL) return;
if (Q_FULL) return; //queue full
q[tail] = x;
tail = (tail+1) % SIZE;
return;
}
struct Node* get()
{
if (Q_EMPTY) return NULL;
head %= SIZE;
return q[head++];
}
// breadth first search
void bfs(struct Node* root)
{
struct Node* n;
printf("breadth first search of tree:\t");

// get the ball rolling..
put(root);

while ( n=get()) {
// visit each node in queue
printf("%d ",n->i);

// push children to queue
put(n->left);
put(n->right);
}
printf("\n");
}
// push down stack
int top=0;
void push(struct Node* x)
{
if (x==NULL) return;
if (top }
struct Node* pop()
{
if (top>0) return q[--top];
else return NULL;
}
// --- depth first search ---
void dfs(struct Node* root)
{
struct Node* n;
printf("depth first search of tree:\t");

// get the ball rolling..
push(root);

while ( n=pop()) {
// visit each node in stack
printf("%d ",n->i);
// push children to stack
push(n->right);
push(n->left);
}
printf("\n");
}
y*******g
发帖数: 6599
2
bsf和dfs不是一般用来search graph嘛?

Not

【在 f*****y 的大作中提到】
: I am preparing algorithm, wrote the following algorithm. Share with you. Not
: sure whether it is a duplicate post or not.
: //---- Breadth Fast Search algorithm ---
: // circular queue
: int head = 0;
: int tail = 0;
: #define Q_EMPTY (head==tail)
: #define Q_FULL ((tail-head+1)%SIZE==0)
: #define SIZE 10
: struct Node* q[SIZE];

c******t
发帖数: 1500
3
BFS在CLRS上的解法就不是recursion呀
DFS是的,因为用到了stack
i**********e
发帖数: 1145
4
DFS visits the leaf first before visiting its parent.
一些常见面试题的答案与总结 -
http://www.ihas1337code.com

Not

【在 f*****y 的大作中提到】
: I am preparing algorithm, wrote the following algorithm. Share with you. Not
: sure whether it is a duplicate post or not.
: //---- Breadth Fast Search algorithm ---
: // circular queue
: int head = 0;
: int tail = 0;
: #define Q_EMPTY (head==tail)
: #define Q_FULL ((tail-head+1)%SIZE==0)
: #define SIZE 10
: struct Node* q[SIZE];

f*****y
发帖数: 444
5
binary tree is graph, right? I was trying to demo the concept..

【在 y*******g 的大作中提到】
: bsf和dfs不是一般用来search graph嘛?
:
: Not

f*****y
发帖数: 444
6
thanks for pointing this out.

【在 i**********e 的大作中提到】
: DFS visits the leaf first before visiting its parent.
: 一些常见面试题的答案与总结 -
: http://www.ihas1337code.com
:
: Not

y*******g
发帖数: 6599
7
input表达完全不一样

【在 f*****y 的大作中提到】
: binary tree is graph, right? I was trying to demo the concept..
1 (共1页)
进入JobHunting版参与讨论
相关主题
合并两个排序好的链表, 优解?一道google面经难题
leetcode上的Sort List那道题今天的一道电面题,有点意思
G家intern电面新鲜面经谁能写个trie的框架?
感觉今天结结实实被烙印阴了一道面试题
一道面试题:Flatten a multilevel linked list请教一个二叉树镜像问题
Print a binary tree in level order but starting from leaf node up to root请教一道面试题
回馈本版,新鲜店面,新题新气象刚刚电面bloomberg,被问到一个没看到过的问题
BFS traverse O(1) space?DFS用stack不用递归的话怎么color node?
相关话题的讨论汇总
话题: node话题: struct话题: size话题: search话题: printf