I'm sorry i cannot do this problem

I want to create a tree like this:

0. level = A (the root)

1.level = B( left of A) C ( right of A)

2.level = D( left of B) E ( right of B) F( left of C) G ( right of C)

3.level = H( left of D) I ( right of D) J( left of E) K ( right of E)
L( left of F)

I think there is a problem in AddNode(string info, Tree_Node* leaf). And ı do not know how to fix it. I would appreciate if someone can help, please help!

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
  #include <iostream>

using namespace std;

// Tree_Node
struct Tree_Node
{

    string data;
    Tree_Node* left;
    Tree_Node* right;

    Tree_Node()
    {

        data = "\0";
        left=NULL;
        right=NULL;

    }

};

// Tree class
class Tree
{

private:

     Tree_Node* root;
     void AddNode( string, Tree_Node* );
     void freeNode( Tree_Node* );

public:

    Tree()                              // Constructor
    {

        root = NULL;

    }
    ~Tree()                             // Destructor
    {

        freeNode(root);

    }

    Tree_Node* Root() { return root; };
    void addNode( string );
    void levelOrder( Tree_Node* );
    void Inorder( Tree_Node* );
    void Postorder( Tree_Node* );
    void Preorder( Tree_Node* );

};


// Free the node
void Tree :: freeNode(Tree_Node* leaf)
{

    if ( leaf != NULL )
    {

       freeNode(leaf->left);
       freeNode(leaf->right);

       delete leaf;

    }
}

// Add a node
void Tree :: addNode(string aInfo)
{
     // No elements. Add the root
     if ( root == NULL )
     {

        Tree_Node* n = new Tree_Node();
        n->data = aInfo;
        root = n;

     }
     else
     {

        AddNode(aInfo, root);

     }

}


void Tree :: AddNode(string info, Tree_Node* leaf)
{

    

    if ( leaf != NULL )
    {

         if(leaf->left == NULL)
        {
            cout << "left e eklendi" << endl;
            Tree_Node* n = new Tree_Node();
            n->data = info;
            leaf->left = n;
            return;
        }

        if(leaf->right == NULL)
        {
            cout << "right e eklendi" << endl;
            Tree_Node* n = new Tree_Node();
            n->data = info;
            leaf->right = n;
            return;
        }

        AddNode(info, leaf->left);

        leaf = leaf->left;

        AddNode(info, leaf->right);



    }

}

void Tree :: Preorder(Tree_Node* Root)
{

    if ( Root != NULL )
    {

        cout << Root->data << " ";
        Preorder(Root->left);
        Preorder(Root->right);

    }

}

void Tree :: Inorder(Tree_Node* Root)
{
    if(Root != NULL)
    {

        Inorder(Root->left);
        cout << Root->data << endl;
        Inorder(Root->right);

    }
}

void Tree :: Postorder(Tree_Node* Root)
{

    if(Root != NULL)
    {

        Postorder(Root->left);
        Postorder(Root->right);
        cout << Root->data << endl;

    }

}

// Test main program
int main() {
   Tree* tree = new Tree();
   tree->addNode("A");
    tree->addNode("B");
    tree->addNode("C");
    tree->addNode("D");
    tree->addNode("E");
    tree->addNode("F");
    tree->addNode("G");
    tree->addNode("H");
    tree->addNode("I");
    tree->addNode("J");
    tree->addNode("K");
    tree->addNode("L");


   cout << "Pre order traversal" << endl;
   tree->Preorder(tree->Root());
   cout << endl;


   cout << "In order traversal" << endl;
    tree->Inorder(tree->Root());
    cout << endl;

   cout << "Post order traversal" << endl;
    tree->Postorder(tree->Root());
    cout << endl;

   delete tree;
   return 0;
}
closed account (j3Rz8vqX)
What is it not doing? or rather, what is it doing that you do not like?
it isn't creating the tree like this:

0. level = A (the root)

1.level = B( left of A) C ( right of A)

2.level = D( left of B) E ( right of B) F( left of C) G ( right of C)

3.level = H( left of D) I ( right of D) J( left of E) K ( right of E)
L( left of F)
closed account (j3Rz8vqX)
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
void Tree :: AddNode(string info, Tree_Node* leaf)
{

    

    if ( leaf != NULL )
    {

         if(leaf->left == NULL)
        {
            cout << "left e eklendi" << endl;
            Tree_Node* n = new Tree_Node();
            n->data = info;
            leaf->left = n;
            return;
        }

        if(leaf->right == NULL)
        {
            cout << "right e eklendi" << endl;
            Tree_Node* n = new Tree_Node();
            n->data = info;
            leaf->right = n;
            return;
        }
        //Assuming both left and right were full...

        AddNode(info, leaf->left);//Using recursion with checking/inserting left nodes data.

        leaf = leaf->left;//Assign the current leaf its left value?

        AddNode(info, leaf->right);//Do it again!! huh? Check left->right



    }

}


So if Tree A had data on both its left and right, it would go the the left node and add data to its left or right. Then return and add data to the left nodes right node!

That may have been your inconsistent solution.

Ideally you should compare the data with the data within your object before pushing it left or right.

Design a algorithm to handle which function it should go to; not both.

Hopefully this helps you.
Last edited on
I'm sorry, this cannot help me, ı cannot solve the problem? Another advice?
Last edited on
Topic archived. No new replies allowed.