二叉树的实现.docx

上传人:b****6 文档编号:8331280 上传时间:2023-01-30 格式:DOCX 页数:9 大小:58.37KB
下载 相关 举报
二叉树的实现.docx_第1页
第1页 / 共9页
二叉树的实现.docx_第2页
第2页 / 共9页
二叉树的实现.docx_第3页
第3页 / 共9页
二叉树的实现.docx_第4页
第4页 / 共9页
二叉树的实现.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

二叉树的实现.docx

《二叉树的实现.docx》由会员分享,可在线阅读,更多相关《二叉树的实现.docx(9页珍藏版)》请在冰豆网上搜索。

二叉树的实现.docx

二叉树的实现

二叉树的实现

二叉树的实现.txt32因为爱心,流浪的人们才能重返家园;因为爱心,疲惫的灵魂才能活力如初。

渴望爱心,如同星光渴望彼此辉映;渴望爱心,如同世纪之歌渴望永远被唱下去。

#include

#include

usingnamespacestd;

#defineNULL0

 

////////////////////////////////////////

//二叉树的结点类

classBinaryTreeNode{

friendclassBinaryTree;

private:

charelement;

BinaryTreeNode*left;

BinaryTreeNode*right;

public:

BinaryTreeNode();

~BinaryTree()

{DeleteBinaryTree(root);}

//////////////////////////析构函数

BinaryTreeNode*Root(){returnroot;}//取根结点的值

BinaryTreeNode**Root1(){return&root;}//取根结点的地址值,主要是为建立树的时候使用

voidCreateTree(constchar&elem,BinaryTree&leftTree,BinaryTree&rightTree);

boolisEmpty()const;

BinaryTreeNode*Parent(BinaryTreeNode*current);

voidBuildTree(BinaryTreeNode**root);

////////前序访问

voidPreOrder(BinaryTreeNode*root)const;

///////中序遍历

voidInOrder(BinaryTreeNode*root)const;

///////后序遍历

voidPostOrder(BinaryTreeNode*root)const;

//////层序遍历

voidLevelOrder(BinaryTreeNode*root)const;

 

//////统计叶结点个数

voidCountLeaf(BinaryTreeNode*root,int&);

intBtDepth(BinaryTreeNode*root)const;

 

};

///////////////////////////////////////

///判断是否为空树

boolBinaryTree:

:

isEmpty()const

{

return((root)?

true:

false);

}

///////////////////////////

/////////////////////////////取父结点

BinaryTreeNode*BinaryTree:

:

GetParent(BinaryTreeNode*root,BinaryTreeNode*current)

{

BinaryTreeNode*temp;

if(root=NULL)

returnNULL;

if((root->leftchild()==current)||(root->rightchild()==current))

returnroot;

if((temp=GetParent(root->leftchild(),current))!

=NULL)

returntemp;

elsereturnGetParent(root->rightchild(),current);

}

////////////////////////////////////////////////

/////

BinaryTreeNode*BinaryTree:

:

Parent(BinaryTreeNode*current)

{

if((current==NULL)||(current==root))

returnNULL;

returnGetParent(root,current);

}

/////////////////////////////////////////////

//删除树

voidBinaryTree:

:

DeleteBinaryTree(BinaryTreeNode*root)

{

if(root)

{

DeleteBinaryTree(root->left);

DeleteBinaryTree(root->right);

deleteroot;

}

}

////////////////////////////////

//创建一棵二叉树

voidBinaryTree:

:

BuildTree(BinaryTreeNode**current)

{//采用前序遍历的方法建立一棵树,传递的参数为一个指向结点的指针的指针,目的是为了解决传值的问题

//输入@符号的时候,表示一棵子树结束

 

BinaryTreeNode*current1;

charch;

cout<<"Pleaseinputtheelement:

";

cin>>ch;

if(ch=='@')

*current=NULL;//current=NULL;

else{

current1=newBinaryTreeNode(ch);

if(root==NULL)

root=current1;

*current=current1;

BuildTree(&((*current)->left));

BuildTree(&((*current)->right));

}

}

/////////////////////////////////////////////定义结束

//前序访问

voidBinaryTree:

:

PreOrder(BinaryTreeNode*current)const

{

if(current!

=NULL)

{

cout<element<<"";

PreOrder(current->leftchild());

PreOrder(current->rightchild());

}

}

/////////中序遍历

voidBinaryTree:

:

InOrder(BinaryTreeNode*current)const

{

if(current!

=NULL)

{

InOrder(current->leftchild());

cout<element<<"";

InOrder(current->rightchild());

}

}

///////////////后序遍历

voidBinaryTree:

:

PostOrder(BinaryTreeNode*current)const

{

if(current!

=NULL)

{

PostOrder(current->leftchild());

PostOrder(current->rightchild());

cout<element<<"";

}

}

/////////////////////////////////////////

//////层序遍历

voidBinaryTree:

:

LevelOrder(BinaryTreeNode*current)const

{

queueaQueue;

BinaryTreeNode*pointer=current;

if(pointer)

aQueue.push(pointer);

while(!

aQueue.empty())

{

pointer=aQueue.front();

cout<element<<"";

aQueue.pop();

if(pointer->leftchild())

aQueue.push(pointer->leftchild());

if(pointer->rightchild())

aQueue.push(pointer->rightchild());

}

}

///////////////////统计叶结点个数

voidBinaryTree:

:

CountLeaf(BinaryTreeNode*root,int&count)

{

if(root!

=NULL)

{

if((root->leftchild()==NULL)&&(root->rightchild()==NULL))

count++;

CountLeaf(root->leftchild(),count);

CountLeaf(root->rightchild(),count);

}

}

////////////////计算树的高度

intBinaryTree:

:

BtDepth(BinaryTreeNode*current)const

{

//intleftdep,rightdep

return0;

}

voidmain()

{

BinaryTreeBT;

BT.BuildTree(BT.Root1());

BT.PreOrder(BT.Root());

BT.InOrder(BT.Root());

BT.PostOrder(BT.Root());

cout<<"层序访问"<

BT.LevelOrder(BT.Root());

intx=0;

BT.CountLeaf(BT.Root(),x);

cout<

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1