资源描述:
《实验5 树宽度 层次遍历》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验5树的宽度层次遍历#includeusingnamespacestd;#defineN20//自定义类型enumStatusCode{SUCCESS,FAIL,UNDER_FLOW,OVER_FLOW,RANGE_ERROR,DUPLICATE_ERROR,NOT_PRESENT,ENTRY_INSERTED,ENTRY_FOUND,VISITED,UNVISITED};//二叉树结点类templatestructBinTreeNode{//数据成员:ElemTypedata;//数据域BinTreeNode*le
2、ftChild;//左孩子指针域BinTreeNode*rightChild;//右孩子指针域//构造函数:BinTreeNode();//无参数的构造函数BinTreeNode(constElemType&val,//已知数据元素值,指向左右孩子的指针构造一个结点BinTreeNode*lChild=NULL,BinTreeNode*rChild=NULL);};//二叉树结点类的实现部分templateBinTreeNode::BinTreeNode()//操作结果:构造一个
3、叶结点{leftChild=rightChild=NULL;//叶结点左右孩子为空}templateBinTreeNode::BinTreeNode(constElemType&val,BinTreeNode*lChild,BinTreeNode*rChild)//操作结果:构造一个数据域为val,左孩子为lChild,右孩子为rChild的结点{data=val;//数据元素值leftChild=lChild;//左孩子rightChild=rChild;//右孩子}//二叉树类template<
4、classElemType>classBinaryTree{protected://二叉树的数据成员:BinTreeNode*root;//辅助函数:BinTreeNode*CopyTreeHelp(BinTreeNode*copy);//复制二叉树voidDestroyHelp(BinTreeNode*&r);//销毁以r为根二叉树intWidthHelp(constBinTreeNode*r)const;BinTreeNode*ParentHelp(BinTreeN
5、ode*r,constBinTreeNode*cur)const;//返回cur的双亲public://二叉树方法声明及重载编译系统默认方法声明:BinaryTree();//无参数的构造函数virtual~BinaryTree();//析构函数BinTreeNode*GetRoot()const;//返回二叉树的根boolEmpty()const;//判断二叉树是否为空intWidth()const;BinaryTree(constElemType&e);//建立以e为根的二叉树BinaryTree(constBinaryTr
6、ee©);//复制构造函数BinaryTree(BinTreeNode*r);//建立以r为根的二叉树BinaryTree&operator=(constBinaryTree©);//赋值运算符重载};templatevoidCreateBinaryTreeHelp(BinTreeNode*&r,ElemTypepre[],ElemTypein[],intpreLeft,intpreRight,intinLeft,intinRight);//已
7、知二叉树的先序序列pre[preLeft..preRight]和中序序列in[inLeft..inRight]构造以r为根的//二叉树templateBinaryTree&CreateBinaryTree(ElemTypepre[],ElemTypein[],intn);//已知先序和中序序列构造二叉树//二叉树类的实现部分templateBinaryT