•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,845 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,511 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1455 | Replies: 3
![]() |
•
•
Join Date: Apr 2007
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
I am having problems with the following program. We are to create a biary tree that takes words form a text file and then create a link list the the number the word was found on. I have the tree created but I am unsure how to incorporate the link list in to the program. I need the node of the word to point to the line number. If any one can help it would be greatly appreciated. Thanks!
#ifndef TREE_H
#define TREE_H
#include"stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<list>
usingnamespace std;
struct TreeNode
{
string data;
int lineNumber;
TreeNode *left;
TreeNode *right;
TreeNode *head;
TreeNode *next;
};
TreeNode *ptr = NULL;
class Tree
{
public:
Tree();
int line;
void insert (string word);
TreeNode *search (string word, int line);
void destroyTree();
void inorder();
void linkList (TreeNode *leaf, string word, int line);
void visit (TreeNode* leaf);
private:
void destroyTree(TreeNode *leaf);
void insert (string word, TreeNode *leaf);
TreeNode *search(string word, TreeNode *leaf, int line);
// void linkList(TreeNode *leaf, string word, int line);
void inorder(TreeNode *leaf);
//void visit (TreeNode *leaf);
TreeNode *root;
};
int _tmain(int argc, _TCHAR* argv[])
{
Tree tree;
char text[100];
char *s;
tree.line = 1;
ifstream myfile;
myfile.open("test.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
myfile>>text;
if(text[0] == '.')
{
tree.line++;
}
s= strtok (text, " ,.-");
while(s != NULL)
{
*s = toupper(*s);
tree.search(s,tree.line);
s = strtok (NULL, " ,.-");
}
}
}
else
{
cout<<"File did not open"<<endl;
}
tree.inorder();
system ("pause");
return 0;
}
Tree::Tree()
{
root = NULL;
}
void Tree::destroyTree(TreeNode *leaf)
{
if (leaf != NULL)
{
destroyTree(leaf->left);
destroyTree(leaf->right);
delete leaf;
}
}
void Tree::insert(std::string word, TreeNode *leaf)
{
if (word < leaf->data)
{
if (leaf->left)
{
insert(word, leaf->left);
}
else
{
leaf->left = new TreeNode;
leaf ->left->data = word;
leaf->left->left = NULL;
leaf->left->right = NULL;
}
}
else
{
if(leaf->right != NULL)
{
insert (word, leaf->right);
}
else
{
leaf->right = new TreeNode;
leaf->right->data = word;
leaf->right->right = NULL;
leaf->right->left = NULL;
}
}
}
TreeNode *Tree::search(string word, TreeNode *leaf, int line)
{
if (leaf != NULL)
{
if (word==leaf->data)
{
linkList(leaf,word,line);
return leaf;
}
if (word<leaf->data)
{
return search(word, leaf->left,line);
}
else
{
return search(word, leaf->right,line);
}
}
else
{
insert(word);
linkList(leaf,word,line);
}
return NULL;
}
void Tree::insert(string word)
{
if (root != NULL)
{
insert (word, root);
}
else
{
root = new TreeNode;
root->data = word;
root->left= NULL;
root->right= NULL;
}
}
TreeNode *Tree::search(std::string word, int line)
{
return search (word, root,line);
}
void Tree::destroyTree()
{
destroyTree(root);
}
void Tree::inorder()
{
inorder(root);
}
void Tree::inorder(TreeNode *leaf)
{
if (leaf != NULL) {
inorder(leaf->left);
visit(leaf);
inorder(leaf->right);
}
}
void Tree::visit (TreeNode *leaf)
{
cout<<leaf->data<<endl;
cout<<leaf->lineNumber<<endl;
}
void Tree::linkList(TreeNode *leaf,string word, int line)
{
TreeNode *temp;
leaf = new TreeNode;
leaf->next = NULL;
if (leaf->lineNumber == NULL)
{
leaf->lineNumber = line;
}
else
{
leaf->lineNumber = line;
leaf->next = NULL;
}
}
#ifndef TREE_H
#define TREE_H
#include"stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<list>
usingnamespace std;
struct TreeNode
{
string data;
int lineNumber;
TreeNode *left;
TreeNode *right;
TreeNode *head;
TreeNode *next;
};
TreeNode *ptr = NULL;
class Tree
{
public:
Tree();
int line;
void insert (string word);
TreeNode *search (string word, int line);
void destroyTree();
void inorder();
void linkList (TreeNode *leaf, string word, int line);
void visit (TreeNode* leaf);
private:
void destroyTree(TreeNode *leaf);
void insert (string word, TreeNode *leaf);
TreeNode *search(string word, TreeNode *leaf, int line);
// void linkList(TreeNode *leaf, string word, int line);
void inorder(TreeNode *leaf);
//void visit (TreeNode *leaf);
TreeNode *root;
};
int _tmain(int argc, _TCHAR* argv[])
{
Tree tree;
char text[100];
char *s;
tree.line = 1;
ifstream myfile;
myfile.open("test.txt");
if (myfile.is_open())
{
while (!myfile.eof())
{
myfile>>text;
if(text[0] == '.')
{
tree.line++;
}
s= strtok (text, " ,.-");
while(s != NULL)
{
*s = toupper(*s);
tree.search(s,tree.line);
s = strtok (NULL, " ,.-");
}
}
}
else
{
cout<<"File did not open"<<endl;
}
tree.inorder();
system ("pause");
return 0;
}
Tree::Tree()
{
root = NULL;
}
void Tree::destroyTree(TreeNode *leaf)
{
if (leaf != NULL)
{
destroyTree(leaf->left);
destroyTree(leaf->right);
delete leaf;
}
}
void Tree::insert(std::string word, TreeNode *leaf)
{
if (word < leaf->data)
{
if (leaf->left)
{
insert(word, leaf->left);
}
else
{
leaf->left = new TreeNode;
leaf ->left->data = word;
leaf->left->left = NULL;
leaf->left->right = NULL;
}
}
else
{
if(leaf->right != NULL)
{
insert (word, leaf->right);
}
else
{
leaf->right = new TreeNode;
leaf->right->data = word;
leaf->right->right = NULL;
leaf->right->left = NULL;
}
}
}
TreeNode *Tree::search(string word, TreeNode *leaf, int line)
{
if (leaf != NULL)
{
if (word==leaf->data)
{
linkList(leaf,word,line);
return leaf;
}
if (word<leaf->data)
{
return search(word, leaf->left,line);
}
else
{
return search(word, leaf->right,line);
}
}
else
{
insert(word);
linkList(leaf,word,line);
}
return NULL;
}
void Tree::insert(string word)
{
if (root != NULL)
{
insert (word, root);
}
else
{
root = new TreeNode;
root->data = word;
root->left= NULL;
root->right= NULL;
}
}
TreeNode *Tree::search(std::string word, int line)
{
return search (word, root,line);
}
void Tree::destroyTree()
{
destroyTree(root);
}
void Tree::inorder()
{
inorder(root);
}
void Tree::inorder(TreeNode *leaf)
{
if (leaf != NULL) {
inorder(leaf->left);
visit(leaf);
inorder(leaf->right);
}
}
void Tree::visit (TreeNode *leaf)
{
cout<<leaf->data<<endl;
cout<<leaf->lineNumber<<endl;
}
void Tree::linkList(TreeNode *leaf,string word, int line)
{
TreeNode *temp;
leaf = new TreeNode;
leaf->next = NULL;
if (leaf->lineNumber == NULL)
{
leaf->lineNumber = line;
}
else
{
leaf->lineNumber = line;
leaf->next = NULL;
}
}
First off, what's with the green color? And use [code] tags for code, please.
You mean the 'line number' the word was found on?
Point to 'the' line number? Or just contain the number?
Why do you have
•
•
•
•
I am having problems with the following program. We are to create a biary tree that takes words form a text file and then create a link list the the number the word was found on.
You mean the 'line number' the word was found on?
•
•
•
•
I have the tree created but I am unsure how to incorporate the link list in to the program. I need the node of the word to point to the line number. If any one can help it would be greatly appreciated. Thanks!
Point to 'the' line number? Or just contain the number?
Why do you have
#include <list> if you don't actually use it? Last edited by Rashakil Fol : Jun 12th, 2007 at 10:04 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- Need help with binary tree program. (C)
- Coding a Complete Binary Tree (C)
- how to reverse link list using recurtion (C)
- Using a class to add/delete/show numbers in a Link List (C++)
- C++ complete binary tree using an array. Unexpected end file (C++)
- coding a complete binary tree with Dev-C++ (C++)
- circular link list (C)
Other Threads in the C++ Forum
- Previous Thread: why link error
- Next Thread: Program for Random numbers having a bit of trouble.



Linear Mode