I have these classes and structs where each Thread contains a number of Posts.
class Post{
private:
std::string Title;
std::string Creator;
std::string text;
Date PostDate;
int id;
public:
Post();
~Post();
void postprint() const;
int idprint();
std::string get_creator() const;
};
class Thread{
private:
std::string Subject;
std::string Creator;
Date ThreadDate;
int post_nums = rand()%10+1; // this will be the number of posts that will be made in the thread, used in threadprint()
Post *posts = new Post[post_nums]; //so the randomizer doesn't return 0 array cells, max 10 posts for each thread
public:
Thread();
~Thread();
std::string subjprint() const;
void threadprint() const;
int getnums() const;
Post *getposts() const;
};
struct Post_list{
Post *post;
Post_list *next;
Post_list();
void list_insert(Post *&);
};
struct Tree_node{
std::string Creator;
Post_list *list_ptr;
Tree_node *left;
Tree_node *right;
};
class Thread_tree{
private:
Tree_node *node_root;
void Tree_create(Tree_node *&);
void Tree_insert(const std::string, Tree_node *&);
void insert_in_list(Post *&);
Tree_node *inorder_search(const std::string creator);
public:
Thread_tree(const Thread&);
~Thread_tree();
};
I've made a BST tree out of Tree_node
s which sorts a thread's posts by creator, putting all the posts of the Thread that were made by the same creator in the Post_list
of the corresponding Tree_Node
. I have this insert_in_list()
function, which calls the search()
function:
void Thread_tree::insert_in_list(Post *&Post1){
string string1 = Post1->get_creator();
Tree_node *ptr = inorder_search(string1); //ptr gets the tree_node with the corresponding creator
Post_list *ptr1 = new Post_list();
cout << "new list node was made" << endl;
(ptr->list_ptr) = ptr1;
(ptr->list_ptr)->post = Post1;
}
Tree_node *Thread_tree::search(const string creator){ //returns the Tree_node that has the same creator as the post's creator
if (creator == node_root->Creator){
cout << "\nFound correct creator\n" << endl;
return node_root;
}
else if (creator.compare(node_root->Creator) < 0){ //creator in string 'creator' is smaller than the root->Creator
cout << "\nsearch goes to left tree node to find creator" << endl;
(node_root->left) = inorder_search(creator); // in ascending order
}
else if (creator.compare(node_root->Creator) > 0){ //creator in string 'creator' is bigger than the root->Creator
cout << "\nsearch goes to right tree node to find creator" << endl;
(node_root->right) = inorder_search(creator); //in ascending order
}
}
The problem is that when the posts are 3 or less than 3, the program is done correctly. If it's more than 3 posts (don't know if 3 is the condition number) the program never exits the search()
function always printing either search goes to left tree node to find creator
or search goes to right tree node to find creator
till it eventually gets a segmentation fault. Why the program works when the posts number is little, but fails for more posts? Can someone help me?
Aucun commentaire:
Enregistrer un commentaire