二叉树的实现

/头文件

 

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());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值