SJTU二叉树的遍历

**

二叉树的遍历

**
【问题描述】

给定一棵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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值