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