# template class Type class Ext Bin Tree template

• Slides: 30

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; //扩充二叉树的根 }

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

【解答】 (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; 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>* 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