-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeTemplate.cpp
More file actions
157 lines (128 loc) · 3.82 KB
/
BinaryTreeTemplate.cpp
File metadata and controls
157 lines (128 loc) · 3.82 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
// BinaryTreeTemplate.cpp : This file created by Anthony Huerta
//**********************************************************
// Both parts of hte assignment are completed in this file
//***********************************************************.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
// Strcutre of the BST is created (very similar to the design used for the contact BST)
template<class T>
struct NodeTemplate
{
T data;
NodeTemplate<T>* left;
NodeTemplate<T>* right;
NodeTemplate<T>(T value)
{
data = value;
left = NULL;
right = NULL;
}
};
// Initialization of BSTtemplate class which holds all needed member functions
template<class T>
class BSTtemplate
{
private:
NodeTemplate<T>* root;
int counter = 0;
//private functions that handle insertion and displaying because a pointer perameter is needed to properly handle recursion
NodeTemplate<T>* insertPrivate(T i, NodeTemplate<T>* p);
void displayPrivate(NodeTemplate<T>* p);
public:
//Constructor
BSTtemplate();
void insert(T i);
void display();
int nodeCounter();
};
template<class T>
BSTtemplate<T>::BSTtemplate() {
root = NULL;
}
template<class T>
void BSTtemplate<T>::insert(T value)
{
counter++;
root = insertPrivate(value, root);
}
template<class T>
void BSTtemplate<T>::display()
{
displayPrivate(root);
}
//nodeCounter completes the 2nd part of the assignment, it keeps track of the number of nodes within the tree
template<class T>
int BSTtemplate<T>::nodeCounter()
{
return counter;
}
//insertPrivate inputs all the values the user gives based on where they fit into the tree
template<class T>
NodeTemplate<T>* BSTtemplate<T>::insertPrivate(T i, NodeTemplate<T>* rootptr)
{
if (rootptr == NULL)
{
rootptr = new NodeTemplate<T>(i);
}
else if (i <= rootptr->data)
{
rootptr->left = insertPrivate(i, rootptr->left);
}
else if (i > rootptr->data)
{
rootptr->right = insertPrivate(i, rootptr->right);
}
return rootptr;
}
//displayPrivate prints out all the values in order of least to greatest
template<class T>
void BSTtemplate<T>::displayPrivate(NodeTemplate<T>* rootptr)
{
if (rootptr != NULL)
{
displayPrivate(rootptr->left);
cout << rootptr->data << endl;
displayPrivate(rootptr->right);
}
}
int main()
{
cout << "Welcome to the Binary Search Tree Template Program!" << endl;
cout << "===================================================" << endl << endl;
cout << "First, a tree will be created with the int data type. . ." << endl << endl;
// BST with int data type
BSTtemplate<int> intTree;
intTree.insert(20);
intTree.insert(25);
intTree.insert(5);
intTree.insert(7);
intTree.insert(3);
intTree.display();
cout << "The total number of nodes: " << intTree.nodeCounter() << endl;
cout << endl << "Second, a tree will be created with the string data type. . ." << endl << endl;
// BST with string data type
BSTtemplate<string> stringTree;
stringTree.insert("Hello");
stringTree.insert("How");
stringTree.insert("are");
stringTree.insert("you");
stringTree.insert("today");
stringTree.insert("?");
stringTree.display();
cout << "The total number of nodes: " << stringTree.nodeCounter() << endl;
cout << endl << "Lastly, a tree will be created with the double data type. . ." << endl << endl;
// BST with double data type
BSTtemplate<double> doubleTree;
doubleTree.insert(9.8);
doubleTree.insert(4.7);
doubleTree.insert(99.2);
doubleTree.insert(2.344);
doubleTree.insert(3.14);
doubleTree.insert(5.66);
doubleTree.insert(22.4);
doubleTree.display();
cout << "The total number of nodes: " << doubleTree.nodeCounter() << endl;
cout << endl << "Thank you for using the Binary Search Tree Template Program!" << endl << endl;
}