本人刚学数据结构,对树的基本功能网上找不到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 }