1. 递归遍历
递归遍历非常简单,,,,,,
1.1 前序遍历
void preOrderTraverse(BSTreeNode *root)
{if (root != NULL)
{printf("%d \n", root->data);
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}
}
1.2 中序遍历
void InOrderTraverse(BSTreeNode *root)
{if (root != NULL)
{InOrderTraverse(root->left);
printf("%d \n", root->data);
InOrderTraverse(root->right);
}
}
1.3 后序遍历
void PostOrderTraverse(BSTreeNode *root)
{if (root != NULL)
{PostOrderTraverse(root->left);
PostOrderTraverse(root->right);
printf("%d \n", root->data);
}
}
2. 非递归遍历
栈和队列的构建 详见其余博文
2.1 前序遍历
void preOrderTraverseNR(BSTreeNode *root)
{
// 建设栈
struct LinkedStack *Stack;
Stack = initStack();
while(NULL != root){printf("%d \n", root->data);
if (NULL != root->right)
{push(Stack, root->right);
}
if (NULL != root->left)
{push(Stack, root->left);
}
root = pop(Stack);
}
free(Stack);
Stack = NULL;
}
下周持续