利用队列实现二叉树的层次遍历

#include<iostream>
#include<stdlib.h>
using namespace std;

struct TreeNode {
    int data;
    TreeNode *leftChild,*rightChild;
	TreeNode()
	{
		leftChild = NULL;
		rightChild = NULL;
	}
};

struct QueueNode {
	TreeNode *t;
	QueueNode *next;
};

class Queue {
private:
	QueueNode *front,*rear;
public:
	Queue():rear(NULL),front(NULL) {}
	void EnQueue(TreeNode *&t);
	void DeQueue(TreeNode *&x);
	bool isEmpty() const { return (front==NULL)?true:false; }
};

void Queue::EnQueue(TreeNode *&t)
{
	if (front==NULL) {
		front=rear=new QueueNode();
		front->t = t;
		front->next = NULL;
	}
	else {
		rear->next = new QueueNode();
		rear->next->t = t;
		rear->next->next = NULL;
		rear = rear->next;
	}
}

void Queue::DeQueue(TreeNode *&x)
{
	if (isEmpty())
		return ;
	QueueNode *p = front;
	x = front->t;
	front = front->next;
	delete p;
}

class BinaryTree {
protected:
	TreeNode *root;
	void copy(TreeNode *&target,TreeNode *origin);
	void create(TreeNode *&root,const int *a,int i,const int l);
	int getHeight(BinaryTree *bt);
	int getSize(BinaryTree *bt);
	void destroy(TreeNode *root);
public:
	BinaryTree() { root=NULL; }
	BinaryTree(BinaryTree *s);
	~BinaryTree() { destroy(root); }
	bool isEmpty()
	{
		return (root==NULL)?true:false;
	}
	void Create(BinaryTree *&bt,const int *a,int i,const int l);
	TreeNode *getRoot() { return root; }
	void Print(TreeNode *root);
	BinaryTree *leftSubTree();
	BinaryTree *rightSubTree();
	int Height();
	int Size();
	void LevelOrder();
	void visit(TreeNode *root);
};

BinaryTree::BinaryTree(BinaryTree *s)
{
	this->copy(this->root,s->root);
}

void BinaryTree::copy(TreeNode *&target,TreeNode *origin)
{
	if (origin==NULL)
		target=NULL;
	else {
		target = new TreeNode();
		target->data = origin->data;
		copy(target->leftChild,origin->leftChild);
		copy(target->rightChild,origin->rightChild);
	}
}

void BinaryTree::create(TreeNode *&root,const int *a,int i,const int l)
{
	if (i>=l)
		return;
	if (a[i]==0) {
		root = NULL;
		return;
	}
	else {
		root = new TreeNode();
		root->data = a[i];
		create(root->leftChild,a,i*2+1,l);
		create(root->rightChild,a,i*2+2,l);
	}
}

void BinaryTree::Create(BinaryTree *&bt,const int *a,int i,const int l)
{
	this->create(bt->root,a,i,l);
}

BinaryTree* BinaryTree::leftSubTree()
{
	BinaryTree *bt = new BinaryTree();
	bt->root = this->root->leftChild;
	return bt;
}

BinaryTree* BinaryTree::rightSubTree()
{
	BinaryTree *bt = new BinaryTree();
	bt->root = this->root->rightChild;
	return bt;
}

int BinaryTree::Height()
{
	return getHeight(this);
}

int BinaryTree::getHeight(BinaryTree *bt)
{
	if (bt->root==NULL)
		return 0;
	int a = bt->leftSubTree()->getHeight(bt->leftSubTree());
	int b = bt->rightSubTree()->getHeight(bt->rightSubTree());
	return (a>b)?++a:++b;
}

int BinaryTree::Size()
{
	return this->getSize(this);
}

int BinaryTree::getSize(BinaryTree *bt)
{
	if (bt->getRoot()==NULL)
		return 0;
	int a = bt->leftSubTree()->getSize(bt->leftSubTree());
	int b = bt->rightSubTree()->getSize(bt->rightSubTree());
	return 1+a+b;
}

void BinaryTree::Print(TreeNode *root)
{
	if (root!=NULL) {
		cout << root->data << " ";
		Print(root->leftChild);
		Print(root->rightChild);
	}
}

void BinaryTree::destroy(TreeNode *root)
{
	if (root->leftChild!=NULL)
		destroy(root->leftChild);
	if (root->rightChild!=NULL)
		destroy(root->rightChild);
	delete root;
}

void BinaryTree::visit(TreeNode *root)
{
	cout << root->data << " ";
}

//write your code here
void BinaryTree::LevelOrder()
{
    Queue Q;
    TreeNode *p=getRoot();
    Q.EnQueue(p);
    while(!Q.isEmpty())
    {
        Q.DeQueue(p);
        visit(p);
        if(p->leftChild)
        Q.EnQueue(p->leftChild);
        if(p->rightChild)
        Q.EnQueue(p->rightChild);
    }
}

int main()
{
	int length = 10;
	cin >> length;//数组大小
	int *a = new int[length];
	int i;
	for (i=0;i<length;i++)
		cin >> a[i];
	BinaryTree *bt=new BinaryTree();
	bt->Create(bt,a,0,length);
	bt->LevelOrder();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值