哈夫曼樹,哈夫曼

來源:互聯網
上載者:User

哈夫曼樹,哈夫曼

關於哈夫曼樹其基本定義我就在這不多說了,我寫的這個哈夫曼樹沒有太多的功能,就是我們參考書上的一個作業,就倆個功能,一個是表示孩子雙親標記法,,還有就是吧這個最有二叉樹給畫出來,其他的就沒有了。另外在今天我調試的時候發現My Code還是有很多欠缺的地方的,比如哦說我的畫圖功能中就沒有辦法完整的輸出,而是無法輸出最後四個結點。原因我還在尋找,

下面就哈夫曼樹我來談談我當時的構思:

首先我是吧他當作一個二叉樹來寫的。我首先申明一個結點構造的結點函數element,在element中我全部都是用的int 來定義的,因為在哈發滿樹中我們需要的是每個節點的權值,所以我們就不需要用到類模板 了,而是直接用int來解決這個類。然偶就是在這個類中添磚加瓦的事情了。自己想要什麼函數,在裡面申明解釋就行了。我在建構函式的時候其中有selected和畫圖函數比較難搞。下面#ifndef HFMTREE_H
#define HFMTREE_H
const int Max = 100;
#include"BiTree.h"
#include<iostream>
using namespace std;
struct element
{
 int weight;
 int lchild, rchild, parent;
};
class HfmTree
{
public:
 HfmTree(int size);
 void Creat(int n);
 void SeLect(int k, int &s1, int &s2 ,int z);
 void reset(int size);
 void print(int size);
 void Makeary(int size);
 void printInl(int size, int i);
 void printInr(int size, int i);
 void Build(int size);
private:
 int m1;
 int top = 0;
 element  *root;
 int maxsize;
 int *pz;
 element *root3;
};

void HfmTree::reset(int size)
{
 int m;
 m = 2 * size - 1;
 pz = new int[m];
 int i;
 pz[0] = root[m].weight;
 for (i = 1; i < m; i++)
  pz[i] = 0;
 
}
void HfmTree::Build(int size)
{
 BiTree<int> zxh1(pz, 2 * size - 1, 0);
 cout << zxh1;
}
void HfmTree::printInl(int m, int i)
{
 if (m == -1)return;
 if (m!=-1)
 {
  //pz[i - 1] = root[m].weight;
  pz[2 * i - 1] = root[root[m].lchild].weight;
  pz[2 * i] = root[root[m].rchild].weight;
  m = root[m].lchild;
  printInl(m, 2 * i);
  m = root[m].lchild;
 }
}
void HfmTree::printInr(int m, int i)
{
 if (m == -1)return;
 if (m!=-1)
 {
  //pz[i - 1] = root[m].weight;
  pz[2 * i - 1] = root[root[m].lchild].weight;
  pz[2 * i] = root[root[m].rchild].weight;
  m = root[m].rchild;
  printInr(m, 2*i+1);
  m = root[m].rchild;
 }
}
void HfmTree::Makeary(int size)
{
 int m;
 m = 2 * size - 1;
 pz = new int[m];
 int i;
 for (i = 1; i <= m; i++)
  pz[i] = 0;
  i = 1;
  while (root[m].parent != size-1)
  {
   if (m == -1)break;
   pz[i-1] = root[m].weight;
   i=2*i;
   m = root[m].lchild;
  }
  m = 2 * size - 1; i = 1;
  while (root[m].parent != size-1)
  {
   if (m == -1)break;
   pz[i-1] = root[m].weight;
   i = 2 * i + 1;
   m = root[m].rchild;
  }
  BiTree<int> zxh1(pz, 2*size+1, 0);
  cout << zxh1;
}
void HfmTree::SeLect(int k, int &s1, int &s2 ,int z)
{
 int w1, w2, i = 1,j;
 w1 = w2 = Max;
 //s1 = s2 = i;
 for (i = 1; i <= k; i++)
 {
  //if (i == s1 || i == s2)continue;
  if (root[i].parent==-1)
  {
   if (root[i].weight < w1)
   {
    w2 = w1;
    w1 = root[i].weight;
    s2 = s1;
    s1 = i;                                  //w1,s1是對應的,且他們的weight是較小者
   }
   else if (root[i].weight < w2)
   {
    w2 = root[i].weight;
    s2 = i;
   }
  }
 }
}
void HfmTree::Creat(int n)
{
 int *w1;
 int i,m,*w,s1,s2; element *p;
 if (n < 1)throw"構建有誤!";
 w = new int[2 * n - 1];
 w1 = new int[2 * n - 1];
 m = 2 * n - 1;                            //n個結點過程中有2*n-1個結點
 m1 = m;
 root = new element[m + 1];
 for (i =1; i <= 2 * n - 1; i++)
 {
  //root[i].weight = NULL;
  root[i].lchild = -1;
  root[i].rchild = -1;
  root[i].parent = -1;
 }
 for (p = root + 1, i = 1; i <= n; i++, p++)
 {
  cout << "請輸入全職:" << endl;
  cin >> w[i];
  p->weight = w[i];
  p->lchild = -1;
  p->rchild = -1;
  p->parent = -1;
 }
 for (i = n+1 ; i <= m; i++, p++)
 {
  p->weight = NULL;
  p->lchild = -1;
  p->rchild = -1;
  p->parent = -1;
 }
 for (i = n+1 ; i <= m; i++)
 {
  SeLect(i-1 , s1, s2,m);
  root[s1].parent = i-1;
  root[s2].parent = i-1;
  root[i].weight = root[s1].weight + root[s2].weight;
  root[i].lchild = s1;
  root[i].rchild = s2;
 }
 for (int i = 1; i <= n; i++)
 {
  w1[i-1] = w[n - i + 1];
 }
}

HfmTree::HfmTree(int size)
{
 Creat(size);
}
void HfmTree::print(int size)
{
 int m, i;
 m = 2 * size - 1;//樹結點總個數。
 cout << "構建的哈夫曼樹為" << endl;
 cout << "no" <<"  "<< "weight" <<"  "<<"Parent"<<"  "<< "lchild" <<"  "<< "rchild" << endl;
 for (i = 1; i <= m; i++){
  cout << i <<"    "<< root[i].weight <<"     "<<root[i].parent<<"       "<< root[i].lchild <<"      "<< root[i].rchild << endl;
 }
}
#endif
——————————————————————————————————————————————————————————————————————
#include"HfmTree.h"
//#include"BiTree.h"
#include<iostream>
using namespace std;
void main()
{
 int num;
 cout << "請輸入您想構造的哈夫曼樹的結點個數(num):"; cin >> num;
 HfmTree zxh(num);
 zxh.print(num);
 //zxh.Makeary(num);
 zxh.reset(num);
 zxh.printInl(2*num-1,1);
 zxh.printInr(2*num-1, 1);
 zxh.Build(num);
}主要用代碼解釋



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.