1.递归法进行先根中根后根遍历
(1)先根遍历
void PreOrder(node *p)
{
if(p!=NULL)
{
cout<<p->data;
PreOrder(p->left);
PreOrder(p->right);
}
}
(2)中根遍历
void InOrder(node *p)
{
if(p!=NULL)
{
InOrder(p->left);
cout<<p->data;
InOrder(p->right);
}
}
(3)后根遍历
void PostOrder(node *p)
{
if(p!=NULL)
{
PostOrder(p->left);
PostOrder(p->right);
cout<<p->data;
}
}
2.非递归算法遍历,利用栈
(1)先根遍历:
void PreOrder(node *p,stack s)
{
makeNull(s);
while(p!=NULL)
{
push(p,s);
node *temp=top(s);
cout<<temp->data;
pop(s);
if(temp->rihgt)
{
push(temp->right);
}
if(temp->left)
{
push(temp->left);
}
}
}
(2)中根遍历:
void InOrder(node *p)
{
Stack S;
node *h;
h=p;
while(h!=NULL||S!=NULL)
{
if(h!=NULL)
{
push(h,s);
h=h->left;//找到最左的儿子,即最先输出的结点
}
else
{
h=Top(s);
Pop(s);//左儿子空时输出该结点
cout<<h->data;
h=h->right;//找该结点的右儿子
}
}
}
(3)后根遍历(比较麻烦,
需要在节点的结构体中再加入一个参数isVisited,表示某个节点是否被访问过。思路如下:首先根节点入栈,然后判断左孩子是否为空,若不为空,则左孩子入栈,并移动node到左孩子,直到左孩子为空。然后判断栈顶元素,如果栈顶元素没有右孩子或者右孩子已经被访问过,那么就访问该栈顶元素,并出栈;如果栈顶元素有右孩子并且右孩子没有被访问过,那么把右孩子入栈,把node指针指向右孩子。)
void PostOrder(node *p,stack s)
{
makenull(s);
push(p,s);
while(s!=NULL)
{
while(p->left!=NULL)
{
push(p,s);
p=p->left;
}
node *temp=top(s);
if(temp->right==NULL||temp->right->isVisited==true)
{
cout<<temp->data;
temp->isVisited=true;
pop(s);
}
else if(temp->right!=NULL&&tetmp->right->isVisited==false)
{
push(temp->right,s);
temp->temp->right;
}
}
}
3.层序遍历(需要用到队列)
非递归法:
void printTree(BinaryTree* arr[])
{
queue<BinaryTree*> rel; //定义一个队列,数据类型是二叉树指针,不要仅是int!!不然无法遍历
rel.push(arr[0]);
while (!rel.empty())
{
BinaryTree* front = rel.front();
printf("%d\n", front->data);
rel.pop(); //删除最前面的节点
if (front->left != nullptr) //判断最前面的左节点是否为空,不是则放入队列
rel.push(front->left);
if (front->right != nullptr)//判断最前面的右节点是否为空,不是则放入队列
rel.push(front->right);
}
}
递归法: void LevelOrder(queue<Node *> &nodeQueue) //已在主函数中将链表的第一个结点入列了
{
- if (nodeQueue.empty())
- {
- return;
- }
-
- Node *frontNode = nodeQueue.front();
- cout << frontNode->element << " ";
- nodeQueue.pop();
- if (frontNode->lChild != NULL)
- {
- nodeQueue.push(frontNode->lChild);
- }
- if (frontNode->rChild != NULL)
- {
- nodeQueue.push(frontNode->rChild);
- }
-
- LevelOrder(nodeQueue);
- }