Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;在二叉树中存储文件 我正在完成一个C++项目,其中我将文本文件作为字符串读取并存储在二进制树中。我必须将文件中的每个字母存储在一个二叉树中,以及该字母出现的次数。然后,我必须存储2个字母引用,依此类推到用户给出的k个引用。例如,如果一个文件包含abcdef,我必须在树中存储a、b、c、d、e、f,然后我再次遍历字符串,并将ab、bc、cd、de、ef和abc、bcd、cde、def等存储到用户输入的数字。存储这些字符串的次数是它们出现的次数,如果某个字符串被传递到树,而树已经包含该字符串,则该字符串的数量只需增加一次。我的代码适用于单次和双次引用,但对于大于2次的引用,它总是将最后一次双次引用传递给树两次_C++_String_Binary Tree - Fatal编程技术网

C++;在二叉树中存储文件 我正在完成一个C++项目,其中我将文本文件作为字符串读取并存储在二进制树中。我必须将文件中的每个字母存储在一个二叉树中,以及该字母出现的次数。然后,我必须存储2个字母引用,依此类推到用户给出的k个引用。例如,如果一个文件包含abcdef,我必须在树中存储a、b、c、d、e、f,然后我再次遍历字符串,并将ab、bc、cd、de、ef和abc、bcd、cde、def等存储到用户输入的数字。存储这些字符串的次数是它们出现的次数,如果某个字符串被传递到树,而树已经包含该字符串,则该字符串的数量只需增加一次。我的代码适用于单次和双次引用,但对于大于2次的引用,它总是将最后一次双次引用传递给树两次

C++;在二叉树中存储文件 我正在完成一个C++项目,其中我将文本文件作为字符串读取并存储在二进制树中。我必须将文件中的每个字母存储在一个二叉树中,以及该字母出现的次数。然后,我必须存储2个字母引用,依此类推到用户给出的k个引用。例如,如果一个文件包含abcdef,我必须在树中存储a、b、c、d、e、f,然后我再次遍历字符串,并将ab、bc、cd、de、ef和abc、bcd、cde、def等存储到用户输入的数字。存储这些字符串的次数是它们出现的次数,如果某个字符串被传递到树,而树已经包含该字符串,则该字符串的数量只需增加一次。我的代码适用于单次和双次引用,但对于大于2次的引用,它总是将最后一次双次引用传递给树两次,c++,string,binary-tree,C++,String,Binary Tree,以下是main.cpp文件: #include <iostream> #include <fstream> #include <string> #include <algorithm> #include "binarytree.h" using namespace std; int main() { string filename; string filedata; string a; string b;

以下是main.cpp文件:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include "binarytree.h"
using namespace std;

int main()
{
    string filename;
    string filedata;
    string a;
    string b;
    int num;
    BinaryTree tree;
    fstream file;

    cout << "Please enter a filename: ";
    cin >> filename;

    file.open(filename.c_str(), ios::in);

    if (!file)
    {
    cout << "ERROR: Cannot open file.";
    }

    else
    {
        cout << "Please enter the maximum number of consecutive letters to count the occurrences of: ";
        cin >> num;

        while (num < 1)
        {
            cout << "Please enter a number greater than 0: ";
            cin >> num;
        }
    }

    while (getline(file, a))
    {
        filedata += a;
    }

    cout << "File data: " << filedata << endl;
    filedata.erase(remove(filedata.begin(),filedata.end(),' '),filedata.end());
    cout << "File data: " << filedata << endl;

    for (unsigned int i = 0; i < filedata.size(); i++)
    {
        if (filedata[i] != ' ')
        {
            string a(1, filedata[i]);
            tree.insertNode(a);
        }
    }

    if (num > 1)
    {
        for (int i = 2; i < num + 1; i++)
        {
            int k = 1;
            for (unsigned int j = 0; j < filedata.size()-1; j++)
            {
                if (filedata[j+k] == '\0')
                    cout << "null" << endl;
                else if (filedata[j+k] != '\0')
                {
                    b = "";
                    b += filedata.substr(j, i);
                    tree.insertNode(b);
                }
            }
            k++;
        }
    }

    file.close();

    tree.displayInOrder();

    return 0;
}
#include <iostream>
#include <cstdlib>
#include "binarytree.h"
using namespace std;

void BinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode, string letter)
{

    if (nodePtr == NULL)
    {
        TreeNode *newTreeNode = new TreeNode;
        newTreeNode->letter = letter;
        newTreeNode->value = 1;
        newTreeNode->left = newTreeNode->right = NULL;
        if(newNode->letter > newTreeNode->letter)
            newNode->left = newTreeNode;
        else
            newNode->right = newTreeNode;
    }
    else if (nodePtr->letter > letter)
        insert(nodePtr->left, nodePtr, letter);
    else if (nodePtr->letter < letter)
        insert(nodePtr->right, nodePtr, letter);
    else if (nodePtr->letter == letter)
        nodePtr->value += 1;
}

void BinaryTree::insertNode(string letter)
{
    if (root == NULL)
    {
        root = new TreeNode;
        root->letter = letter;
        root->value = 1;
        root->left = root->right = NULL;
    }
    else if(root->letter > letter)
        insert(root->left, root, letter);
    else if(root->letter < letter)
        insert(root->right, root, letter);
    else if(root->letter == letter)
        root->value += 1;
}

void BinaryTree::destroySubTree(TreeNode *nodePtr)
{
    if (nodePtr)
    {
        if (nodePtr->left)
            destroySubTree(nodePtr->left);
        if (nodePtr->right)
            destroySubTree(nodePtr->right);
        delete nodePtr;
    }
}

void BinaryTree::displayInOrder(TreeNode *nodePtr) const
{
    if (nodePtr)
    {
        displayInOrder(nodePtr->left);
        cout << nodePtr->letter << ": " << nodePtr->value << endl;
        displayInOrder(nodePtr->right);
    }
}
当在程序中输入文本文件以及出现次数的num 3时,屏幕上会显示:

a:2 az:1 阿祖:1 e:1 欧盟:1 欧盟:1 j:1 ja:1 jaz:1 u:4 ua:2 ue:1 ueu:1 uu:2 uua:1 uue:1 z:1 祖:1 zuu:1/(每一个都在一个新行上)

正如你所看到的,ua被计算了两次,而它只应该被计算一次。这仅在num>2时发生。问题出在if(num>1)语句的某个地方。我试图避免将null字符传递到树中,并添加了一个cout语句,如果曾经达到null,则显示“null”,但从未显示,并且我不确定是什么导致了问题


谢谢你的帮助

问题在于此循环:
for(unsigned int j=0;j

这并不能保证所有插入的元素的大小都是i


如果将其替换为(unsigned int j=0;j,则j将上升到长度为i的字符串的最后一个索引,这就是您要查找的字符串。

使用调试器时,是哪条语句导致了问题?与该语句相关联的变量值是什么?是否有原因不使用
std::map
或整数数组?对于单个字母(字符),
std::string
类型可能过多。问题在“if(num>1)”语句的for循环中的某个地方,我使用整数k来更改传递到树的字符串的长度,以便根据num的不同,字符串长度会有所不同。请确定,是否将字符串或字符存储到树中?你真的不需要担心一个单词中的字符数。从文件中读取一个字符。如果是一封信,就在树上搜索。如果不存在,则插入到树中,否则递增计数器。
#include <iostream>
#include <string>
#ifndef BINARYTREE_H_
#define BINARYTREE_H_
using namespace std;

class BinaryTree
{
    private:
        struct TreeNode
        {
            string letter;
            int value;
            TreeNode *left;
            TreeNode *right;
        };

        TreeNode *root;

        void insert(TreeNode *&, TreeNode *&, string);
        void destroySubTree(TreeNode *);
        void deleteNode(int, TreeNode *&);
        void makeDeletion(TreeNode *&);
        void displayInOrder(TreeNode *) const;

    public:
        BinaryTree()
        {
            root = NULL;
        }
        ~BinaryTree()
        {
            destroySubTree(root);
        }
        void insertNode(string);
        void remove(int);
        void displayInOrder() const
        {
            displayInOrder(root);
        }
};

#endif
j a z uu e uu a