**
二叉树的遍历
**
【问题描述】
给定一棵N个节点的二叉树,输出其前序遍历,中序遍历,后序遍历,层次遍历。
【输入形式】
输入共N+1行。
第1行为一个整数N,描述节点个数。
其余N行按顺序描述第1,2,……,N个结点的左右子节点编号,0表示没有相应子节点。
【输出形式】
输出共4行,分别为前序遍历,中序遍历,后序遍历,层次遍历。
【样例输入】
10
8 0
4 1
0 0
6 9
0 0
3 7
0 0
0 0
5 10
0 0
【样例输出】
2 4 6 3 7 9 5 10 1 8
3 6 7 4 5 9 10 2 8 1
3 7 6 5 10 9 4 8 1 2
2 4 1 6 9 8 3 7 5 10
【数据范围】
保证输入的是合法的二叉树。
1<= N <= 10000.
cpp文件:
#include <iostream>
#include "linkQueue.h"
using namespace std;
template <class T>
class bTree {
public:
virtual void preOrder()const = 0;
virtual void midOrder()const = 0;
virtual void postOrder()const = 0;
virtual void levelOrder()const = 0;
};
template<class T>
class binaryTree :public bTree<T> {
private:
struct Node {
T data;
Node* left, * right, * parent;
Node() :left(NULL), right(NULL), parent(NULL) {}
Node(T item, Node* L = NULL, Node* R = NULL, Node* P = NULL) :data(item), left(L), right(R), parent(P){}
~Node() {}
};
Node* root;
public:
binaryTree() :root(NULL) {}
binaryTree(T x) { root = new Node(x); }
~binaryTree();
void preOrder()const;
void midOrder()const;
void postOrder()const;
void levelOrder()const;
void creatTree(int n, T flag);
private:
void clear(Node*& t);
void preOrder(Node* t)const;
void midOrder(Node* t)const;
void postOrder(Node* t)const;
};
template<class T>
void binaryTree<T>::clear(binaryTree<T>::Node*& t)
{
if (t == NULL) return;
clear(t->left);
clear(t->right);
delete t;
t = NULL;
}
template<class T>
binaryTree<T>::~binaryTree()
{
clear(root);
}
template<class T>
void binaryTree<T>::preOrder(binaryTree<T>::Node* t)const
{
if (t == NULL) return;
cout << t->data << ' ';
preOrder(t->left);
preOrder(t->right);
}
template<class T>
void binaryTree<T>::preOrder()const
{
cout << endl;
preOrder(root);
}
template<class T>
void binaryTree<T>::postOrder(binaryTree<T>::Node* t)const
{
if (t == NULL) return;
postOrder(t->left);
postOrder(t->right);
cout << t->data << ' ';
}
template<class T>
void binaryTree<T>::postOrder()const
{
cout << endl;
postOrder(root);
}
template<class T>
void binaryTree<T>::midOrder(binaryTree<T>::Node* t)const
{
if (t == NULL) return;
midOrder(t->left);
cout << t->data << ' ';
midOrder(t->right);
}
template<class T>
void binaryTree<T>::midOrder()const
{
cout << endl;
midOrder(root);
}
template<class T>
void binaryTree<T>::levelOrder()const
{
linkQueue<Node*>que;
Node* tmp;
que.enQueue(root);
while (!que.isEmpty()) {
tmp = que.deQueue();
cout << tmp->data << ' ';
if (tmp->left) que.enQueue(tmp->left);
if (tmp->right) que.enQueue(tmp->right);
}
}
template<class T>
void binaryTree<T>::creatTree(int n, T flag)
{
Node** p = NULL;
T ldata, rdata;
p = new Node * [n + 1];
for (int i = 1; i <= n; ++i) {
p[i] = new Node(i);
}
for (int i = 1; i <= n; ++i) {
cin >> ldata >> rdata;
if (ldata != flag) {
p[i]->left = p[ldata];
p[i]->left->parent = p[i];
}
if (rdata != flag) {
p[i]->right = p[rdata];
p[i]->right->parent = p[i];
}
}
for (int i = 1; i <= n; ++i) {
if (p[i]->parent == NULL) root = p[i];
}
}
int main()
{
binaryTree<int> tree;
int n;
cin >> n;
tree.creatTree(n, 0);
tree.preOrder();
tree.midOrder();
tree.postOrder();
cout << endl;
tree.levelOrder();
return 0;
}
头文件linkQueue,h
#pragma once
template <class elemType>
class queue
{
public:
virtual bool isEmpty()const = 0;
virtual void enQueue(const elemType& x) = 0;
virtual elemType deQueue() = 0;
virtual elemType getHead() const = 0;
virtual ~queue() {}
};
template <class elemType>
class linkQueue: public queue<elemType>
{
private:
struct node {
elemType data;
node* next;
node(const elemType &x, node *N = NULL)
{
data = x; next = N;
}
node():next(NULL){}
~node(){}
};
node* front, * rear;
public :
linkQueue();
~linkQueue();
bool isEmpty()const;
void enQueue(const elemType& x);
elemType deQueue();
elemType getHead()const;
};
template <class elemType>
linkQueue<elemType>::linkQueue()
{
front = rear = NULL;
}
template <class elemType>
linkQueue<elemType>::~linkQueue()
{
node* tmp;
while (front != NULL) {
tmp = front;
front = front->next;
delete tmp;
}
}
template <class elemType>
bool linkQueue<elemType>::isEmpty()const
{
return front == NULL;
}
template <class elemType>
void linkQueue<elemType>::enQueue(const elemType& x)
{
if (rear == NULL)
front = rear = new node(x);
else
rear = rear->next = new node(x);
}
template <class elemType>
elemType linkQueue<elemType>::deQueue()
{
node* tmp = front;
elemType value = front->data;
front = front->next;
if (front == NULL) rear = NULL;
delete tmp;
return value;
}
template <class elemType>
elemType linkQueue<elemType>::getHead()const
{
return front->data;
}