-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTNode.h
180 lines (168 loc) · 4.26 KB
/
BTNode.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
#ifndef BTNODE_H
#define BTNODE_H
/** \file BTNode.h
\brief Contains class for a Node to be used for a binary tree.
\author René H. Thomsen
\date 30/03 -10
*/
// License: GPL
#include <iostream>
#include <cstdlib>
/** \brief A Node for a binary tree.
*/
template<typename Item>
class BTNode
{
// Fields
private:
Item data;
BTNode* left, *right;
public:
// Functions
// -Constructors and deconstructors
/** \brief Default constructor.
\pre NULL for BTNode arguments if no children.
\post Constructs a Binary Node with the children specified.
*/
BTNode(BTNode* argLeft, BTNode* argRight, Item argData)
{
// Set data
setLeft(argLeft);
setRight(argRight);
setData(argData);
}
/** \brief Destructor.
\pre None.
\post Cleans up.
*/
~BTNode()
{
// Cleaning
}
// -Get and set
/** \brief Returns the data that the node holds.
\pre None.
\post Returns a copy of the data the Node holds.
*/
Item getData()
{
return(data);
}
/** \brief Sets the data that the node holds.
\pre None.
\post Changes the data the Node holds.
*/
void setData(Item argData)
{
data = argData;
}
/** \brief Returns left child.
\pre None.
\post Returns the address of the left child. (NULL if none)
*/
BTNode* getLeft()
{
return(left);
}
/** \brief Sets left child.
\pre 'argLeft' is the adress of a BTNode or NULL.
\post The Nodes left child is set to the address of 'argLeft'.
*/
void setLeft(BTNode* argLeft)
{
left = argLeft;
}
/** \brief Returns right child.
\pre None.
\post Returns the address of the right child. (NULL if none)
*/
BTNode* getRight()
{
return(right);
}
/** \brief Sets right child.
\pre 'argRight' is the adress of a BTNode or NULL.
\post The Nodes rihgt child is set to the address of 'argRight'.
*/
void setRight(BTNode* argRight)
{
right = argRight;
}
};
// Extra functions
/** \brief Returns the sum of all the nodes.
\pre The type of 'Item' should be able to added together with their own type.
\post Returns the sum of the data all the nodes in the tree holds.
*/
template<typename Item>
unsigned int sum(BTNode<Item>* root)
{
// Base case: Not a node
if(root == NULL)
return(0);
// Recursive case: Returns sum of 'root' and subtree sum of 'left' and 'right'.
return(sum(root->getLeft()) + sum(root->getRight()) + 1);
}
/** \brief Prints the tree´s nodes.
\pre 'depth' should be 0 at first call for proper output.
\post The tree is printed like seen laying down on it´s left side.
*/
template<typename Item>
void printTree(BTNode<Item>* root, unsigned int depth = 0)
{
// BC
if(root == NULL)
return;
// RC
printTree(root->getRight(), depth + 1);
for(unsigned int i = 0; i < depth; i++)
cout << " ";
cout << root->getData() << '\n';
printTree(root->getLeft(), depth + 1);
}
// -Helper function
template<typename Item>
void insertItem(BTNode<Item>* root, Item data)
{
// Find location
BTNode<Item>* cursor = root;
while(cursor != NULL)
{
if(cursor->getData() < data)
{
if(cursor->getRight() == NULL)
{
cursor->setRight(new BTNode<Item>(NULL, NULL, data));
return;
}
else
cursor = cursor->getRight();
}
else
{
if(cursor->getLeft() == NULL)
{
cursor->setLeft(new BTNode<Item>(NULL, NULL, data));
return;
}
else
cursor = cursor->getLeft();
}
}
return; // Nothing inserted
}
/** \brief Generates a binary tree from an array.
\pre 'root' is not already pointing to other data.(Except NULL) 'array' should be atleast the size of 'size'.
\post A tree is generated from the array, with a total number of nodes equal to 'size'.
\warning dynamic memory should be released later.
*/
// The function
template<typename Item>
void generateBST(BTNode<Item>* root, Item* array, size_t size)
{
for(size_t i = 0; i < size; i++)
{
insertItem(root, array[i]);
}
}
#endif