segmentation fault 11

Hi it compiles but gave me segmentation fault 11 and also doesn't print the letter as the tree, it printed as list. ?
plz help me guys.. thank you

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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "morse.h"
#include <string>
#include <iostream>
#include <iomanip>


MorseTree::MorseTree(istream& code_file)
{
    BTNode<char> * local_root=new BTNode<char>('*');
    int n;
    code_file>>n;
    BTNode<char>* tracker=new BTNode<char>;
    tracker=local_root;
    char letter;
    string code;
    code_file>>letter;
    while(!code_file.eof())
    {
        if(n!=0)
        {
            // BTNode<char>* copy_root=new BTNode<char>;
            code_file>>code;
            unsigned int i=0;
            while(i!=code.length())
            {
                if(code.at(i)=='.')
                {
                    if(tracker->left==NULL)
                    {
                        BTNode<char>* newLeft=new BTNode<char>;
                        tracker->left=newLeft;
                    }
                    tracker=tracker->left;
                }
                else if(code.at(i)=='-')
                {
                    if(tracker->right==NULL)
                    {
                        BTNode<char>* newRight=new BTNode<char>;
                        tracker->right=newRight;
                    }
                    tracker=tracker->right;
                }
                i++;
            }
            tracker->nodeValue=letter;
            local_root=tracker;
            cout<<local_root->nodeValue<<endl;
        }
        code_file>>letter;
        tracker=local_root;
        n--;
    }
}


MorseTree::MorseTree(const MorseTree& original)
{
    if(this!=&original)
    {
        copy_tree(original.source_root);
    }
    
}

BTNode<char>* MorseTree::copy_tree(const BTNode<char>* source_root)
{
    BTNode<char> *newLeft, *newRight, *copy_root;
    
    if(source_root==NULL)
        return NULL;
    
    newLeft=copy_tree(source_root->left);
    newRight=copy_tree(source_root->right);
    
    copy_root=new BTNode<char>(source_root->nodeValue, newLeft, newRight);
    
    return copy_root;
}

MorseTree::~MorseTree()
{
    delete_tree(source_root);
}

void MorseTree::delete_tree(const BTNode<char>* source_root)
{
    if(source_root!=NULL)
    {
        delete_tree(source_root->left);
        delete_tree(source_root->right);
        delete source_root;
    }
}

MorseTree MorseTree::operator=(const MorseTree& original)
{
    while(source_root!=NULL)
    {
        delete_tree(source_root);
    }
    if(&original!=this)
    {
        copy_tree(original.source_root);
    }
    return *this;
}

void MorseTree::encode(istream& message_file, ostream& out)
{
    char ch;
    int i=0;
    message_file.get(ch);
    while(!message_file.fail())
    {
        if(i!=0)
            out<<" ";
        string code="";
        if(ch=='\n')
            out<<"||";
        else if(ch==' ')
            out<<"|";
        else if(ch!=' ')
            code=char_to_code(ch, source_root, code);
        out<<code;
        message_file.get(ch);
        i++;
    }
    out<<endl;
}

string MorseTree::char_to_code(const char& ch, BTNode<char>* t, const string& path_so_far)
{
    string code;
    char dot='.';
    char dash='-';
    if(t!=NULL)
    {
        if(t->nodeValue==ch)
            return path_so_far;
        else
        {
            code=char_to_code(ch, t->right, path_so_far+dash);
            if(code=="")
            {
                code=char_to_code(ch, t->left, path_so_far+dot);
                return code;
            }
            else
                return code;
        }
    }
    
    else
        return "";
}

void MorseTree::decode(istream& message_file, ostream& out)
{
    BTNode<char>* trav=new BTNode<char>;
    trav=source_root;
    string code;
    bool flag=true;
    message_file>>code;
    while(!message_file.eof())
    {
        if(code.at(0)=='|')
        {
            flag=false;
            if(code.length()==2)
            {
                out<<endl;
            }
            else
                out<<" ";
        }
        else
        {
            flag=true;
            for(unsigned int i=0;i<code.length();i++)
            {
                if(code.at(i)=='.')
                    trav=trav->left;
                else if(code.at(i)=='-')
                    trav=trav->right;
                else
                    out<<" Invalid code"<<endl;
            }
        }
        if(flag)
            out<<trav->nodeValue;
        message_file>>code;
        trav=source_root;
    }
}



void MorseTree::write_tree(ostream& out)
{
    write_tree(out, source_root, 0);

}

void MorseTree::write_tree(ostream& out, BTNode<char>* local_root, const int& depth)
{
    BTNode<char>* trav=new BTNode<char>;
    trav=local_root;
    if(depth<=16)
        out<<endl;
    if((trav!=NULL))
    {
        write_tree(out, trav->right, depth+8);
        out<<setw(depth);
        out<<trav->nodeValue<<endl;
        write_tree(out, trav->left, depth+8);
    }
    if(depth<=16)
        out<<endl;
}









morse.h


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



#ifndef BTNode_CLASS
#define BTNode_CLASS
#include<string>
#include <iostream>
using namespace std;

template <typename T>
class BTNode
{
public:
    T nodeValue;
    BTNode<T> *left, *right;
    
    BTNode()
    {}
    
    BTNode (const T& item, BTNode<T> *lptr=NULL, BTNode<T> *rptr=NULL):
    nodeValue(item), left(lptr), right(rptr)
    {}
};
#endif //BTNode_CLASS

#ifndef MORSETREE_CLASS
#define MORSETREE_CLASS
#include <iostream>
#include <cstdlib>
#include <string>

class MorseTree
{
    
public:
    MorseTree(istream& code_file);
    MorseTree(const MorseTree& source);
    ~MorseTree();
    MorseTree operator=(const MorseTree& source);
    void write_tree(ostream& out);
    void encode(istream& message_file, ostream& out);
    void decode(istream& message_file, ostream& out);

private:
    BTNode<char>* copy_tree(const BTNode<char>* source_root);
    void delete_tree(const BTNode<char>* source_root);
    void write_tree(ostream& out, BTNode<char>* local_root, const int& depth);
    string char_to_code(const char& ch, BTNode<char>* t, const string& path_so_far);
    BTNode<char>*source_root;
};
#endif 
Last edited on
Topic archived. No new replies allowed.