您的位置:首页 > 其它

二叉树(上机作业版)

2010-11-19 20:56 99 查看
#ifndef BINARYTREENODE

#define BINARYTREENODE

template <typename T >

class BinaryTreeNode

{

private:

int counter; //计数

T element;

BinaryTreeNode < T > *left;

BinaryTreeNode < T > *right;

public:

BinaryTreeNode ();

BinaryTreeNode (const T& ele);

BinaryTreeNode (const T& ele,BinaryTreeNode < T > *l,BinaryTreeNode < T > *r);

T GetValue() const; //返回当前结点的数据

int GetCounter() const;

BinaryTreeNode < T > * GetLeftChild()const;

BinaryTreeNode < T > * GetRightChild() const;

void SetLeftChild(BinaryTreeNode < T > *);

void SetRightChild(BinaryTreeNode < T > *);

void SetValue(const T & val); //设置当前结点的数据

void SetCounter();

bool IsLeaf() const;

BinaryTreeNode < T > & operator =(const BinaryTreeNode < T > & Node);

};

template <typename T >

class BinaryTree

{

private:

BinaryTreeNode < T > *root;

public:

BinaryTree( BinaryTreeNode < T > * node = NULL)

{

root=node;

}

~BinaryTree()

{

cout<<"依次被删除的BinaryTreeNode 结点"<<endl;

DeleteBinaryTree(root);

}

bool IsEmpty() const

{

return (root?(false,true));

}

T Visit(BinaryTreeNode < T > *current); //访问元素值

BinaryTreeNode < T > *Serach(T v);

void Insert(T v); //插入元素

BinaryTreeNode < T > *GetRoot();

void CreatTree(const T & ele,BinaryTree< T > &leftTree,BinaryTree < T > &rightTree);

void PreOrder(BinaryTreeNode < T > *root); //前序周游

void PreOrder1(BinaryTreeNode < T > *root); //前序周游(递归)

void InOrder(BinaryTreeNode < T > *root); //中序周游

void InOrder1(BinaryTreeNode < T > *root); //中序周游(递归)

void PostOrder(BinaryTreeNode < T > *root); //后序周游

void PostOrder1(BinaryTreeNode < T > *root);//后序周游(递归)

void LeveOrder(BinaryTreeNode < T > *root); //按层次周游

void DeleteBinaryTree(BinaryTreeNode < T > *root);

};

#endif

#include "BinaryTree.h"

#include <iostream>

#include <stack>

#include <queue>

#include <string>

#include <fstream>

using namespace std;

/*------------------------------------------------------BinaryTreeNode----------------------------------------------------*/

template <typename T >

BinaryTreeNode < T > ::BinaryTreeNode ()

{

counter=1;

left=right=NULL;

}

template <typename T >

BinaryTreeNode < T > ::BinaryTreeNode (const T& ele)

{

element=ele;

counter=1;

left=right=NULL;

}

template <typename T >

BinaryTreeNode < T > ::BinaryTreeNode(const T& ele, BinaryTreeNode<T> *l, BinaryTreeNode<T> *r)

{

element=ele;

counter=1;

left=l;

right=r;

}

template <typename T >

T BinaryTreeNode < T > ::GetValue() const

{

return this->element;

}

template <typename T >

int BinaryTreeNode < T > ::GetCounter() const

{

return this->counter;

}

template <typename T >

BinaryTreeNode < T > * BinaryTreeNode < T > ::GetLeftChild()const

{

return this->left;

}

template <typename T >

BinaryTreeNode < T > * BinaryTreeNode < T > ::GetRightChild() const

{

return this->right;

}

template <typename T >

void BinaryTreeNode < T >::SetLeftChild(BinaryTreeNode < T > * l)

{

this->left=l;

}

template <typename T >

void BinaryTreeNode < T > ::SetRightChild(BinaryTreeNode < T > * r)

{

this->right=r;

}

template <typename T >

void BinaryTreeNode < T > ::SetValue(const T & val)

{

this->element=val;

}

template <typename T >

void BinaryTreeNode < T >::SetCounter()

{

this->counter++;

}

template <typename T >

bool BinaryTreeNode < T > :: IsLeaf() const

{

if (this->left ==NULL && this ->right)

return true;

else

return false;

}

template <typename T >

BinaryTreeNode < T > & BinaryTreeNode < T >::operator =(const BinaryTreeNode < T > & Node)

{

this->value=node.value;

this->left=node.left;

this->right=node.right;

return *this;

}

/*----------------------------------------------------------------BinaryTree--------------------------------------------------------------------*/

template <typename T >

void BinaryTree < T > :: CreatTree(const T & ele,BinaryTree< T > &leftTree,BinaryTree < T > &rightTree)

{

root=new BinaryTreeNode < T > (v);

root->left=leftTree.root;

root->right=rightTree.root;

leftTree.root=rightTree.root=NULL;

}

template <typename T >

BinaryTreeNode < T > * BinaryTree < T > ::Serach(T v)

{

queue <BinaryTreeNode < T > *> q;

BinaryTreeNode < T > *curNode=root;

if (curNode)

q.push(curNode);

while (!q.empty())

{

curNode=q.front();

q.pop();

if (curNode->GetValue()==v)

return curNode;

if (curNode->GetLeftChild())

q.push(curNode->GetLeftChild());

if (curNode->GetRightChild())

q.push(curNode->GetRightChild());

}

return NULL;

}

template <typename T >

void BinaryTree < T > ::Insert(T v)

{

queue <BinaryTreeNode < T >* > q;

BinaryTreeNode < T > *newPointer,*p;

if(root==NULL)

{

root=new BinaryTreeNode < T >(v);

return ;

}

BinaryTreeNode < T > *pointer=root;

if (pointer!=NULL)

{

q.push(pointer);

}

while (!q.empty())

{

pointer=q.front();

q.pop();

p=this->Serach(v);

if (p!=NULL)

{

p->SetCounter();

return ;

}

if (pointer->GetLeftChild()!=NULL)

q.push(pointer->GetLeftChild());

else

{

newPointer=new BinaryTreeNode < T > (v);

pointer->SetLeftChild(newPointer);

return ;

}

if (pointer->GetRightChild()!=NULL)

q.push(pointer->GetRightChild());

else

{

newPointer=new BinaryTreeNode < T > (v);

pointer->SetRightChild(newPointer);

return ;

}

}

}

template <typename T >

BinaryTreeNode < T > * BinaryTree < T > ::GetRoot()

{

return root;

}

template <typename T >

T BinaryTree < T > ::Visit(BinaryTreeNode<T> *cur)

{

if (cur)

{

return cur->GetValue();

}

}

template <typename T >

void BinaryTree < T > ::PreOrder1(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

if (root!=NULL)

{

fout<<root->GetCounter()<<" ";

fout<<this->Visit(root)<<endl;

PreOrder1(root->GetLeftChild());

PreOrder1(root->GetRightChild());

}

}

template <typename T >

void BinaryTree < T > ::PreOrder(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

stack < BinaryTreeNode < T > *> s;

BinaryTreeNode < T > *curNode;

curNode=root;

if (curNode)

{

s.push(curNode);

}

while (!s.empty())

{

curNode =s.top();

fout<<curNode->GetCounter()<<" ";

fout<<this->Visit(curNode)<<endl;

s.pop();

if (curNode->GetRightChild())

s.push(curNode->GetRightChild());

if (curNode->GetLeftChild())

s.push(curNode->GetLeftChild());

}

}

template <typename T >

void BinaryTree < T > ::InOrder(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

stack < BinaryTreeNode < T >* > s;

BinaryTreeNode < T > *curNode;

if (root==NULL)

return ;

curNode=root;

while (curNode || !s.empty())

{

if (curNode )

{

s.push(curNode);

curNode=curNode->GetLeftChild();

}

else

{

curNode=s.top();

s.pop();

fout<<curNode->GetCounter()<<" ";

fout<<this->Visit(curNode)<<endl;

curNode=curNode->GetRightChild();

}

}

}

template <typename T >

void BinaryTree < T >::InOrder1(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

if (root!=NULL)

{

InOrder1(root->GetLeftChild());

fout<<root->GetCounter()<<" ";

fout<<this->Visit(root)<<endl;

InOrder1(root->GetRightChild());

}

}

enum Tags {left,right};

template <typename T >

struct NewBinaryTreeNode

{

BinaryTreeNode < T > *curNode;

Tags tag;

};

template <typename T >

void BinaryTree < T > ::PostOrder(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

stack < NewBinaryTreeNode < T > > s;

BinaryTreeNode < T > *curNode=root;

NewBinaryTreeNode < T > curNewNode;

while (!s.empty() || curNode )

{

while (curNode!=NULL)

{

curNewNode.curNode=curNode;

curNewNode.tag=(enum Tags) 0;

s.push(curNewNode);

curNode=curNode->GetLeftChild();

}

curNewNode=s.top();

s.pop();

curNode=curNewNode.curNode;

if (curNewNode.tag==(enum Tags) 0)

{

curNewNode.tag=(enum Tags) 1;

s.push(curNewNode);

curNode=curNode->GetRightChild();

}

else

{

fout<<curNode->GetCounter()<<" ";

fout<<this->Visit(curNode)<<endl;

curNode=NULL;

}

}

}

template <typename T >

void BinaryTree < T > ::PostOrder1(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

if (root!=NULL)

{

PostOrder1(root->GetLeftChild());

PostOrder1(root->GetRightChild());

fout<<root->GetCounter()<<" ";

fout<<this->Visit(root)<<endl;

}

}

template <typename T >

void BinaryTree < T > ::LeveOrder(BinaryTreeNode<T> *root)

{

ofstream fout("output.txt",ios::app);

queue <BinaryTreeNode < T > *> q;

BinaryTreeNode < T > *curNode=root;

if (curNode)

q.push(curNode);

while (!q.empty())

{

curNode=q.front();

q.pop();

fout<<curNode->GetCounter()<<" ";

fout<<this->Visit(curNode)<<endl;

if (curNode->GetLeftChild())

q.push(curNode->GetLeftChild());

if (curNode->GetRightChild())

q.push(curNode->GetRightChild());

}

}

template <typename T >

void BinaryTree < T > ::DeleteBinaryTree(BinaryTreeNode<T> *root)

{

queue <BinaryTreeNode < T > *> q;

BinaryTreeNode < T > *curNode=root;

if (curNode)

q.push(curNode);

while (!q.empty())

{

curNode=q.front();

q.pop();

cout<<curNode->GetCounter()<<" ";

cout<<this->Visit(curNode)<<endl;

if (curNode->GetLeftChild())

q.push(curNode->GetLeftChild());

if (curNode->GetRightChild())

q.push(curNode->GetRightChild());

delete curNode;

}

/*

if (root->GetLeftChild()==NULL && root->GetRightChild()==NULL)

{

root->~BinaryTreeNode();

}

else

{

if (root->GetLeftChild()!=NULL)

this->DeleteBinaryTree(root->GetLeftChild());

if (root->GetRightChild()!=NULL)

this->DeleteBinaryTree(root->GetRightChild());

}*/

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: