我試圖在預先迭代地遍歷一個BST,但是這個函數在打印時陷入了無限循環。有人能告訴我爲什麼會發生這種情況嗎?無限循環在BST迭代序列遍歷中
1 typedef struct node_{
2 int data;
3 struct node_* left;
4 struct node_* right;
5 }BST;
22 void preorder(BST* tree){
23 if(tree==NULL){
24 return;
25 }
26 // create stack
27 stack* stack=create_stack();
28 // push data onto stack
29 push(stack,tree->data);
30 while(isEmpty(stack)==0){
31 struct node_ *node;
32 node->data=top(stack);
33 printf("%d ",node->data);
34 // pop value off stack
35 pop(stack);
36 if(node->right!=NULL){
37 //push right child onto stack
38 push(stack,node->right->data);
39 }
40 if(node->left!=NULL){
41 // push left child onto stack
42 push(stack,node->left->data);
43 }
44 }
45 }
我對這個問題回答滿意嗎? – 2015-04-24 06:50:00