Skip to content

Commit 24cff80

Browse files
Complete Code
1 parent 83958ef commit 24cff80

34 files changed

+549
-0
lines changed

src/.vs/Binary-Search-Tree/FileContentIndex/read.lock

Whitespace-only changes.

src/.vs/Binary-Search-Tree/v17/.suo

38 KB
Binary file not shown.
15.5 MB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

src/AccountsRecords.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#pragma once
2+
#include<string>
3+
#include<iostream>
4+
using namespace std;
5+
6+
class AccountsRecords {
7+
string firstName, lastName;
8+
double balance;
9+
int recordNo, accID;
10+
11+
public:
12+
AccountsRecords() : firstName(""), lastName(""), balance(0), recordNo(0), accID(0) {
13+
}
14+
15+
AccountsRecords(string fN, string lN, double b, int rN, int aID) : firstName(fN), lastName(lN), balance(b), recordNo(rN), accID(aID) {
16+
}
17+
18+
void setFirstName(string fN) {
19+
firstName = fN;
20+
}
21+
22+
void setLastName(string lN) {
23+
lastName = lN;
24+
}
25+
26+
void setBalance(const double& b) {
27+
balance = b;
28+
}
29+
30+
void setRecordNo(const int& rN) {
31+
recordNo = rN;
32+
}
33+
34+
void setAccountID(const int& aID) {
35+
accID = aID;
36+
}
37+
38+
string getFirstName() const {
39+
return firstName;
40+
}
41+
42+
string getLastName() const {
43+
return lastName;
44+
}
45+
46+
double getBalance() const {
47+
return balance;
48+
}
49+
50+
int getRecordNo() const {
51+
return recordNo;
52+
}
53+
54+
int getAccountID() const {
55+
return accID;
56+
}
57+
58+
void Display() {
59+
cout << "Record Number: " << recordNo << endl;
60+
cout << "Account ID: " << accID << endl;
61+
cout << "First Name: " << firstName << endl;
62+
cout << "Last Name: " << lastName << endl;
63+
cout << "Balance: " << balance << endl << endl;
64+
}
65+
66+
AccountsRecords operator=(const AccountsRecords& rhs) {
67+
accID = rhs.accID;
68+
firstName = rhs.firstName;
69+
lastName = rhs.lastName;
70+
balance = rhs.balance;
71+
recordNo = rhs.recordNo;
72+
73+
return *this;
74+
}
75+
76+
bool operator<(const AccountsRecords& rhs) const {
77+
if (this->accID < rhs.accID) {
78+
return true;
79+
}
80+
return false;
81+
}
82+
83+
bool operator>(const AccountsRecords& rhs) const {
84+
if (this->accID > rhs.accID) {
85+
return true;
86+
}
87+
return false;
88+
}
89+
90+
bool operator==(const AccountsRecords& rhs) const {
91+
if (this->accID == rhs.accID) {
92+
return true;
93+
}
94+
return false;
95+
}
96+
};
97+
98+
ostream& operator<<(ostream& output, AccountsRecords& rhs) {
99+
rhs.Display();
100+
return output;
101+
}

src/BSTree.h

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#pragma once
2+
#include<iostream>
3+
using namespace std;
4+
5+
template<class DT>
6+
class Node {
7+
public:
8+
DT data;
9+
Node* left;
10+
Node* right;
11+
12+
Node() :left(NULL), right(NULL) {
13+
}
14+
};
15+
16+
template<class DT>
17+
class BSTree {
18+
Node<DT>* root;
19+
20+
Node<DT>* traverseForInserting(Node<DT>* temp, const DT& newDataItem) {
21+
if (temp == NULL) {
22+
temp = new Node<DT>;
23+
temp->data = newDataItem;
24+
return temp;
25+
}
26+
27+
else if (newDataItem < temp->data) {
28+
temp->left = traverseForInserting(temp->left, newDataItem);
29+
}
30+
31+
else if (newDataItem > temp->data) {
32+
temp->right = traverseForInserting(temp->right, newDataItem);
33+
}
34+
35+
else {
36+
cout << "Duplicates are present" << endl;
37+
}
38+
return temp;
39+
}
40+
41+
bool traverseForSearching(Node<DT>* temp, const DT& searchDataItem) {
42+
if (temp == NULL) {
43+
return false;
44+
}
45+
46+
else if (searchDataItem == temp->data) {
47+
return true;
48+
}
49+
50+
else if (searchDataItem < temp->data) {
51+
return traverseForSearching(temp->left, searchDataItem);
52+
}
53+
54+
else if (searchDataItem > temp->data) {
55+
return traverseForSearching(temp->right, searchDataItem);
56+
}
57+
}
58+
59+
int getMax(const int& num1, const int& num2) {
60+
if (num1 == num2) {
61+
return num1;
62+
}
63+
64+
else if (num1 > num2) {
65+
return num1;
66+
}
67+
68+
return num2;
69+
}
70+
71+
int calculateHeight(Node<DT>* temp) {
72+
if (temp == NULL) {
73+
return -1;
74+
}
75+
int leftHeight = calculateHeight(temp->left);
76+
int rightHeight = calculateHeight(temp->right);
77+
78+
return getMax(leftHeight, rightHeight) + 1;
79+
}
80+
81+
void deleteNode(Node<DT>*& nodePtr) {
82+
Node<DT>* temp;
83+
84+
if (nodePtr->right == NULL) {
85+
temp = nodePtr;
86+
nodePtr = nodePtr->left;
87+
delete temp;
88+
}
89+
90+
else if (nodePtr->left == NULL) {
91+
temp = nodePtr;
92+
nodePtr = nodePtr->right;
93+
delete temp;
94+
}
95+
96+
else {
97+
temp = nodePtr->right;
98+
99+
while (temp->left != NULL) {
100+
temp = temp->left;
101+
}
102+
103+
nodePtr->data = temp->data;
104+
105+
Node<DT>* prev = nodePtr->right;
106+
107+
if (prev->left != NULL) {
108+
109+
while (prev->left != temp) {
110+
prev = prev->left;
111+
}
112+
113+
if (temp->right == NULL) {
114+
delete temp;
115+
prev->left = NULL;
116+
}
117+
118+
else {
119+
prev->left = temp->right;
120+
delete temp;
121+
}
122+
}
123+
124+
else {
125+
delete temp;
126+
nodePtr->right = NULL;
127+
}
128+
}
129+
}
130+
131+
bool traverseForRemove(Node<DT>*& temp, const DT& deleteDataItem) {
132+
if (temp == NULL) {
133+
return false;
134+
}
135+
136+
else if (temp->data == deleteDataItem) {
137+
deleteNode(temp);
138+
}
139+
140+
else if (deleteDataItem < temp->data) {
141+
return traverseForRemove(temp->left, deleteDataItem);
142+
}
143+
144+
else if (deleteDataItem > temp->data) {
145+
return traverseForRemove(temp->right, deleteDataItem);
146+
}
147+
148+
}
149+
150+
public:
151+
BSTree() : root(NULL) {
152+
}
153+
154+
Node<DT>* getRoot() {
155+
return root;
156+
}
157+
158+
void insert(const DT& newDataItem) {
159+
root = traverseForInserting(root, newDataItem);
160+
}
161+
162+
bool retrieve(const DT& searchDataItem) {
163+
return traverseForSearching(root, searchDataItem);
164+
}
165+
166+
bool isEmpty() {
167+
if (root == NULL) {
168+
return true;
169+
}
170+
return false;
171+
}
172+
173+
int getHeight() {
174+
return calculateHeight(root);
175+
}
176+
177+
bool remove(const DT& deleteDataItem) {
178+
return traverseForRemove(root, deleteDataItem);
179+
}
180+
181+
void inOrderDisplay(Node<DT>* temp) {
182+
if (temp != NULL) {
183+
inOrderDisplay(temp->left);
184+
cout << temp->data;
185+
inOrderDisplay(temp->right);
186+
}
187+
}
188+
189+
void preOrderDisplay(Node<DT>* temp) {
190+
if (temp != NULL) {
191+
cout << temp->data;
192+
preOrderDisplay(temp->left);
193+
preOrderDisplay(temp->right);
194+
}
195+
}
196+
197+
void postOrderDisplay(Node<DT>* temp) {
198+
if (temp != NULL) {
199+
postOrderDisplay(temp->left);
200+
postOrderDisplay(temp->right);
201+
cout << temp->data;
202+
}
203+
}
204+
205+
~BSTree() {
206+
while (root != NULL) {
207+
remove(root->data);
208+
}
209+
}
210+
};

src/Binary-Search-Tree.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32630.192
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Binary-Search-Tree", "Binary-Search-Tree.vcxproj", "{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Debug|x64.ActiveCfg = Debug|x64
17+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Debug|x64.Build.0 = Debug|x64
18+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Debug|x86.ActiveCfg = Debug|Win32
19+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Debug|x86.Build.0 = Debug|Win32
20+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Release|x64.ActiveCfg = Release|x64
21+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Release|x64.Build.0 = Release|x64
22+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Release|x86.ActiveCfg = Release|Win32
23+
{3C04C22A-A83F-49B2-BA0C-AC6FFBD492F1}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {405651D1-3C7B-44C9-9CE1-059DDCEE7D55}
30+
EndGlobalSection
31+
EndGlobal

0 commit comments

Comments
 (0)