#ifndef BINARYTREE_H
#define BINARYTREE_H
#include"llqueue.h"
#include"tnode.h"
#include"stack.h"
#include"xcept.h"
#include"minheap.h"
int _count;
template<class T>
class Huffman
{
public:
friend BinaryTree<int> HuffmanTree(T [], int );
operator T () const {return weight;}
private:
BinaryTree<int> tree;
T weight;
};
template<class T>
class BinaryTree
{
public:
BinaryTree() {root = 0;};
~BinaryTree(){};
bool IsEmpty() const
{return ((root) ? 0 : 1);}
bool Root(T& x) const;
void MakeTree(const T& element,BinaryTree<T>& left, BinaryTree<T>& right);
void BreakTree(T& element, BinaryTree<T>& left, BinaryTree<T>& right);
void PreOrder(void(*Visit)(BinaryTreeNode<T> *u))
{PreOrder(Visit, root);}
void InOrder(void(*Visit)(BinaryTreeNode<T> *u))
{InOrder(Visit, root);}
void PostOrder(void(*Visit)(BinaryTreeNode<T> *u))
{PostOrder(Visit, root);}
void LevelOrder(void(*Visit)(BinaryTreeNode<T> *u));
void PreOutput()
{PreOrder(Output, root); cout << endl;}
void InOutput()
{InOrder(Output, root); cout << endl;}
void PostOutput()
{PostOrder(Output, root); cout << endl;}
void LevelOutput()
{LevelOrder(Output); cout << endl;}
void Delete()
{PostOrder(Free, root); root = 0;}
int Height() const
{return Height(root);}
int Size()
{_count = 0; PreOrder(Add, root); return _count;}
BinaryTree<int> HuffmanTree(T [], int );
private:
BinaryTreeNode<T> *root;
void PreOrder(void(*Visit)(BinaryTreeNode<T> *u),BinaryTreeNode<T> *t);
void InOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
void PostOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t);
static void Output(BinaryTreeNode<T> *t)
{
CString s;
s.Format("%c",t->data);
STR+=s;
}
static void Free(BinaryTreeNode<T> *t)
{delete t;}
static void Add(BinaryTreeNode<T> *t)
{_count++;}
int Height(BinaryTreeNode<T> *t) const;
};
template<class T>
bool BinaryTree<T>::Root(T& x) const
{
if (root)
{
x = root->data;
return true;
}
else return false;
}
template<class T>
void BinaryTree<T>::MakeTree(const T& element,BinaryTree<T>& left, BinaryTree<T>& right)
{
root = new BinaryTreeNode<T>(element, left.root, right.root);
left.root = right.root = 0;
}
template<class T>
void BinaryTree<T>::BreakTree(T& element,BinaryTree<T>& left, BinaryTree<T>& right)
{
if (!root)
throw BadInput();
element = root->data;
left.root = root->LeftChild;
right.root = root->RightChild;
delete root;
root = 0;
}
template <class T>
void BinaryTree<T>::PreOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)
{
if (t)
{
Visit(t);
PreOrder(Visit, t->LeftChild);
PreOrder(Visit, t->RightChild);
}
}
template <class T>
void BinaryTree<T>::InOrder( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)
{
if (t)
{
InOrder(Visit, t->LeftChild);
Visit(t);
InOrder(Visit, t->RightChild);
}
}
template <class T>
void BinaryTree<T>::PostOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t)
{
if (t)
{
PostOrder(Visit, t->LeftChild);
PostOrder(Visit, t->RightChild);
Visit(t);
}
}
template <class T>
void BinaryTree<T>::LevelOrder(void(*Visit)(BinaryTreeNode<T> *u))
{
LinkedQueue<BinaryTreeNode<T>*> Q;
BinaryTreeNode<T> *t=root;
while (t)
{
Visit(t);
if (t->LeftChild) Q+(t->LeftChild);
if (t->RightChild) Q+(t->RightChild);
try
{Q-(t);}
catch (OutOfBounds)
{return;}
}
}
template <class T>
int BinaryTree<T>::Height(BinaryTreeNode<T> *t) const
{
if (!t) return 0;
int hl = Height(t->LeftChild);
int hr = Height(t->RightChild);
if (hl > hr) return ++hl;
else return ++hr;
}
template <class T>
BinaryTree<int> BinaryTree<T>::HuffmanTree(T a[], int n)
{
Huffman<T> *w=new Huffman<T>[n+1];
BinaryTree<int> z,zero;
int num=0;
for(int i=1;i<=n;i++)
{
if(a[i]==0)
continue;
z.MakeTree(i,zero,zero);
w[i].weight=a[i];
w[i].tree=z;
num++;
}
MinHeap<Huffman<T>> H(1);
H.Initialize(w,n,n);
Huffman<T> x,y;
for(int i=1;i<num;i++)
{
H.DeleteMin(x);
H.DeleteMin(y);
z.MakeTree(0,x.tree,y.tree);
x.weight+=y.weight;
x.tree=z;
H.Insert(x);
}
H.DeleteMin(x);
H.Deactivate();
delete[]w;
return x.tree;
}
#endif
e:\工程文件\huffman\huffman\binarytree.h(199): error C2248: “Huffman<T>::weight”: 无法访问 private 成员(在“Huffman<T>”类中声明)
1>e:\工程文件\huffman\huffman\binarytree.h(200): error C2248: “Huffman<T>::tree”: 无法访问 private 成员(在“Huffman<T>”类中声明)
1 个解决方案
#1
太长的说。。。应该是那个函数没有声明为友元吧。楼主这样的做法还不如直接把他们设为公有成员呢。
#1
太长的说。。。应该是那个函数没有声明为友元吧。楼主这样的做法还不如直接把他们设为公有成员呢。