树和 二叉树 叉树遍历线索二叉树二叉搜索树二叉树的计数.ppt
文本预览下载声明
树和 二叉树 二叉树遍历 线索二叉树 二叉搜索树 二叉树的计数 堆 树与森林 霍夫曼树及其应用 一、树和二叉树 树tree的定义 (1) 无结点的树 空树 (2) 非空树 仅有一个根结点 其余结点分为若干 互不相交的子树 二叉树的存储 完全二叉树的顺序存储 #define MaxTreeSize 100 typedef TElemType SqBiTree[MaxTreeSize]; SqBiTree bt; 二叉树的链式存储 树结点 二叉树结点类的定义 #ifndef TREENODE_CLASS #define TREENODE_CLASS #ifndef NULL const int NULL = 0; #endif // NULL template class Tclass BinSTree; template class T class TreeNode { protected: TreeNodeT *left, *right; public: T data; TreeNode (const T item, TreeNodeT *lptr = NULL, TreeNodeT *rptr = NULL); virtual ~TreeNode(void); TreeNodeT* Left(void) const; TreeNodeT* Right(void) const; void GetLeft(TreeNodeT*lptr); void GetRight(TreeNodeT*rptr); friend class BinSTreeT; }; // the pointer NULL assigns an empty tree template class T TreeNodeT::TreeNode (const T item, TreeNodeT *lptr, TreeNodeT *rptr): data(item), left(lptr), right(rptr) {} // method Left allows the user// to reference the left child template class T TreeNodeT* TreeNodeT::Left(void) const { // return the private member value left return left; } // method Left allows the user //to reference the right child template class T TreeNodeT* TreeNodeT::Right(void) const { // return the private member value right return right; } // does nothing. //exists so nodes derived from it will be // destroyed properly by delete. //used in Chapter 13 for AVL trees template class T TreeNodeT::~TreeNode(void) {} #endif 建立一个结点 template class T TreeNodeT *GetTreeNode(T item, TreeNodeT *lptr= NULL, TreeNodeT *rptr = NULL) { TreeNodeT *p; p = new TreeNodeT (item, lptr, rptr); if (p == NULL) { cerr Memory allocation failure!\n; exit(1); } return p; } 撤销一个结点 template class T void FreeTreeNode(TreeNodeT *p) { delete p; } 创建三棵树 void MakeCharTree(TreeNodecha
显示全部