#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;
}
利用队列实现二叉树的层次遍历
最新推荐文章于 2023-07-29 15:59:15 发布