ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:20.09KB ,
资源ID:5372942      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5372942.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(数据结构与算法分析第八章.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

数据结构与算法分析第八章.docx

1、数据结构与算法分析第八章首页, 上一页, 下一页; 目录 第八章二叉树8.1基本概念树是一种非线性的数据结构,它在客观世界中广泛存在,例如人类社会的族谱和各种社会组织机构都可以用树来表示。我们最常用到的是树和二叉树,其中又以二叉树更为实用。为什么这样说呢?因为大部分的操作都可以转变为一个父亲、一个左儿子和一个右儿子来实现,而且对二叉树的操作更为简单。8.2代码实现二叉树的代码实现如下:/ FileName : btree.h/ Version : 0.10/ Author : Luo Cong/ Date : 2005-1-12 12:22:40/ Comment : /#ifndef _BI

2、NARY_TREE_H_#define _BINARY_TREE_H_#include #include #ifdef _DEBUG#define DEBUG_NEW new (_NORMAL_BLOCK, THIS_FILE, _LINE_)#endif#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif#ifdef _DEBUG#ifndef ASSERT#define ASSERT assert#endif#else / not _DEBUG#ifndef ASSER

3、T#define ASSERT#endif#endif / _DEBUGtemplateclass CBTNodepublic: T data; CBTNode *parent; CBTNode *left; CBTNode *right; CBTNode( T data = T(), CBTNode *parent = NULL, CBTNode *left = NULL, CBTNode *right = NULL ) : data(data), parent(parent), left(left), right(right) ;templateclass CBTreeprotected:

4、 CBTNode *m_pNodeRoot;public: CBTree(CBTNode *initroot = NULL); CBTree(); void AssignTo(CBTNode *p); void Copy(CBTree &p);private: CBTNode* Copy(CBTNode *p); void DestroyNode(CBTNode *p); void PreOrderTraverse( const CBTNode *p, void (*Visit)(const T &data) ) const; void InOrderTraverse( const CBTNo

5、de *p, void (*Visit)(const T &data) ) const; void PostOrderTraverse( const CBTNode *p, void (*Visit)(const T &data) ) const; void GetNodesCount(const CBTNode *p, unsigned int *unCount) const; void GetLeafCount(const CBTNode *p, unsigned int *unCount) const;public: T& GetNodeData(CBTNode *p); T GetNo

6、deData(const CBTNode *p) const; void SetNodeData(CBTNode *p, const T &data); CBTNode*& GetRoot(); CBTNode* GetRoot() const; CBTNode*& GetParent(CBTNode *p); CBTNode* GetParent(const CBTNode *p) const; CBTNode*& GetLeftChild(CBTNode *p); CBTNode* GetLeftChild(const CBTNode *p) const; CBTNode*& GetRig

7、htChild(CBTNode *p); CBTNode* GetRightChild(const CBTNode *p) const; CBTNode*& GetLeftSibling(CBTNode *p); CBTNode* GetLeftSiblig(const CBTNode *p) const; CBTNode*& GetRightSibling(CBTNode *p); CBTNode* GetRightSibling(const CBTNode *p) const;public: int IsEmpty() const; void Destroy(); void PreOrde

8、rTraverse(void (*Visit)(const T &data) const; void InOrderTraverse(void (*Visit)(const T &data) const; void PostOrderTraverse(void (*Visit)(const T &data) const; unsigned int GetNodesCount() const; / Get how many nodes unsigned int GetLeafCount() const; unsigned int GetDepth() const; unsigned int Ge

9、tDepth(const CBTNode *p) const;templateinline CBTree:CBTree(CBTNode *initroot) : m_pNodeRoot(initroot)templateinline CBTree:CBTree() Destroy();templateinline void CBTree:AssignTo(CBTNode *p) ASSERT(p); m_pNodeRoot = p;templateinline void CBTree:Copy(CBTree &p) if (NULL != p.m_pNodeRoot) m_pNodeRoot

10、= Copy(p.m_pNodeRoot); else m_pNodeRoot = NULL;templateinline CBTNode* CBTree:Copy(CBTNode *p) if (p) CBTNode *pNewNode = new CBTNode; if (NULL = pNewNode) return NULL; pNewNode-data = p-data; pNewNode-parent = p-parent; pNewNode-left = Copy(p-left); pNewNode-right = Copy(p-right); return pNewNode;

11、else return NULL;templateinline CBTNode*& CBTree:GetLeftChild(CBTNode *p) ASSERT(p); return *(&(p-left);templateinline CBTNode* CBTree:GetLeftChild(const CBTNode *p) const ASSERT(p); return p-left;templateinline CBTNode*& CBTree:GetRightChild(CBTNode *p) ASSERT(p); return *(&(p-right);templateinline

12、 CBTNode* CBTree:GetRightChild(const CBTNode *p) const ASSERT(p); return p-right;templateinline CBTNode*& CBTree:GetLeftSibling(CBTNode *p) ASSERT(p); if (p-parent) return *(&(p-parent-left); else return *(&(p-parent); / return NULL;templateinline CBTNode* CBTree:GetLeftSiblig(const CBTNode *p) cons

13、t ASSERT(p); if (p-parent) return p-parent-left; else return p-parent; / return NULL;templateinline CBTNode*& CBTree:GetRightSibling(CBTNode *p) ASSERT(p); if (p-parent) return *(&(p-parent-right); else return *(&(p-parent); / return NULL;templateinline CBTNode* CBTree:GetRightSibling(const CBTNode

14、*p) const ASSERT(p); if (p-parent) return p-parent-right; else return p-parent; / return NULL;templateinline CBTNode*& CBTree:GetParent(CBTNode *p) ASSERT(p); return *(&(p-parent);templateinline CBTNode* CBTree:GetParent(const CBTNode *p) const ASSERT(p); return p-parent;templateinline T& CBTree:Get

15、NodeData(CBTNode *p) ASSERT(p); return p-data;templateinline T CBTree:GetNodeData(const CBTNode *p) const ASSERT(p); return p-data;templateinline void CBTree:SetNodeData(CBTNode *p, const T &data) ASSERT(p); p-data = data;templateinline int CBTree:IsEmpty() const return NULL = m_pNodeRoot;templatein

16、line CBTNode*& CBTree:GetRoot() return *(&(m_pNodeRoot);templateinline CBTNode* CBTree:GetRoot() const return m_pNodeRoot;templateinline void CBTree:DestroyNode(CBTNode *p) if (p) DestroyNode(p-left); DestroyNode(p-right); delete p; templateinline void CBTree:Destroy() DestroyNode(m_pNodeRoot); m_pN

17、odeRoot = NULL;templateinline void CBTree:PreOrderTraverse(void (*Visit)(const T &data) const PreOrderTraverse(m_pNodeRoot, Visit);templateinline void CBTree:PreOrderTraverse( const CBTNode *p, void (*Visit)(const T &data) const if (p) Visit(p-data); PreOrderTraverse(p-left, Visit); PreOrderTraverse

18、(p-right, Visit); templateinline void CBTree:InOrderTraverse(void (*Visit)(const T &data) const InOrderTraverse(m_pNodeRoot, Visit);templateinline void CBTree:InOrderTraverse( const CBTNode *p, void (*Visit)(const T &data) const if (p) InOrderTraverse(p-left, Visit); Visit(p-data); InOrderTraverse(p

19、-right, Visit); templateinline void CBTree:PostOrderTraverse(void (*Visit)(const T &data) const PostOrderTraverse(m_pNodeRoot, Visit);templateinline void CBTree:PostOrderTraverse( const CBTNode *p, void (*Visit)(const T &data) const if (p) PostOrderTraverse(p-left, Visit); PostOrderTraverse(p-right,

20、 Visit); Visit(p-data); templateinline unsigned int CBTree:GetNodesCount() const unsigned int unCount; GetNodesCount(m_pNodeRoot, &unCount); return unCount;templateinline void CBTree:GetNodesCount( const CBTNode *p, unsigned int *unCount) const ASSERT(unCount); unsigned int unLeftCount; unsigned int

21、 unRightCount; if (NULL = p) *unCount = 0; else if (NULL = p-left) & (NULL = p-right) *unCount = 1; else GetNodesCount(p-left, &unLeftCount); GetNodesCount(p-right, &unRightCount); *unCount = 1 + unLeftCount + unRightCount; templateinline unsigned int CBTree:GetLeafCount() const unsigned int unCount

22、 = 0; GetLeafCount(m_pNodeRoot, &unCount); return unCount;templateinline void CBTree:GetLeafCount( const CBTNode *p, unsigned int *unCount) const ASSERT(unCount); if (p) / if the nodes left & right children are both NULL, it must be a leaf if (NULL = p-left) & (NULL = p-right) +(*unCount); GetLeafCo

23、unt(p-left, unCount); GetLeafCount(p-right, unCount); templateinline unsigned int CBTree:GetDepth() const / Minus 1 here because I think the root nodes depth should be 0. / So, dont do it if u think the root nodes depth should be 1. return GetDepth(m_pNodeRoot) - 1;templateinline unsigned int CBTree

24、:GetDepth(const CBTNode *p) const unsigned int unDepthLeft; unsigned int unDepthRight; if (p) unDepthLeft = GetDepth(p-left); unDepthRight = GetDepth(p-right); return 1 + / if dont plus 1 here, the trees depth will be always 0 (unDepthLeft unDepthRight ? unDepthLeft : unDepthRight); else return 0;#e

25、ndif / _BINARY_TREE_H_测试代码:/ FileName : btree.cpp/ Version : 0.10/ Author : Luo Cong/ Date : 2005-1-12 13:17:07/ Comment : /#include #include btree.husing namespace std;/ 结点的数据类型typedef char ElementType;/ 回调函数:Visit() = PrintElement()static void PrintElement(const ElementType &data) cout data;int main() CBTNode *pRoot; CBTNode *pLeftChild; CBTNode *pRightChild; CBTree btree;#ifdef _DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);#endif pRoo

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

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