-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriend_class.cpp
More file actions
189 lines (149 loc) · 3.38 KB
/
friend_class.cpp
File metadata and controls
189 lines (149 loc) · 3.38 KB
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//Use const instead of macros
const int NumElements = 27;
const int dataLimit = 97;
// In future I can add a parent pointer for optimization without affecting the users of BinarySearchTree
class Node
{
private:
int data;
int key;
Node *left;
Node *right;
Node ():left(NULL), right (NULL), data(0), key(0)
{
}
Node (int ckey, int cdata):left(NULL), right (NULL), data(cdata), key(ckey)
{
}
Node (Node &cnode)
{
data = cnode.data;
key = cnode.key;
}
bool isLeaf ()
{
if (left == NULL && right == NULL)
return true;
else
return false;
}
friend class BinaryTree; // class BinaryTree can now access data directly
};
class BinaryTree
{
private:
Node *root;
Node * find_suitable_parent (Node *parent, int key);
Node * find_node (Node *node, Node **parent, int key);
void in_order_traverse (Node *node, ostream &out);
public:
BinaryTree ():root(NULL)
{
}
bool get_data (int key, int *data);
void insert (int key, int data);
~BinaryTree ();
friend ostream & operator<< (ostream &, BinaryTree *tree);
BinaryTree operator+ (int key);
};
Node * BinaryTree::find_suitable_parent (Node *parent, int key)
{
if (key < parent->key)
{
if (parent->left == NULL)
return parent;
else
return find_suitable_parent (parent->left, key);
}
if (key >= parent->key)
{
if (parent->right == NULL)
return parent;
else
return find_suitable_parent (parent->right, key);
}
}
void BinaryTree::insert (int key, int data)
{
if (root == NULL)
{
root = new Node (key, data);
return;
}
Node *parent = find_suitable_parent (root, key);
if (key >= parent->key)
parent->right = new Node (key, data);
else
parent->left = new Node (key, data);
}
Node * BinaryTree::find_node (Node *node, Node **parent, int key)
{
if (node->key == key)
return node;
else if (node->isLeaf ())
return NULL;
*parent = node;
if (node->left && key < node->key)
return find_node (node->left, parent, key);
if (node->right && key >= node->key)
return find_node (node->right, parent, key);
return NULL;
}
bool BinaryTree::get_data (int key, int *data)
{
Node *parent = NULL;
Node *node = find_node (root, &parent, key);
if (node)
{
*data = node->data;
return true;
}
return false;
}
BinaryTree::~BinaryTree ()
{
}
BinaryTree BinaryTree::operator+ (int key)
{
insert(key, 1234);
return *this;
}
void BinaryTree::in_order_traverse (Node *node, ostream &out)
{
if (node && node->left)
in_order_traverse (node->left, out);
out << node->key << " ";
if (node && node->right)
in_order_traverse (node->right, out);
}
//
ostream & operator<< (ostream &out, BinaryTree *tree)
{
out << "Printing Binary Tree InOrder \n";
tree->in_order_traverse (tree->root, out);
out << "\n\n";
}
int main ()
{
BinaryTree *tree = new BinaryTree ();
int a[NumElements];
// Initialize random seed
srand(time(NULL));
for (int i = 0; i < NumElements; i++)
a[i] = 0;
for (int i = 1; i < NumElements; i++) {
a[i] = rand () % dataLimit;
tree->insert (a[i], 799);
}
//Tree with data inserted
cout << tree;
// Inserting an element using a fancy overloaded operator
cout << "Inserting 799 using our fancy overloaded operator for fun ;) \n" << endl;
(*tree) + 799;
cout << tree;
std::cout << "Demonstrating friend class and friend functions" << std::endl;
}