template class Type class Ext Bin Tree template

  • Slides: 30
Download presentation

扩充二叉树的类定义 template <class Type> class Ext. Bin. Tree; template <class Type> class Element {

扩充二叉树的类定义 template <class Type> class Ext. Bin. Tree; template <class Type> class Element { friend class Ext. Bin. Tree; private: Type data; Element<Type> *left. Child, *right. Child; }; template <class Type> class Ext. Bin. Tree { public: Ext. Bin. Tree ( Ext. Bin. Tree<Type> &bt 1, Ext. Bin. Tree<Type> &bt 2 ) {

root→left. Child = bt 1. root; root→right. Child = bt 2. root; root→data. key

root→left. Child = bt 1. root; root→right. Child = bt 2. root; root→data. key = bt 1. root→data. key + bt 2. root→data. key; } protected: const int Default. Size = 20; Element <Type> *root; //扩充二叉树的根 }

建立霍夫曼树的算法 template <class Type> void Huffman. Tree (Type *fr, int n, Ext. Bin. Tree

建立霍夫曼树的算法 template <class Type> void Huffman. Tree (Type *fr, int n, Ext. Bin. Tree <Type> & newtree ) { Ext. Bin. Tree <Type> & first, & second; Ext. Bin. Tree <Type> Node[Dafualt. Size]; Min. Heap < Ext. Bin. Tree <Type> > hp; //最小堆 if ( n > Default. Size ) { cout << “大小 n ” << n << “超出了数组边界 ” << endl; return; } for ( int i = 0; i < n; i++ ) { Node[i]. root→data. key = fr[i];

Node[i]. root→left. Child = Node[i]. root→right. Child = NULL; } //传送初始权值 hp. Min. Heap

Node[i]. root→left. Child = Node[i]. root→right. Child = NULL; } //传送初始权值 hp. Min. Heap ( Node, n ); for ( int i = 0; i < n-1; i++ ) { //建立霍夫曼树的过程,做n-1趟 hp. Delete. Min ( first ); //选根权值最小的树 hp. Delete. Min ( second ); //选根权值次小的树 newtree = new Ext. Bin. Tree <Type> ( first, second ); //建新的根结点 hp. Insert ( newtree ); //形成新树插入 } }

【解答】前序序列 ABECDFGHIJ, 中序序列 EBCDAFHIGJ 时: A EBCD A B FHIGJ E A CD HIGJ

【解答】前序序列 ABECDFGHIJ, 中序序列 EBCDAFHIGJ 时: A EBCD A B FHIGJ E A CD HIGJ

前序序列 ABECDFGHIJ 中序序列 EBCDAFHIGJ A A B E B F C E G D

前序序列 ABECDFGHIJ 中序序列 EBCDAFHIGJ A A B E B F C E G D HI J F C G D H J I

【解答】 (1) 定义二叉树结构 template <class Type> class Binary. Tree; //二叉树 template <class Type> class

【解答】 (1) 定义二叉树结构 template <class Type> class Binary. Tree; //二叉树 template <class Type> class Bin. Tree. Node //二叉树结点 left. Child data right. Child

template <class Type> int leaf ( Bin. Tree. Node<Type>* t ) { int leaves;

template <class Type> int leaf ( Bin. Tree. Node<Type>* t ) { int leaves; if ( !t ) leaves = 0; else if ( !t→left. Child && !t→right. Child ) leaves = 1; else leaves = leaf ( t→left. Child ) + leaf ( t→right. Child ); return *leaves; }

template <class Type> void exchange ( Bin. Tree. Node<Type>* t ){ Bin. Tree. Node<Type>*

template <class Type> void exchange ( Bin. Tree. Node<Type>* t ){ Bin. Tree. Node<Type>* p; if ( t→left. Child || t→right. Child ) { //非叶结点,交换左、右子女 p = t→left. Child; t→left. Child = t→right. Child ; exchange ( t→left. Child ); exchange ( t→right. Child ); t→right. Child = p; } }

【解答】 011 10 0001 001 0100 0101 11

【解答】 011 10 0001 001 0100 0101 11

则Huffman编码为 c 1 c 2 c 8 c 3 c 4 c 5 c

则Huffman编码为 c 1 c 2 c 8 c 3 c 4 c 5 c 6 c 7 0100 10 0000 0101 011 11 0001 电文总码数为 4 * 5 + 2 * 25 + 4 * 3 + 4 * 6 + 3 * 10 + 3 * 11 + 2 * 36 + 4 * 4 = 257