二叉树的实现_第1页
二叉树的实现_第2页
二叉树的实现_第3页
二叉树的实现_第4页
二叉树的实现_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#if 0 #include #include #include using namespace std; template classBinaryTreeNode public:Type m_Data;BinaryTreeNode*m_leftChild; BinaryTreeNode*m_rightChild;public:BinaryTreeNode() m_leftChild = m_rightChild = NULL; BinaryTreeNode(constType&data, BinaryTreeNode *leftChild = NULL, BinaryTreeNode *ri

2、ghtChild = NULL)m_Data = data; m_leftChild = leftChild; m_rightChild = rightChild;Type &GetData() return m_Data; ;BinaryTreeNode*GetleftChild() return m_leftChild; BinaryTreeNode*GetrightChild() return m_rightChild; voidsetData(constType&data) m_Data = data; voidsetLeftChild(BinaryTreeNode*leftChild

3、) m_leftChild = leftChild; voidsetRightChild(BinaryTreeNode*rightChild) m_rightChild = rightChild; ;template classBinaryTree public:BinaryTreeNode*m_root;public:BinaryTree() m_root = NULL; BinaryTree(T data) m_root = new BinaryTreeNode(data); virtual BinaryTree();/判断节点是否为空 boolIsEmpty()const;/判断是否为左

4、孩子 boolIsLeftChild(BinaryTreeNode*p);/判断是否为右孩子 boolIsRightChild(BinaryTreeNode*p);/获得整棵树的根BinaryTreeNode *GetRoot();/取得一个节点的父节点指针 BinaryTreeNode*GetParent(BinaryTreeNode*p);/取得一个节点的左孩子指针 BinaryTreeNode*GetLeftChild(BinaryTreeNode*p);/取得一个节点的右孩子指针 BinaryTreeNode*GetRightChild(BinaryTreeNode*p);/取得一个节

5、点的左兄弟指针 BinaryTreeNode*GetLeftSibling(BinaryTreeNode*p);/取得一个节点的右兄弟指针 BinaryTreeNode*GetRightSibling(BinaryTreeNode*p);/返回一个节点的数据TRetrieve(BinaryTreeNode*p);/设置一个节点的数据void Assign(BinaryTreeNode*p, const T &d); /插入右边孩子到当前节点 voidInsertRightChild(BinaryTreeNode*p, const T &d); /插入左孩子到当前节点 voidInsertLef

6、tChild(BinaryTreeNode*p, const T &d);/线序遍历二叉树virtual void PreOrderTraverse(BinaryTreeNode*root)const;/中序遍历二叉树 virtual void InOrderTraverse(BinaryTreeNode*root)const;/后序遍历二叉树virtual void PostOrderTraverse(BinaryTreeNode*root)const; protected:/得到本节点的父节点 BinaryTreeNode*Parent(BinaryTreeNode*root, Binar

7、yTreeNode*p); /删除节点void Destroy(BinaryTreeNode*p);templateBinaryTree:BinaryTree()Destroy(m_root); m_root = NULL;/判断节点是否为空template boolBinaryTree:IsEmpty()const return NULL = m_root ? true : false; /判断是否为左孩子 templateboolBinaryTree:IsLeftChild(BinaryTreeNode*p)return Parent(m_root,p)-GetleftChild() =

8、p;/判断是否为右孩子template boolBinaryTree:IsRightChild(BinaryTreeNode*p)return Parent(m_root,p)-GetrightChild() = p;/获得整棵树的根templateBinaryTreeNode *BinaryTree:GetRoot()returnm_root;/取得一个节点的父节点指针templateBinaryTreeNode*BinaryTree:GetParent(BinaryTreeNode*p)return Parent(m_root,p);/取得一个节点的左孩子指针templateBinaryT

9、reeNode*BinaryTree:GetLeftChild(BinaryTreeNode*p) if (NULL=p)return NULL:return p-GetleftChild();/取得一个节点的右孩子指针templateBinaryTreeNode*BinaryTree:GetRightChild(BinaryTreeNode*p) if (NULL = p)return NULL;return p-GetrightChild();/取得一个节点的左兄弟指针templateBinaryTreeNode*BinaryTree:GetLeftSibling(BinaryTreeNo

10、de*p) if (p = m_root)return NULL;BinaryTreeNode*temp; temp= Parent(m_root,p); if (p=temp-GetleftChild) return NULL;return temp-GetleftChild();/取得一个节点的右兄弟指针 templateBinaryTreeNode*BinaryTree:GetRightSibling(BinaryTreeNode*p) if (p = m_root) return NULL;BinaryTreeNode*temp;temp = Parent(m_root, p);if

11、(p = temp-GetrightChild()return NULL;return temp-GetrightChild();/返回一个节点的数据 templateT BinaryTree:Retrieve(BinaryTreeNode*p)return p-GetData();/设置一个节点的数据 template voidBinaryTree:Assign(BinaryTreeNode*p,const T &d) p-setData(d);/插入右边孩子到当前节点 template voidBinaryTree:InsertRightChild(BinaryTreeNode*p,con

12、st T &d) BinaryTreeNode*temp=new BinaryTreeNode(d); temp-setRightChild(p-GetrightChild();p-setRightChild(temp);/插入左孩子到当前节点 template voidBinaryTree:InsertLeftChild(BinaryTreeNode*p, const T &d) BinaryTreeNode*temp = new BinaryTreeNode(d);temp-setLeftChild(p-GetleftChild(); p-setLeftChild(temp);/先序遍历二

13、叉树 template voidBinaryTree:PreOrderTraverse(BinaryTreeNode*root)const stackBinaryTreeNode* s;BinaryTreeNode*p = root;while (!s.empty()|p!=NULL)while (p)s.push(p);coutGetData();p = p-GetleftChild();p = s.top();s.pop();p = p-GetrightChild();/中序遍历二叉树 template voidBinaryTree:InOrderTraverse(BinaryTreeNo

14、de*root)const stackBinaryTreeNode* s;BinaryTreeNode*p = root;while (!s.empty()|NULL!=p)while (p)s.push(p); p-GetleftChild();p = s.top();s.pop();coutGetData();p = p-GetrightChild(); /后序遍历二叉树 template voidBinaryTree:PostOrderTraverse(BinaryTreeNode*root)const if (NULL != root)PostOrderTraverse(root-Ge

15、tleftChild();PostOrderTraverse(root-GetrightChild();coutGetData();/得到本节点的父节点templateBinaryTreeNode*BinaryTree:Parent(BinaryTreeNode*root, BinaryTreeNode*p) if (NULL = root)return NULL;if (root-GetleftChild() = p | root-GetrightChild = p)return root;if (NULL != (q = Parent(root-GetleftChild(), p)return q;elsereturn Parent(root-GetrightChild(), p);/删除节点template voidBinaryTree:Destroy(BinaryTreeNode*p) if (NULL != p)Destroy(p-GetleftChi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论