Sentence Tree Program Issues
Mar 30, 2011 at 9:47pm UTC
hey everyone, I'm trying to write a program to get a sentence from input, create a tree of the sentence in alphabetical order moving left to right, output the tree in alphabetical order, prompt for a word to remove from the sentence, remove the word from the tree, and print the tree alphabetically with the word removed. Right now the program is crashing at the input function. My main.cpp code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include<iostream>
#include<string>
#include"TreeNode.h"
using namespace std;
template <typename T>void Insert(TreeNode<T>*& root, const T& data);
template <typename T>void Delete(TreeNode<T>*& root, const T& value);
template <typename T> void Print(TreeNode<T>* root);
void main()
{
TreeNode<string>* treeRoot = NULL;
string s;
string deleteWord;
cout << "Please enter a sentence: " << endl;
while (cin.peek()!= '\n' )
{
cin >> s;
Insert<string>(treeRoot, s);
}
Print(treeRoot);
cout << endl << "Please enter a word to delete: " << endl;
cin >> deleteWord;
Delete(treeRoot, deleteWord);
Print(treeRoot);
system("pause" );
}
template <typename T>void Insert(TreeNode<T>*& root, const T& data)
{
if (root == NULL)
root->Value = data;
else if (root->Value == data)
return ;
else if (root->Value > data)
Insert(root->Left, data);
else
Insert(root->Right, data);
}
template <typename T>void Delete(TreeNode<T>*& root, const T& value)
{
if (root == NULL)
return ;
else
{
if (root->Value > value)
Delete(root->Left, value);
else
{
if (root->Value < value)
Delete(root->Right, value);
else
{
if (root->IsLeaf())
{
delete root;
root = NULL;
}
else
{
if (root->Left == NULL)
{
TreeNode<T>* temp = root;
delete root;
root = root->Right;
}
else
{
if (root->Right == NULL)
{
TreeNode<T>* temp = root;
delete root;
root = root->Left;
}
else
{
TreeNode<T>* newRoot = root->Left;
TreeNode<T>* rmn = NULL;
while (newRoot->Right != NULL)
{
newRoot = newRoot->Right;
rmn = newRoot;
}
root->Value = rmn->Value;
Delete(root->Left, rmn->Value);
delete rmn;
}
}
}
}
}
}
}
template <typename T> void Print(TreeNode<T>* root)
{
if (root->IsLeaf())
cout << root->Value << " " ;
Print(root->Left);
cout << root->Value << " " ;
Print(root->Right);
}
and TreeNode.h is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#pragma once
template <typename T> struct TreeNode
{
TreeNode(const T& value, TreeNode<T>* left = NULL, TreeNode<T>* right = NULL)
{
Value = value;
Left = left;
Right = right;
}
T Value;
TreeNode<T>* Left;
TreeNode<T>* Right;
bool IsLeaf() const
{
return Left == NULL && Right == NULL;
}
};
Mar 30, 2011 at 10:49pm UTC
Lines 34 & 35 are not right.
Mar 30, 2011 at 11:25pm UTC
mind explaining a bit?
EDIT: seem to have fixed it(i think)
1 2
if (root == NULL)
root = new TreeNode<string>(data);
havin problems with the IsLeaf() function in the TreeNode header now though, not sure why
Last edited on Mar 31, 2011 at 12:30am UTC
Topic archived. No new replies allowed.