C++中二叉樹如何實現(xiàn)詞頻分析功能

小編給大家分享一下C++中二叉樹如何實現(xiàn)詞頻分析功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站設計、做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元衡山做網(wǎng)站,已為上家服務,為衡山各地企業(yè)和個人服務,聯(lián)系電話:18980820575

通過二叉樹存單詞,并且對總共的單詞數(shù)量進行計數(shù),二叉樹自適應的將出現(xiàn)頻率高的單詞往上移動以減少二叉樹的搜索時間。
代碼如下

/***********************genSplay.h***********************/
#ifndef _GENSPLAY_H_
#define _GENSPLAY_H_

#include <iostream>
using namespace std;

//樹節(jié)點
template<class T>
class SplayingNode
{
public:
  T info;
  SplayingNode *left, *right, *parent;  //節(jié)點指針
  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é)點指針
  void semisplay(SplayingNode<T> *); //更改樹結構,將權值大的向上移動
  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;

//用作計數(shù)對象的類
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;
}

以上是“C++中二叉樹如何實現(xiàn)詞頻分析功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享題目:C++中二叉樹如何實現(xiàn)詞頻分析功能
網(wǎng)站地址:http://muchs.cn/article44/jcphee.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、移動網(wǎng)站建設靜態(tài)網(wǎng)站、網(wǎng)站建設手機網(wǎng)站建設、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營