/头文件
template <typename T >
class BinaryTreeNode
{
private:
T element;
BinaryTreeNode < T > *left;
BinaryTreeNode < T > *right;
public:
BinaryTreeNode ();
BinaryTreeNode (const& ele);
BinaryTreeNode (const& ele,BinaryTreeNode < T > *l,BinaryTreeNode < T > *r);
T value() const; //返回当前结点的数据
BinaryTreeNode < T > * LeftChild()const;
BinaryTreeNode < T > * RightChld() const;
void SetLeftChild(BinaryTreeNode < T > *);
void SetRightChild(BinaryTreeNode < T > *);
void SetValue(const T & val); //设置当前结点的数据
bool IsLeaf() const;
BinaryTreeNode < T > & operator =(const BinaryTreeNode < T > & Node);
};
template <typename T >
class BinaryTree
{
private:
BinaryTreeNode < T > *root;
BinaryTreeNode < T > *GetParent(BinaryTreeNode *root,BinaryTreeNode < T > *current);
public:
BinaryTree(r=NULL)
{
root=r;
}
~BinaryTree()
{
DeleteBinaryTree(root);
}
bool IsEmpty() const
{
return (root?(false,true));
}
BinaryTreeNode < T > * Root()
{
return root;
}
void Visit(BinaryTreeNode < T > *current)
{
cout<<current->value()<<" ";
}
BinaryTreeNode < T > * GetParent(BinaryTreeNode < T > * current);
BinaryTreeNode < T > * LeftSibling(BinaryTreeNode < T > * current);//返回current结点的左兄弟
BinaryTreeNode < T > * RightSibling(BinaryTreeNode < T > * current);//返回current结点的右兄弟
void CreatTree(const T & ele,BinaryTree< T > &leftTree,BinaryTree < T > &rightTree);
void PreOrder(BinaryTreeNode < T > *root); //前序周游
void InOrder(BinaryTreeNode < T > *root); //中序周游
void PostOrder(BinaryTreeNode < T > *root); //后序周游
void LeveOrder(BinaryTreeNode < T > *root); //按层次周游
void DeleteBinaryTree(BinaryTreeNode < T > *root);
};
/实现
#include <stack>
#include "tree.h"
#include <queue>
using namespace std;
template <typename T >
BinaryTreeNode::BinaryTreeNode ()
{
element=0;
left=right=NULL;
}
template <typename T >
BinaryTreeNode::BinaryTreeNode (const& ele)
{
element=ele;
left=right=NULL;
}
template <typename T >
BinaryTreeNode::BinaryTreeNode(const int &ele, BinaryTreeNode<T> *l, BinaryTreeNode<T> *r)
{
element=ele;
left=l;
right=r;
}
template <typename T >
T BinaryTreeNode < T > ::value() const
{
return this->element;
}
template <typename T >
BinaryTreeNode < T > * BinaryTreeNode < T > ::LeftChild()const
{
return this->left;
}
template <typename T >
BinaryTreeNode < T > * BinaryTreeNode < T > ::RightChld() 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;
}
template <typename T >
void BinaryTreeNode < T > ::SetValue(const T & val)
{
this->element=val;
}
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)
{
BinaryTreeNode < T > a;
a.SetValue(Node.element);
a.SetLeftChild(Node.left);
a.SetRightChild(Node.right);
return a;
}
/*-----------------------------------------BinaryTree的实现------------------------------------------*/
BinaryTreeNode < T > *BinaryTree < T > :: GetParent(BinaryTreeNode *root,BinaryTreeNode < T > *current)
{
if (root==NULL)
return NULL;
if (root->LeftChild()==current || root->RightChld()==current)
return root;
BinayTreeNode < T > *temp;
if ((temp=GetParent(root->LeftChild(),current))!=NULL)
return temp;
else
return (GetParent(root->RightChld(),current));
}
BinaryTreeNode < T > *BinaryTree < T > :: GetParent(BinaryTreeNode < T > * current)
{
if (current == NULL || current==root)
return NULL;
return (GetParent(root,current));
}
BinaryTreeNode < T > * BinaryTree < T > ::LeftSibling(BinaryTreeNode < T > * current)//返回current结点的左兄弟
{
if (current)
{
BinaryTreeNode < T > *temp;
temp=GetParent(current);
if ((temp==NULL) || (current==temp->LeftChild()))
return NULL;
else
temp->RightChld();
}
return NULL;
}
BinaryTreeNode < T > * BinaryTree < T > ::RightSibling(BinaryTreeNode < T > * current)//返回current结点的右兄弟
{
if (current)
{
BinaryTreeNode < T > * temp;
temp=GetParent(current);
if (temp==NULL || current==temp->RightChld())
return NULL;
else
temp->RightChld();
}
return NULL;
//return (GetParent(current)->RightChld());
}
void BinaryTree < T > ::CreatTree(const T & ele,BinaryTree< T > &leftTree,BinaryTree < T > &rightTree)
{
root=new BinaryTreeNode < T > ;
root->SetValue(ele);
root->SetLeftChild(leftTree.Root());
root->SetRightChild(rightTree.Root());
}
void BinaryTree < T > :: PreOrder(BinaryTreeNode < T > *root) //前序周游
{
stack < BinaryTreeNode < T > * > aStack;
BinaryTreeNode < T > * pointer=root;
while (pointer!=NULL || !aStack.empty())
{
if (pointer)
{
Visit(pointer);
aStack.push(pointer);
pointer=pointer->LeftChild();
}
else
{
pointer=aStack.top();
aStack.pop();
pointer->RightChld();
}
}
}
void BinaryTree < T > ::InOrder(BinaryTreeNode < T > *root) //中序周游
{
stack < BinaryTreeNode < T > * > aStack;
BinaryTreeNode < T > * pointer=root;
while (pointer || !aStack.empty())
{
if (pointer)
{
Stack.push(pointer);
pointer=pointer->LeftChild();
}
else
{
pointer=aStack.top();
Visit(pointer);
pointer=pointer->RightChld();
aStack.pop();
}
}
}
enum tage {left,right};
template < typename T >
struct StackElement
{
int tage;
BinaryTreeNode < T > * pointer;
};
void BinaryTree < T > ::PostOrder(BinaryTreeNode < T > *root) //后序周游
{
int tage;
stack < StackElement < T > > aStack;
StackElement < T > element;
BinaryTreeNode < T > * pointer=root;
if (root==NULL)
return ;
else
pointer=root;
while (true)
{
while (pointer!=NULL) //进左子树
{
element.pointer=pointer;
element.tage=left;
aStack.push(element);
pointer=pointer->LeftChild();
}
element=aStack.top;
aStack.pop();
pointer=element.pointer;
while (element.tage==right) //从右子树回来
{
Visit(pointer);
if (aStack.empty())
return ;
else
{
element=aStack.top();
aStack.pop();
pointer=element.pointer;
}
} //右子树
element.tage=right;
aStack.push(element);
pointer=pointer->RightChld();
}
}
void BinaryTree < T > ::LeveOrder(BinaryTreeNode < T > *root) //按层次周游
{
queue <BinartTreeNode < T > * > aQueue;
BinaryTreeNode < T > * pointer=root;
if (pointer)
aQueue.push(pointer);
while (!aQueue.empty())
{
pointer=aQueue.front();
visit(pointer);
aQueue.pop();
if (pointer->LeftChild())
aQueue.push(pointer->LeftChild());
if (pointer->RightChld())
aQueue.push(pointer->RightChld());
}
}