C++二叉樹實(shí)現(xiàn)詞頻分析功能
通過二叉樹存單詞,并且對(duì)總共的單詞數(shù)量進(jìn)行計(jì)數(shù),二叉樹自適應(yīng)的將出現(xiàn)頻率高的單詞往上移動(dòng)以減少二叉樹的搜索時(shí)間。 
代碼如下
/***********************genSplay.h***********************/
#ifndef _GENSPLAY_H_
#define _GENSPLAY_H_
#include <iostream>
using namespace std;
//樹節(jié)點(diǎn)
template<class T>
class SplayingNode
{
public:
  T info;
  SplayingNode *left, *right, *parent;  //節(jié)點(diǎn)指針
  SplayingNode(){
    left = right = parent = 0;
  }
  SplayingNode(const T &el, SplayingNode *l = 0,
         SplayingNode *r = 0, SplayingNode *p = 0)
         :info(el), left(l), right(r), parent(p){ }
};
//二叉樹
template<class T>
class SplayTree
{
protected:
  SplayingNode<T> *root;
  void rotateR(SplayingNode<T> *);  //向右旋轉(zhuǎn)
  void rotateL(SplayingNode<T> *);  //向左旋轉(zhuǎn)
  void continueRotation(SplayingNode<T> *gr, SplayingNode<T> *par,
             SplayingNode<T> *ch, SplayingNode<T> *desc); //重新定義父節(jié)點(diǎn)指針
  void semisplay(SplayingNode<T> *); //更改樹結(jié)構(gòu),將權(quán)值大的向上移動(dòng)
  void inorder(SplayingNode<T> *);  //中序遍歷
  void virtual visit(SplayingNode<T> *){ } //虛函數(shù)
public:
  SplayTree(){
    root = 0;
  }
  void inorder(){
    inorder(root);
  }
  T *search(const T &);
  void insert(const T &);
};
template<class T>
void SplayTree<T>::continueRotation(SplayingNode<T> *gr, SplayingNode<T> *par,
    SplayingNode<T> *ch, SplayingNode<T> *desc)
{
  if(gr != 0){
    if(gr->right == ch->parent)
      gr->right = ch;
    else
      gr->left = ch;
  }
  else
    root = ch;
  if(desc != 0)
    desc->parent = par;
  par->parent = ch;
  ch->parent = gr;
}
template<class T>
void SplayTree<T>::rotateR(SplayingNode<T> *p)
{
  p->parent->left = p->right;
  p->right = p->parent;
  continueRotation(p->parent->parent, p->right, p, p->right->left);
}
template<class T>
void SplayTree<T>::rotateL(SplayingNode<T> *p)
{
  p->parent->right = p->left;
  p->left = p->parent;
  continueRotation(p->parent->parent, p->left, p, p->left->right);
}
template<class T>
void SplayTree<T>::semisplay(SplayingNode<T> *p)
{
  while(p != root){
    if(p->parent->parent == NULL){
      if(p->parent->left == p)
        rotateR(p);
      else
        rotateL(p);
    }
    else if(p->parent->left == p){
      if(p->parent->parent->left == p->parent){
        rotateR(p->parent);
        p = p->parent;
      }
      else{
        rotateR(p);
        rotateL(p);
      }
    }
    else{
      if(p->parent->parent->right == p->parent){
        rotateL(p->parent);
        p = p->parent;
      }
      else{
        rotateL(p);
        rotateR(p);
      }
    }
    if(root == NULL)
      root = p;
  }
}
template<class T>
T *SplayTree<T>::search(const T &el)
{
  SplayingNode<T> *p = root;
  while(p != NULL){
    if(p->info == el){
      semisplay(p);
      return &p->info;
    }
    else if(el < p->info)
      p = p->left;
    else
      p = p->right;
  }
  return 0;
}
template<class T>
void SplayTree<T>::insert(const T &el)
{
  SplayingNode<T> *p = root, *prev = NULL, *newNode;
  while(p != 0){
    prev = p;
    if(el < p->info)
      p = p->left;
    else
      p = p->right;
  }
  if((newNode = new SplayingNode<T>(el, 0, 0, prev)) == 0){
    cerr << "no room for new node.\n";
    exit(1);
  }
  if(root == 0)
    root = newNode;
  else if(el < prev->info)
    prev->left = newNode;
  else
    prev->right = newNode;
}
template<class T>
void SplayTree<T>::inorder(SplayingNode<T> *p)
{
  if(p != 0){
    inorder(p->left);
    visit(p);
    inorder(p->right);
  }
}
#endif // _GENSPLAY_H_
/***********************Splay.cpp***********************/
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <cstdlib> //exit(0)
#include "genSplay.h"
using namespace std;
//用作計(jì)數(shù)對(duì)象的類
class Word
{
private:
  char *word;
  int freq;
  friend class WordSplay;
  //friend ostream & operator<<(ostream &out, const Word &wd);
public:
  Word(){
    freq = 1;
  }
  int operator==(const Word &ir) const{
    return strcmp(word, ir.word) == 0;
  }
  int operator<(const Word &ir) const{
    return strcmp(word, ir.word) < 0;
  }
};
class WordSplay : public SplayTree<Word>
{
private:
  int differentWords, wordCnt;
  void visit(SplayingNode<Word> *);
public:
  WordSplay(){
    differentWords = wordCnt = 0;
  }
  void run(ifstream &, char *);
};
void WordSplay::visit(SplayingNode<Word> *p)
{
  differentWords++;
  wordCnt += p->info.freq;
}
void WordSplay::run(ifstream &fin, char *filename)
{
  char ch = ' ', i;
  char s[100];
  Word rec;
  while(!fin.eof()){
    while(1){
      if(!fin.eof() && !isalpha(ch))
        fin.get(ch);
      else
        break;
    }
    if(fin.eof())
      break;
    for(i = 0; !fin.eof() && isalpha(ch); i++){
      s[i] = toupper(ch);
      fin.get(ch);
    }
    s[i] = '\0';
    if(!(rec.word = new char[strlen(s) + 1])){
      cerr << "no room for new words.\n";
      exit(1);
    }
    strcpy(rec.word, s);
    Word *p = search(rec);
    if(p == 0)
      insert(rec);
    else
      p->freq++;
  }
  inorder();
  cout << "\n\nFile " << filename
     << " contains " << wordCnt << " words among which "
     << differentWords << " are different.\n";
}
int main()
{
  char Filename[80];
  WordSplay SplayTree;
  cout << "enter a filename: ";
  cin >> Filename;
  ifstream fin(Filename);
  if(fin.fail()){
    cerr << "cannot open " << Filename << endl;
    return 1;
  }
  SplayTree.run(fin, Filename);
  fin.close();
  return 0;
}
有空回來補(bǔ)充相應(yīng)的其他功能:
將對(duì)應(yīng)的單詞和文件寫到文件里面去,先序遍歷
優(yōu)化性能
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:使用C++一步步實(shí)現(xiàn)俄羅斯方塊后續(xù)
欄 目:C語(yǔ)言
下一篇:淺談C++模板元編程
本文標(biāo)題:C++二叉樹實(shí)現(xiàn)詞頻分析功能
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1011.html
您可能感興趣的文章
- 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
 - 01-10深入二叉樹兩個(gè)結(jié)點(diǎn)的最低共同父結(jié)點(diǎn)的詳解
 - 01-10深入理解C++中常見的關(guān)鍵字含義
 - 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
 - 01-10c++中inline的用法分析
 - 01-10深入理解二叉樹的非遞歸遍歷
 - 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
 - 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
 - 01-10C++大數(shù)模板(推薦)
 - 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
 - 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
 - 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
 - 04-02c語(yǔ)言用函數(shù)寫分段 用c語(yǔ)言表示分段
 - 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
 - 04-02c語(yǔ)言編寫函數(shù)冒泡排序 c語(yǔ)言冒泡排
 - 04-02c語(yǔ)言沒有round函數(shù) round c語(yǔ)言
 - 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
 - 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
 - 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 


