-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactTree.cpp
More file actions
46 lines (41 loc) · 1.2 KB
/
ContactTree.cpp
File metadata and controls
46 lines (41 loc) · 1.2 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
#include <iostream>
#include <string>
#include "ContactTree.h"
using namespace std;
ContactTree::ContactTree() {
root = NULL;
}
void ContactTree::display() {
displayPrivate(root);
}
void ContactTree::insert(long long phone, string mail) {
root = insertPrivate(root, phone, mail);
}
ContactTree::ContactNode* ContactTree::insertPrivate(ContactNode* rootptr, long long phone, string mail) {
//if Root is empty, user input is assigned directly to it
if (rootptr == NULL) {
rootptr = new ContactNode;
rootptr->phoneNum = phone;
rootptr->email = mail;
rootptr->left = NULL;
rootptr->right = NULL;
}
else if (rootptr->phoneNum > phone) {
rootptr->left = insertPrivate(rootptr->left, phone, mail);
}
else if(rootptr->phoneNum < phone)
{
rootptr->right = insertPrivate(rootptr->right, phone, mail);
}
return rootptr;
}
//displayPrivate prints out all the values in the ContactTree in order from smallest phone number to largest phone number
void ContactTree::displayPrivate(ContactNode* rootptr) {
if (rootptr == NULL)
{
return;
}
displayPrivate(rootptr->left);
cout << rootptr->phoneNum << " " << rootptr->email << endl;
displayPrivate(rootptr->right);
}