二叉树的遍历——链表描述
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
template<class T>
struct binaryTreeNode
{
T element;
binaryTreeNode<T> *leftNode;
binaryTreeNode<T> *rightNode;
binaryTreeNode()
{
leftNode = rightNode = nullptr;
}
binaryTreeNode(const T &theElement)
{
element(theElement);
leftNode = rightNode = nullptr;
}
binaryTreeNode(const T &theElement, binaryTreeNode<T> *theLeftNode, binaryTreeNode<T> *theRightNode)
{
element(element);
leftNode = theLeftNode;
rightNode = theRightNode;
}
};
template<class T>
void visit(binaryTreeNode<T> *treeNode)
{
cout << treeNode->element << endl;
}
template<class T>
void preOrder(binaryTreeNode<T> *tree)//先序遍历
{
if (tree == nullptr)
{
return;
}
visit(tree);
preOrder(tree->leftNode);
preOrder(tree->rightNode);
}
template<class T>
void inOrder(binaryTreeNode<T> *tree)//中序遍历
{
if (tree == nullptr)
{
return;
}
preOrder(tree->leftNode);
visit(tree);
preOrder(tree->rightNode);
}
template<class T>
void postOrder(binaryTreeNode<T> *tree)//后序遍历
{
if (tree == nullptr)
{
return;
}
preOrder(tree->leftNode);
preOrder(tree->rightNode);
visit(tree);
}
int main(void)
{
cout << "JZQ!" << endl;
system("pause");
return 0;
//自动排版:ctrl+K,ctrl+F
}