二叉树的深度优先遍历有以下三种模式:
1. 先序遍历(VLR) – 此模式为先访问父节点,再访问左节点,最后访问右节点.
2. 中序遍历(LVR) – 此模式为先访问左节点,再访问父节点,最后访问右节点. 按照中序遍历方法访问二叉查找树,得到的结果是从小到大排列有序的.
3. 后序遍历(LRV) – 此模式为先访问左节点,再访问右节点,最后访问父节点.
遍历的思想有两种:
递归
非递归: 非递归方法使用的一个栈做为辅助数据结构. 利用栈的后进先出特点方便实现遍历.
VLR: 先访问,再压栈,没有左子树后出栈.出栈后,转向右子树.
LVR: 先压栈,没有左子树后出栈.出栈后,访问,转向右子树.
LRV: 先压栈,没有左子树后查看栈顶元素.栈顶元素如果没有右子树或者栈顶元素的右子树是刚刚访问过的节点,则出栈.出栈后,访问该节点.之后将该节点设置为刚刚访问过的节点.栈顶元素如果有右子树,并且不是刚刚访问过的节点,则转向右子树.
VLR递归与非递归遍历算法
template <class T, unsigned int capacity>
bool BinSearchTree<T, capacity>::preOrder()
{
bool result = false;
if (isEmpty())
{
result = false;
}
else
{
cout << "PreOrder: The Data is: " << root->data << endl;
preOrder(root->left);
preOrder(root->right);
result = true;
}
return result;
}
template <class T, unsigned int capacity>
void BinSearchTree<T, capacity>::preOrder(BinNode<T> *node)
{
if (NULL == node)
{
}
else
{
cout << "PreOrder: The Data is: " << node->data << endl;
preOrder(node->left);
preOrder(node->right);
}
return;
}
template <class T, unsigned int capacity>
bool BinSearchTree<T, capacity>::preOrderWithoutRecursion()
{
bool result = false;
if (isEmpty())
{
result = false;
}
else
{
stackHelper = new Stack<BinNode<T>*, capacity>();
BinNode<T> *iter = root;
while ((NULL != iter) || !(stackHelper->isEmpty()))
{
while (NULL != iter)
{
cout << "preOrderWithoutRecursion: The data is: " << iter->data << endl;
stackHelper->push(iter);
iter = iter->left;
}
if (!(stackHelper->isEmpty()))
{
stackHelper->pop(iter);
iter = iter->right;
}
}
if (NULL != stackHelper)
{
delete stackHelper;
stackHelper = NULL;
}
result = true;
}
return result;
}
LVR递归与非递归遍历算法
template <class T, unsigned int capacity>
bool BinSearchTree<T, capacity>::midOrder()
{
bool result = false;
if (isEmpty())
{
result = false;
}
else
{
midOrder(root->left);
cout << "MidOrder: The data is: " << root->data << endl;
midOrder(root->right);
}
return result;
}
template <class T, unsigned int capacity>
void BinSearchTree<T, capacity>::midOrder(BinNode<T> *node)
{
if (NULL == node)
{
}
else
{
midOrder(node->left);
cout << "MinOrder: The data is: " << node->data << endl;
midOrder(node->right);
}
return;
}
template <class T, unsigned int capacity>
bool BinSearchTree<T, capacity>::midOrderWithoutRecursion()
{
bool result = false;
if (isEmpty())
{
result = false;
}
else
{
BinNode<T> *iter = root;
stackHelper = new Stack<BinNode<T>*, capacity>();
while ((NULL != iter) || !(stackHelper->isEmpty()))
{
while (NULL != iter)
{
stackHelper->push(iter);
iter = iter->left;
}
if (!(stackHelper->isEmpty()))
{
stackHelper->pop(iter);
cout << "midOrderWithoutRecursion: The data is: " << iter->data << endl;
iter = iter->right;
}
}
if (NULL != stackHelper)
{
delete stackHelper;
stackHelper = NULL;
}
result = true;
}
return result;
}
LRV递归与非递归遍历算法
template <class T, unsigned int capacity>
bool BinSearchTree<T, capacity>::afterOrder()
{
bool result = false;
if (isEmpty())
{
result = false;
}
else
{
afterOrder(root->left);
afterOrder(root->right);
cout << "AfterOrder: The data is: " << root->data <<endl;
result = true;
}
return result;
}
template <class T, unsigned int capacity>
void BinSearchTree<T, capacity>::afterOrder(BinNode<T>* node)
{
if (NULL == node)
{
}
else
{
afterOrder(node->left);
afterOrder(node->right);
cout << "AfterOrder: The data is: " << node->data << endl;
}
return;
}
template <class T, unsigned int capacity>
bool BinSearchTree<T, capacity>::afterOrderWithoutRecursion()
{
bool result = false;
if (isEmpty())
{
result = false;
}
else
{
BinNode<T> *preVisited = NULL;
BinNode<T> *iter = root;
stackHelper = new Stack<BinNode<T>*, capacity>();
while ((NULL != iter) || !(stackHelper->isEmpty()))
{
while (NULL != iter)
{
stackHelper->push(iter);
iter = iter->left;
}
if (!(stackHelper->isEmpty()))
{
stackHelper->topElem(iter);
if ((NULL == iter->right) || (preVisited == iter->right))
{
stackHelper->pop(iter);
cout << "afterOrderWithoutRecursion: The data is: " << iter->data << endl;
preVisited = iter;
iter = NULL;
}
else
{
iter = iter->right;
}
}
}
}
return result;
}