Help Printing templated AVL tree

i have no idea as to how i would write a print function for this program and i was hoping somebody could show me i will attach my TreeType.h and main below

TreeType.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
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
#ifndef AvlClass_h
#define AvlClass_h
#include <iostream>
using namespace std;
typedef enum {LH,EH,RH} Balfactor;

template <class ItemType>
struct TreeNode
{
    ItemType info;
    TreeNode *left;
    TreeNode *right;
    Balfactor bf;
};

template <class ItemType>
class TreeType
{
public:
    void InsertItem(ItemType item);
private:
    TreeNode <ItemType> * root;
};



template <class ItemType>
void TreeType<ItemType> :: InsertItem(ItemType item)
// Calls recursive function Insert to insert item into tree.
{
    bool taller=false;
    Insert (root, item, taller);
}

template <class ItemType>
void Insert (TreeNode<ItemType>*& tree, ItemType item, bool & taller)
// Inserts item into tree.
// Post:item is in tree; search property is maintained.
{
    if (tree == NULL)
    {    // Insertion place found.
        tree = new TreeNode<ItemType>;
        tree->left = NULL;
        tree->right = NULL;
        tree->info = item;
        tree->bf = EH;
        taller = true;
    }
    else if ( item == tree->info)
        cerr << "Duplicate key is not allowed in AVL tree." << endl;
    else if (item < tree->info)
    {
        Insert (tree->left, item, taller);
        // Insert into left subtree
        if (taller)
            switch (tree->bf)
        {
            case LH: LeftBalance(tree,taller);
                break;
            case EH: tree->bf = LH;
                break;
            case RH: tree->bf = EH;
                taller = false;
                break;
        }
    }
    else
    {
        Insert (tree->right, item, taller);
        // Insert into right subtree
        if (taller)
            switch (tree->bf)
        {
            case RH: RightBalance (tree,taller);
                break;
            case EH: tree->bf = RH;
                break;
            case LH: tree->bf = EH;
                taller = false;
                break;
        }
        
    }
}

template <class ItemType>
void RotateLeft (TreeNode<ItemType> * & tree)
{
    TreeNode<ItemType> * rs;
    if (tree == NULL)
        cerr << "It is impossible to rotate an empty tree in RotateLeft" << endl;
    else if (tree->right == NULL)
        cerr << "It is impossible to make an empty subtree the root in RotateLeft" << endl;
    else
    {
        rs = tree->right;
        tree->right = rs->left;
        rs->left = tree;
        tree = rs;
        
    }
}

template <class ItemType>
void RotateRight (TreeNode<ItemType> * & tree)
{
    TreeNode<ItemType> * ls;
    if (tree == NULL)
        cerr << "It is impossible to rotate an empty tree in RotateRight" << endl;
    else if (tree->left == NULL)
        cerr << "It is impossible to make an empty subtree the root in RotateRight" << std::endl;
    else
    {
        ls = tree->left;
        tree->left = ls->right;
        ls->right = tree;
        tree = ls;
        
    }
    
}

template <class ItemType>
void RightBalance (TreeNode<ItemType> *& tree, bool & taller)
{
    TreeNode<ItemType> * rs = tree->right;
    TreeNode<ItemType> * ls;
    switch (rs->bf)
    {
        case RH:
            tree->bf = rs->bf = EH;
            RotateLeft(tree);
            taller = false;
            break;
        case EH:
            std::cerr << "Tree already balanced " << std::endl;
            break;
        case LH:
            ls = rs->left;
            switch (ls->bf)
        {
            case RH:
                tree->bf = LH;
                rs->bf = EH;
                break;
            case EH:
                tree->bf = rs->bf = EH;
                break;
            case LH:
                tree->bf = EH;
                rs->bf = RH;
                break;
        }
            ls->bf = EH;
            RotateRight(tree->right);
            RotateLeft(tree);
            taller = false;
    }
}


template <class ItemType>
void LeftBalance (TreeNode<ItemType> *& tree, bool & taller)
 {
     TreeNode<ItemType> * ls = tree->left;
     TreeNode<ItemType> * rs;
     switch (ls->bf)
     {
         case LH:
             tree->bf = ls->bf = EH;
             RotateRight(tree);
             taller = false;
             break;
         case EH:
             cerr << "Tree already balanced " << endl;
             break;
         case RH:
             rs = ls->left;
             switch (rs->bf)
         {
             case LH:
                 tree->bf = RH;
                 ls->bf = EH;
                 break;
             case EH:
                 tree->bf = ls->bf = EH;
                 break;
             case RH:
                 tree->bf = EH;
                 ls->bf = LH;
                 break;
                 
         }
             rs->bf = EH;
             RotateLeft(tree->left);
             RotateRight(tree);
             taller = false;
     }
     
 }
#endif /* AvlClass_h */ 


main.cpp

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
#include <iostream>
#include "TreeType.h"
using namespace std;

TreeType<string>tree;

int main(int argc, const char * argv[]) {
    
    int option;
    do
    {
        cout << "\t1. Insert into an AVL Tree\n";
        cout << "\t2. Print the contents of the AVL tree\n";
        cout << "\t3. Exit\n";
        cout << "\t\tEnter option : ";
        cin >> option;
        
        switch (option)
        {
            case 1:
                //Insert
                tree.InsertItem("dean");
                tree.InsertItem("joe");
                tree.InsertItem("jason");
                break;
            case 2:
                //tree.PrintTree();
                break;
        }
        
    } while (option < 3);
    return 0;
}
Last edited on
You need to visit every node and output it. Recursion can be applied.

Something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void print(TreeNode* nodeToPrint)
{
  cout << nodeToPrint->whatever_you_want_to-print_out;

  if (nodeToPrint->has_a_left_child_node)
  {
    print(the_left_child_node);
  }

  if (nodeToPrint->has_a_right_child_node)
  {
    print(the_right_child_node);
  }
  
  return;
}

Last edited on
Topic archived. No new replies allowed.