Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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++_Binary Tree_Alphabetical_Tree Traversal - Fatal编程技术网

C++ 在二叉搜索树的有序遍历中,输出非常轻微

C++ 在二叉搜索树的有序遍历中,输出非常轻微,c++,binary-tree,alphabetical,tree-traversal,C++,Binary Tree,Alphabetical,Tree Traversal,在正确的位置,但顺序不正确 这三部电影的orderating()值相同。因此,它们将按与插入树相反的顺序打印 指环王:双塔 魔戒之王:魔戒的团契 指环王:国王的回归 double中的精度只有这么高。对于调试器来说,这是一个很好的例子。使用调试器时您发现了哪些问题?当超过一半的页面空间是未格式化的输出,而其余部分是巨大的代码块时,这很好地表明post不适合这样做。我们依靠最少的例子,而不是使用的全部代码。我强烈建议重构。BinarySearchTree应仅包含与创建和搜索树相关的代码。菜单之类

在正确的位置,但顺序不正确

这三部电影的
orderating()
值相同。因此,它们将按与插入树相反的顺序打印

  • 指环王:双塔
  • 魔戒之王:魔戒的团契
  • 指环王:国王的回归

double中的精度只有这么高。

对于调试器来说,这是一个很好的例子。使用调试器时您发现了哪些问题?当超过一半的页面空间是未格式化的输出,而其余部分是巨大的代码块时,这很好地表明post不适合这样做。我们依靠最少的例子,而不是使用的全部代码。我强烈建议重构。
BinarySearchTree
应仅包含与创建和搜索树相关的代码。菜单之类的东西应该放在另一个翻译单元中,例如
main
。在您的inorder中,您正在检查
if(p->left)inorder(p->left)
,这与
p->right
相同,但是,由于您已经在检查
if(p!=NULL),因此不需要进行此检查
在您的inorder函数的开头。创建一个将帮助您,也将帮助我们帮助您。
#ifndef MOVIETREE_H_INCLUDED
#define MOVIETREE_H_INCLUDED
#include <iostream>
#include <cstdlib>
#include <vector>
#include <math.h>
using namespace std;

class BinarySearchTree
{
    private:
        struct tree_node
        {
            tree_node* left;
            tree_node* right;
            int ranking;
            string title;
            int year;
            int quantity;
        };
        tree_node* root;

    public:
        BinarySearchTree()
        {
            root = NULL;
        }

        bool isEmpty() const { return root==NULL; }
        void insert(int ranking, string title, int year, int quantity);
        void remove(string title);
        void inorder(tree_node* p);
        double orderrating(string title);
        void print_inorder();
        void search(string d);
        void rent(string l);
        // void  split(const string& s, char c,vector<string>& v);
};

double BinarySearchTree::orderrating(string title)
{
    // string letters[52]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","o","p","q","r","s","t","u","v","w","x","y","z"};
    char letters[54]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
        'P','Q','R','S','T','U','V','W','X','Y','Z',':','a','b','c','d','e','f','g','h','i','j',
        'k','l','m','o','p','q','r','s','t','u','v','w','x','y','z',' '};
    double rating = 0;
    for(int i = 0; i<title.length();i++)
    {
        for(int j = 0;j<52;j++)
        {
            if(letters[j]==title.at(i))
            {
                rating = rating+pow(10,-i)*((j)%26);
            }
        }
    }
    //cout<<rating<<endl;
    return rating;
}

void split(const string& s, char c, vector<string>& v)
{
    string::size_type i = 0;
    string::size_type j = s.find(c);

    while (j != string::npos) {
        v.push_back(s.substr(i, j-i));
        i = ++j;
        j = s.find(c, j);

        if (j == string::npos)
            v.push_back(s.substr(i, s.length()));
    }
}

void BinarySearchTree::insert(int ranking, string title, int year, int quantity)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->quantity = quantity;
    t->ranking = ranking;
    t->title = title;
    t->year = year;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;
    // is this a new tree?
    if(isEmpty()) root = t;
    else
    {
        //Note: ALL insertions are as leaf nodes
        tree_node* curr;
        curr = root;
        // Find the Node's parent
        while(curr)
        {
            parent = curr;
            if(orderrating(t->title) > orderrating(curr->title)) curr = curr->right;
            else curr = curr->left;
        }

        if(orderrating(t->title) <= orderrating(parent->title))
            parent->left = t;
        else
            parent->right = t;
    }
}

void BinarySearchTree::search(string l)
{
    //Locate the element
    bool found = false;
    double d = orderrating(l);
    if(isEmpty())
    {
        cout<<" This Tree is empty! "<<endl;
        return;
    }
    tree_node* curr;
    tree_node* parent;
    curr = root;
    while(curr != NULL)
    {
        if(curr->title == l)
        {
            found = true;
            cout << "Movie Info:" << endl;
            cout << "===========" << endl;
            cout << "Ranking:" <<curr->ranking<<endl;
            cout << "Title:"<<curr->title<<endl;
            cout << "Year:" <<curr->year<<endl;
            cout << "Quantity:"<<curr->quantity<<endl;
            break;
        }
        else
        {
            parent = curr;
            if(d>orderrating(curr->title)) curr = curr->right;
            else curr = curr->left;
        }
    }
    if(!found)
    {
        cout<<" Movie not found."<<endl;
        return;
    }
}

void BinarySearchTree::rent(string l)
{
    //Locate the element
    bool found = false;
    double d = orderrating(l);
    if(isEmpty())
    {
        cout<<"This Tree is empty!"<<endl;
        return;
    }
    tree_node* curr;
    tree_node* parent;
    curr = root;
    while(curr != NULL)
    {
        if(curr->title == l)
        {
            found = true;
            if(curr->quantity!=0)
            {curr->quantity = curr->quantity-1;
                cout << "Movie has been rented." << endl;
                cout << "Movie Info:" << endl;
                cout << "===========" << endl;
                cout << "Ranking:" <<curr->ranking<<endl;
                cout << "Title:" <<curr->title<<endl;
                cout << "Year:" <<curr->year<<endl;
                cout << "Quantity:" << curr->quantity<<endl;
            }
            else{//If movie is in stock
                cout << "Movie out of stock." << endl;
            }
            break;
        }
        else
        {
            parent = curr;
            if(d>orderrating(curr->title)) curr = curr->right;
            else curr = curr->left;
        }
    }
    if(!found)
    {
        cout<<"Movie not found."<<endl;
        return;
    }
}

void BinarySearchTree::print_inorder()
{
    inorder(root);
}

int counter =0;

void BinarySearchTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<"Movie: "<<p->title<<endl;
        //cout<<" "<<p->quantity<<endl;//cout<<counter<<endl;
        if(p->right) inorder(p->right);
    }
    else return;
}
#endif // MOVIETREE_H_INCLUDED

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include "MovieTree.h"

using namespace std;

struct MovieInfo{
    int ranking;
    string title;
    int year;
    int quantity;
};

int main(int argc, char * argv[])
{
    //creates struct to store movie info
    MovieInfo MovieInfo1[51];
    int counter1 = 0;
    std::string word1;
    //"Assignment5Movies.txt"
    ifstream myfile1("Assignment5Movies.txt");
    if (myfile1.is_open())
    {
        std::string line;
        while ( getline (myfile1,line) )
        {
            //need delimeter for file.
            vector<string> v;
            string s = line;
            split(s, ',', v);
            for (unsigned int i = 0; i < v.size(); ++i)
                //add individual atributes to moviearray
            {//cout<<v[i]<<endl;
                if(i%4==1){MovieInfo1[counter1].title = v[i];}
                if(i%4==2){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].year = value;}
                if(i%4==3){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].quantity = value;}
                if(i%4==0){ int value =atoi(v[i].c_str()); MovieInfo1[counter1].ranking = value;}
            }//cout<<counter1<<endl;
            counter1++;
        }
        myfile1.close();
    }
    // construct Binary tree
    BinarySearchTree b;
    for(int i = 0; i<counter1;i++)
    {
        b.insert(MovieInfo1[i].ranking,MovieInfo1[i].title,MovieInfo1[i].year, MovieInfo1[i].quantity);
    }
    int option;
    //do while loop for user interface
    do{
        cout << "======Main Menu=====" << endl;
        cout << "1. Find a movie" << endl;
        cout << "2. Rent a movie" << endl;
        cout << "3. Print the inventory" << endl;
        cout << "4. Quit" << endl;
        cin>>option;
        cin.ignore(10000,'\n');
        if (option==1)//find movie
        {
            cout << "Enter title:" << endl;
            string temp;
            cin >> ws;
            getline(cin, temp);
            b.search(temp);
        }
        if (option==2)//rent movie
        {
            cout << "Enter title:" << endl;
            string temp;
            cin >> ws;
            getline(cin, temp);
            b.rent(temp);
        }
        if (option==3)//print inventory
        {
            b.print_inorder();
        }
    }while(option!=4);
    cout << "Goodbye!" << endl;
}
12 Angry Men  
Back to the Future  
Casablanaca  
... the rest are in order except  
Lord of the Rings: the two towers   
Lord of the Rings: the fellowship of the ring  
Lord of the Rings: the return of the king