欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++二叉樹實(shí)現(xiàn)詞頻分析功能

 更新時間:2017年12月06日 11:24:01   作者:七夜落幕丶  
這篇文章主要為大家詳細(xì)介紹了C++二叉樹實(shí)現(xiàn)詞頻分析功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

/***********************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)值大的向上移動
  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ù)對象的類
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)的其他功能:

將對應(yīng)的單詞和文件寫到文件里面去,先序遍歷
優(yōu)化性能

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言中strcmp的實(shí)現(xiàn)原型

    C語言中strcmp的實(shí)現(xiàn)原型

    這篇文章主要介紹了C語言中strcmp的實(shí)現(xiàn)原型的相關(guān)資料,這里提供實(shí)例幫助大家理解這部分內(nèi)容,希望能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • 淺談QT打包的兩種方式

    淺談QT打包的兩種方式

    本文主要介紹了淺談QT打包的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 詳解C++之C++11的牛逼特性

    詳解C++之C++11的牛逼特性

    這篇文章主要介紹了C++之C++11的牛逼特性,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-09-09
  • C語言內(nèi)存對齊實(shí)例詳解

    C語言內(nèi)存對齊實(shí)例詳解

    這篇文章主要介紹了C語言內(nèi)存對齊,包括內(nèi)存對其的基本概念及用法,以及注意事項(xiàng),并以實(shí)例形式加以說明,需要的朋友可以參考下
    2014-09-09
  • C語言使用Bresenham算法生成直線(easyx圖形庫)

    C語言使用Bresenham算法生成直線(easyx圖形庫)

    這篇文章主要為大家詳細(xì)介紹了C語言使用Bresenham算法生成直線,基于easyx圖形庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++利用Socket實(shí)現(xiàn)主機(jī)間的UDP/TCP通信

    C++利用Socket實(shí)現(xiàn)主機(jī)間的UDP/TCP通信

    這篇文章主要為大家詳細(xì)介紹了C++如何利用Socket實(shí)現(xiàn)主機(jī)間的UDP/TCP通信功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-01-01
  • QT應(yīng)用啟動失敗排查方法小結(jié)

    QT應(yīng)用啟動失敗排查方法小結(jié)

    啟動QT應(yīng)用經(jīng)常會碰到應(yīng)用啟動失敗,qt platform plugin無法啟動,本文就來介紹一下QT應(yīng)用啟動失敗排查方法小結(jié),具有一定的參考價值,感興趣的可以了解以下
    2023-09-09
  • c語言中malloc、realloc與calloc 的區(qū)別以及聯(lián)系

    c語言中malloc、realloc與calloc 的區(qū)別以及聯(lián)系

    以下是對c語言中的malloc函數(shù),realloc函數(shù)與calloc函數(shù)的區(qū)別以及它們之間的聯(lián)系進(jìn)行了介紹,需要的朋友可以過來參考下
    2013-08-08
  • C語言實(shí)現(xiàn)BST二叉排序樹的基本操作

    C語言實(shí)現(xiàn)BST二叉排序樹的基本操作

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)BST二叉排序樹的基本操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持(下)

    C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持(下)

    這篇文章主要介紹的是 C++ 多線程編程建議之 C++ 對多線程/并發(fā)的支持的相關(guān)資料,承接前文 現(xiàn)代 C++ 對多線程/并發(fā)的支持,接下來我們看看回發(fā)生什么吧
    2021-10-10

最新評論