本人刚学数据结构,对树的基本功能网上找不到C++代码
便自己写了一份,贴出方便大家进行测试和学习。
大部分功能未测试,如有错误或者BUG,请高手们指教一下,谢谢。
结点声明:
BinTreeNode.h
1 template<typename ElemType> 2 struct BinTreeNode 3 { 4 ElemType data; //数据元素 5 BinTreeNode<ElemType> *leftChild; //指向左孩子的指针 6 BinTreeNode<ElemType> *rightChild; //指向右孩子的指针 7 BinTreeNode<ElemType> *pre; //指向双亲的指针 8 9 //函数构造 10 BinTreeNode(); 11 BinTreeNode(const ElemType &val, 12 BinTreeNode<ElemType> *lChild=NULL, 13 BinTreeNode<ElemType> *rChild=NULL); 14 BinTreeNode<ElemType> &operator =(const BinTreeNode<ElemType> ©); 15 }; 16 17 template<typename ElemType> 18 BinTreeNode<ElemType>::BinTreeNode() 19 { 20 leftChild=rightChild=pre=NULL; 21 } 22 23 template<typename ElemType> 24 BinTreeNode<ElemType>::BinTreeNode(const ElemType &val, 25 BinTreeNode<ElemType> *lChild, 26 BinTreeNode<ElemType> *rChild) 27 { 28 data=val; 29 leftChild=lChild; 30 rightChild=rChild; 31 pre=NULL; 32 } 33 34 template<typename ElemType> 35 BinTreeNode<ElemType> &BinTreeNode<ElemType>::operator =(const BinTreeNode<ElemType> ©) 36 { 37 data=copy.data; 38 leftChild=copy.leftChild; 39 rightChild=copy.leftChild; 40 pre=copy.pre; 41 }
BinTreeNode.h
类声明:
BinaryTree.h
1 #include"BinTreeNode.h" 2 template<typename ElemType> 3 class BinaryTree 4 { 5 protected: 6 //数据成员 7 BinTreeNode<ElemType> *root; 8 //辅助函数 9 void CreateBTreeHelp(BinTreeNode<ElemType> *&r,ElemType pre[],ElemType in[],int,int,int,int);//构造树 10 BinTreeNode<ElemType> *CopyTreeHelp(const BinTreeNode<ElemType> *r);//复制二叉树 11 void DestroyHelp(BinTreeNode<ElemType> *&r);//销毁r为根的二叉树 12 //先,中,后序遍历 13 void PreOrderHelp (const BinTreeNode<ElemType> *r,void (*visit) (const ElemType &))const; 14 void InOrderHelp (const BinTreeNode<ElemType> *r,void (*visit) (const ElemType &))const; 15 void PostOrderHelp(const BinTreeNode<ElemType> *r,void (*visit) (const ElemType &))const; 16 17 int HeightHelp(const BinTreeNode<ElemType> *r) const;//返回树的高度 18 int NodeCountHelp(const BinTreeNode<ElemType> *r)const;//返回树的节点个数 19 20 public: 21 BinaryTree(){root=NULL}//无参构造 22 BinaryTree(const ElemType &e);//建立以e元素为根的二叉树 23 BinaryTree(BinTreeNode<ElemType> *r);//建立以r为根的二叉树 24 virtual ~BinaryTree();//有指针用虚虚构 25 BinaryTree<ElemType> &CreateBTree(ElemType pre[],ElemType in[],int n); //构造树 26 BinTreeNode<ElemType> *GetRoot() const;//返回根 27 bool Empty()const; 28 bool GetElem(const BinTreeNode<ElemType> *cur,ElemType &e);//用e结点返回cur元素值 29 bool SetTlem(const BinTreeNode<ElemType> *cur,const ElemType &e);//e赋值给cur 30 //先,中,后序遍历 31 void PreOrder(void (*visit) (const ElemType &))const; 32 void InOrder(void (*visit) (const ElemType &))const; 33 void PostOrder(void (*visit) (const ElemType &))const; 34 //层次遍历 35 void LevelOrder(void (*visit) (const ElemType &))const; 36 int NodeCount()const; 37 BinTreeNode<ElemType> *LeftChild(const BinTreeNode<ElemType> *cur)const;//返回cur左孩子 38 BinTreeNode<ElemType> *RightChild(const BinTreeNode<ElemType> *cur)const;//返回cur右孩子 39 BinTreeNode<ElemType> *Parent(const BinTreeNode<ElemType> *cur)const;//返回cur双亲 40 void InsertLeftChild(BinTreeNode<ElemType> *cur,const ElemType &e);//插入左孩子 41 void InsertRightChild(BinTreeNode<ElemType> *cur,const ElemType &e);//插入右孩子 42 void DeleteLeftChild(BinTreeNode<ElemType> *cur);//删除左子树 43 void DeleteRightChild(BinTreeNode<ElemType> *cur);//删除右子树 44 int Height()const;//求二叉树高 45 BinaryTree(const BinaryTree<ElemType> ©);//复制构造函数 46 BinaryTree<ElemType> &operator =(const BinaryTree<ElemType> ©);//重载赋值运算符 47 }; 48 #include"CreateBTree.h" 49 #include"Destroy,copy,operator.h" 50 #include"height.h" 51 #include"NodeCount.h" 52 #include"return,set.h" 53 #include"Traversal.h"
BinaryTree.h
成员函数:
CreateBTree.h
1 template<typename ElemType> 2 void BinaryTree<ElemType>::CreateBTreeHelp(BinTreeNode<ElemType> *&r, 3 ElemType pre[],ElemType in[], 4 int preLeft,int preRight,int inLeft,int inRight) 5 6 { 7 if(preLeft>preRight||inLeft>inRight) 8 r=NULL; 9 else 10 { 11 r=new BinTreeNode<ElemType>(pre[preLeft]);//生成根结点 12 int mid=inLeft; 13 while(in[mid]!=pre[preLeft]) 14 mid++; 15 CreateBTreeHelp(r->leftChild,pre,in,preLeft+1,preLeft+mid-inLeft,inLeft,mid-1); 16 CreateBTreeHelp(r->rightChild,pre,in,preLeft+mid-inLeft+1,preRight,mid+1,inRight); 17 } 18 } 19 20 template<typename ElemType> 21 //构造树 22 BinaryTree<ElemType>& BinaryTree<ElemType>::CreateBTree(ElemType pre[],ElemType in[],int n) 23 { 24 BinTreeNode<ElemType> *r; //根 25 CreateBTreeHelp(r,pre,in,0,n-1,0,n-1); 26 //return BinaryTree<ElemType>(r);//Error:不应该返回局部变量的地址 27 *this = BinaryTree<ElemType>(r); 28 return *this; 29 }
CreateBTree.h
Destroy,copy,operator.h
1 //Destroy 2 template<typename ElemType> 3 void BinaryTree<ElemType>::DestroyHelp(BinTreeNode<ElemType> *&r) 4 { 5 if(r!=NULL) 6 { 7 DestroyHelp(r->leftChild); 8 DestroyHelp(r->rightChild); 9 delete r; 10 r=NULL; 11 } 12 } 13 template<typename ElemType> 14 //删除左子树 15 void BinaryTree<ElemType>::DeleteLeftChild(BinTreeNode<ElemType> *cur) 16 { 17 DestroyHelp(cur->leftChild); 18 } 19 template<typename ElemType> 20 //删除右子树 21 void BinaryTree<ElemType>::DeleteRightChild(BinTreeNode<ElemType> *cur) 22 { 23 DestroyHelp(cur->rightChild); 24 } 25 //虚构 26 template<typename ElemType> 27 BinaryTree<ElemType>::~BinaryTree() 28 { 29 DestroyHelp(root); 30 } 31 32 //Copy 33 template<typename ElemType> 34 BinTreeNode<ElemType> *BinaryTree<ElemType>::CopyTreeHelp(const BinTreeNode<ElemType> *r) 35 { 36 BinTreeNode<ElemType> *cur; 37 if(r==NULL) cur=NULL; 38 else 39 { 40 BinTreeNode<ElemType> *lChild=CopyTreeHelp(r->leftChild);//复制左子树 41 BinTreeNode<ElemType> *rChild=CopyTreeHelp(r->rightChild);//复制右子树 42 cur=new BinTreeNode<ElemType>(r->data,lChild,rChild); 43 //复制根节点 44 } 45 return cur; 46 } 47 template<typename ElemType> 48 BinaryTree<ElemType>::BinaryTree(const BinaryTree<ElemType> ©) 49 { 50 root=CopyTreeHelp(copy.root) 51 } 52 53 //operator = 54 template<typename ElemType> 55 BinaryTree<ElemType> &BinaryTree<ElemType>::operator=(const BinaryTree<ElemType> ©) 56 { 57 if(©!=this) 58 { 59 DestroyHelp(root); 60 root=CopyTreeHelp(copy.root); 61 } 62 return *this; 63 }
Destroy,copy,operator.h
height.h
1 template<typename ElemType> 2 int BinaryTree<ElemType>::HeightHelp(const BinTreeNode<ElemType> *r) const 3 { 4 if(r==NULL) return 0; 5 else 6 { 7 int lHeight,rHeight,height; 8 lHeight=HeightHelp(r->leftChild); 9 rHeight=HeightHelp(r->rightChild); 10 height-1=lHeight>rHeight?lHeight:rHeight;//深度为左右子树最大值加1; 11 return height; 12 } 13 14 }
height.h
NodeCount.h
1 template<class ElemType> 2 int BinaryTree<ElemType>::NodeCountHelp(const BinTreeNode<ElemType> *r) const 3 { 4 int count; 5 if (r == NULL) 6 count=0; 7 else 8 { 9 count = NodeCountHelp(r->leftChild) + NodeCountHelp(r->rightChild) + 1; 10 //左孩子加右孩子结点数再加根节点。 11 } 12 return count; 13 } 14 template<class ElemType> 15 int BinaryTree<ElemType>::NodeCount() const 16 { 17 return NodeCountHelp(root); 18 }
NodeCount.h
return,set.h
template<typename ElemType> //返回根 BinTreeNode<ElemType> *BinaryTree<ElemType>::GetRoot() const { return root; } template<typename ElemType> //用e结点返回cur元素值 bool BinaryTree<ElemType>::GetElem(const BinTreeNode<ElemType> *cur, ElemType &e) { if(cur) { e = cur->data; return true } else return false; } template<typename ElemType> //e赋值给cur bool BinaryTree<ElemType>::SetTlem(const BinTreeNode<ElemType> *cur, const ElemType &e) { if(cur) { cur->data = e; return true; } else return false; } template<typename ElemType> //返回cur左孩子 BinTreeNode<ElemType> *BinaryTree<ElemType>::LeftChild(const BinTreeNode<ElemType> *cur)const { if(cur->leftChild) return cur->leftChild; else return NULL; } template<typename ElemType> //返回cur右孩子 BinTreeNode<ElemType> *BinaryTree<ElemType>::RightChild(const BinTreeNode<ElemType> *cur)const { if(cur->RightChild) return cur->RightChild; else return NULL; } template<typename ElemType> //插入左孩子 void BinaryTree<ElemType>::InsertLeftChild(BinTreeNode<ElemType> *cur,const ElemType &e)//插入左孩子 { if(!(cur->leftChild)) cur->leftChild = new BinTreeNode<ElemType>(e); else throw "左孩子已存在!插入失败."; } template<typename ElemType> //插入右孩子 void BinaryTree<ElemType>::InsertRightChild(BinTreeNode<ElemType> *cur,const ElemType &e)//插入右孩子 { if(!(cur->rightChild)) cur->rightChild = new BinTreeNode<ElemType>(e); else throw "右孩子已存在!插入失败."; } template<typename ElemType> //返回cur的双亲 BinTreeNode<ElemType> *BinaryTree<ElemType>::Parent(const BinTreeNode<ElemType> *cur)const { if(cur->pre != NULL) return cur->pre; else return NULL; } template<typename ElemType> //建立以r为根的二叉树 BinaryTree<ElemType>::BinaryTree(BinTreeNode<ElemType> *r) { root = r; } template<typename ElemType> //建立以e元素为根的二叉树 BinaryTree<ElemType>::BinaryTree(const ElemType &e)//建立以e元素为根的二叉树 { root = new BinTreeNode(e); } template<typename ElemType> //判断树空 bool BinaryTree<ElemType>::Empty() const { return root == NULL; }
return,set.h
Traversal.h
1 #include<iostream> 2 #include"BinaryTree.h" 3 using namespace std; 4 BinaryTree<char>; 5 int main() 6 { 7 char s1[1000], s2[1000]; 8 cin >> s1 >> s2; 9 int n = strlen(s1); 10 if(n != strlen(s2)) 11 cout << "ERROR\n"; 12 BinTreeNode<char> *root = NULL; 13 BinaryTree<char> T(root); 14 T.CreateBTree(s1, s2, n); 15 T.LevelOrder(print<char>); 16 cout<<endl; 17 T.PreOrder(print<char>); 18 cout<<endl; 19 T.InOrder(print<char>); 20 cout<<endl; 21 T.PostOrder(print<char>); 22 cout<<endl; 23 }
Traversal.h
主函数:
1 #include<iostream> 2 #include"BinaryTree.h" 3 using namespace std; 4 BinaryTree<char>; 5 int main() 6 { 7 char s1[1000], s2[1000]; 8 cin >> s1 >> s2; 9 int n = strlen(s1); 10 if(n != strlen(s2)) 11 cout << "ERROR\n"; 12 BinTreeNode<char> *root = NULL; 13 BinaryTree<char> T(root); 14 T.CreateBTree(s1, s2, n); 15 T.LevelOrder(print<char>); 16 cout<<endl; 17 T.PreOrder(print<char>); 18 cout<<endl; 19 T.InOrder(print<char>); 20 cout<<endl; 21 T.PostOrder(print<char>); 22 cout<<endl; 23 }
main
运行结果如下:
时间: 2024-10-06 20:50:28