1、在树的孩子兄弟链表模板类中增加下列函数成员,并的验证
(1)在孩子兄弟链表模板类中增加函数成员PostRootOrder(),实现树的后根遍历。
template <class ElemType> void ChildSiblingTree<ElemType>::PostRootOrder(ChildSiblingTreeNode<ElemType> *r, void (*Visit)(const ElemType &)) const // 操作结果:按后根序依次对以r为根的树的每个元素调用函数(*visit)访问 { ChildSiblingTreeNode<ElemType> *p; if(r != NULL) { for(p = FirstChild(r); p != NULL; p = NextSibling(p)) { PostRootOrder(p, Visit); } (*Visit)(r->data); } }
(2)在孩子兄弟链表模板类中增加函数成员Height (),求树的高度。
template <class ElemType> int ChildSiblingTree<ElemType>::Height(ChildSiblingTreeNode<ElemType> *r) const // 操作结果: 求以r为根的树的高 { ChildSiblingTreeNode<ElemType> *p; int i = 0;//用递归的思想来写,而有nextSibling时,不增加高度 int max = 0; if(r != NULL) { max = 1 + Height(r->firstChild); p = r->firstChild; while(p)//寻找兄弟节点中最大的数 { i = Height(p); if(i > max) max = i; p = p->nextSibling; } } return max; } template <class ElemType> int ChildSiblingTree<ElemType>::Height() const // 操作结果: 求树的高 { return Height(root); }
(3)在孩子兄弟链表模板类中增加函数成员LeafCount(),统计树中叶子结点的数目
//操作结果:求树的叶子结点的个数 template <class ElemType> int ChildSiblingTree<ElemType>::LeafCount(ChildSiblingTreeNode<ElemType> *r) const { static int sum = 0; if(r == NULL) return 0; if(r->firstChild == NULL) { sum++; } else LeafCount(r->firstChild); LeafCount(r->nextSibling); return sum; } //操作结果:求树的叶子结点的个数 template <class ElemType> int ChildSiblingTree<ElemType>::LeafCount() const { return LeafCount(root); }
2、树的孩子兄弟表示法验证
(1)在树的孩子兄弟链表示中,利用二叉树的相应遍历算法实现树的先根遍历和后根遍历,完成算法的设计与实行,并进行测试验证,再与原有的算法进行比较。
按二叉树的方法来遍历树 template <class ElemType> void ChildSiblingTree<ElemType>::PostRootOrder(ChildSiblingTreeNode<ElemType> *r, void (*Visit)(const ElemType &)) const // 操作结果:按先根序依次对以r为根的树的每个元素调用函数(*visit)访问 { if(r !=NULL) { (*Visit)(r->data); PostRootOrder(r->firstChild, Visit); PostRootOrder(r->nextSibling, Visit); } } template <class ElemType> void ChildSiblingTree<ElemType>::PostRootOrder(ChildSiblingTreeNode<ElemType> *r, void (*Visit)(const ElemType &)) const // 操作结果:按后根序依次对以r为根的树的每个元素调用函数(*visit)访问 { if(r !=NULL)//在化为二叉树后,在二叉树中进行中序遍历得到树的后序遍历的结果,而不是直接在二叉树上后序遍历 { PostRootOrder(r->firstChild, Visit); (*Visit)(r->data); PostRootOrder(r->nextSibling, Visit); } }
(2)在孩子兄弟链表模板类中,设计并实现相应函数,求该树指定层次的叶子数。
//求指定层次的叶子数 template <class ElemType> int ChildSiblingTree<ElemType>::ALeafCount(ChildSiblingTreeNode<ElemType> *r, int k) const { LinkQueue<ChildSiblingTreeNode<ElemType> *> q; ChildSiblingTreeNode<ElemType> *cur, *p; int sum=0, index = 1, flag1 = 0, flag2 = 1; if(r != NULL) q.EnQueue(r); while(!q.Empty()) { q.DelQueue(cur); flag2--; p= cur->firstChild; while(p != NULL) { flag1++; q.EnQueue(p); p = p->nextSibling; } if(flag2 == 0) //用两个标志判断是否为这一层的最后一个元素 { flag2 = flag1; flag1 = 0; index++; } if(k == index) { while(!q.Empty()) { q.DelQueue(p); if(p->firstChild == NULL) sum++; } return sum; } } return 0; } template <class ElemType> int ChildSiblingTree<ElemType>::ALeafCount(int k) const { return ALeafCount(root, k); }
(3) 在孩子兄弟链表模板类中,设计并实现相应函数,求相应二叉树的高度和叶子数。
//求孩子兄弟表示法的二叉树的高度 template <class ElemType> int ChildSiblingTree<ElemType>::BinHeight(ChildSiblingTreeNode<ElemType> *r) const { int Leftheight, Rightheight; Leftheight = Rightheight = 0; if(r == NULL) return 0; Leftheight = BinHeight(r->firstChild) + 1; Rightheight = BinHeight(r->nextSibling) + 1; return (Leftheight > Rightheight) ? Leftheight : Rightheight; } template <class ElemType> int ChildSiblingTree<ElemType>::BinHeight() const { return BinHeight(root); }
//操作结果:求二叉树的叶子结点的个数 template <class ElemType> int ChildSiblingTree<ElemType>::BinLeafCount(ChildSiblingTreeNode<ElemType> *r) const { static int sum = 0; if(r == NULL) return 0; if(r->firstChild == NULL && r->nextSibling == NULL) { sum++; } else BinLeafCount(r->firstChild); BinLeafCount(r->nextSibling); return sum; } template <class ElemType> int ChildSiblingTree<ElemType>::BinLeafCount() const { return BinLeafCount(root); }
3、等价类模板的验证:
(1)对等价类模板中的函数成员Union()利用加权规则(将高度较低的树并入高度较高的树)进行修改,以改善等价类树的查找性能。
template <class ElemType> int Equivalence<ElemType>::GetHeight(int k) const { int height=0, max=0; for(int p =0; p < size; p++) { if(set[p].parent == k) { height = GetHeight(p) + 1; if(height > max) max = height; } } return max; } template <class ElemType> void Equivalence<ElemType>::Union(ElemType a, ElemType b) // 操作结果:合并a与b所在的等价类 { int root1 = Find(a); // 查找a所在树(等价类)的根 int root2 = Find(b); // 查找b所在树(等价类)的根 if (GetHeight(root1) > GetHeight(root2)) { set[root1].parent += set[root2].parent; set[root2].parent = root1; // 合并树(等价类) } else { set[root2].parent += set[root1].parent; set[root1].parent = root2; // 合并树(等价类) } }
(2)对等价类模板中的函数成员Find()利用折叠规则来“压缩路径”,以改善等价类树的查找性能。
template <class ElemType> int Equivalence<ElemType>::CFind(ElemType e) const { int i, k, p = 0; while(p < size && set[p].data != e) p++; if(p==size) throw Error("不存在该元素"); for(i=p; set[i].parent>=0; i = set[i].parent); while(i != set[p].parent) { k = set[p].parent; set[p].parent = i; p = k; } return i; }