-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.cpp
More file actions
122 lines (110 loc) · 2.81 KB
/
tree.cpp
File metadata and controls
122 lines (110 loc) · 2.81 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
#include "tree.h"
tree::tree()
{
}
tree::~tree()
{
}
// belom selesai
void tree::insert(string data, string umur, string alamat)
{
// cout << "insert";
if(diisi==0)
{
addNode(data, umur, alamat);
}
else if(cek(data,here->nama) == 1)
if(kiri==0)
{
left = new tree;
kiri = 1;
left->insert(data, umur, alamat);
}
else
{
left->insert(data, umur, alamat);
}
else if(cek(data,here->nama) == 0)
if(kanan==0)
{
right = new tree;
kanan = 1;
right->insert(data, umur, alamat);
}
else
{
right->insert(data, umur, alamat);
}
}
void tree::addNode(string n, string a, string b)
{
diisi = 1;
// cout << "addnode";
here = new node(n,a,b);
}
void tree::print(int maxNama, int maxUmur, int maxAlamat)
{
// cout << here->nama;
// getchar();
if(kiri == 1) left->print(maxNama, maxUmur, maxAlamat);
cout.setf(ios::left, ios::adjustfield);
cout << setw(maxNama) << here->nama << " " << setw(maxUmur) << here->umur << " " << setw(maxAlamat) << here->alamat << endl;
cout.unsetf(ios::adjustfield);
if(kanan == 1) right->print(maxNama, maxUmur, maxAlamat);
}
void tree::kop(int maxNama, int maxUmur, int maxAlamat)
{
int temp = maxNama + 1 + maxUmur + 1 + maxAlamat;
for (int i = 0; i < temp; i++)cout << '-';
cout << endl;
cout.setf(ios::left, ios::adjustfield);
cout << setw(maxNama) << "Nama" << " " << setw(maxUmur) << "Umur" << " " << setw(maxAlamat) << "Alamat" << endl;
cout.unsetf(ios::adjustfield);
for (int i = 0; i < temp; i++)cout << '-';
cout << endl;
}
void tree::cariUmur(string umur, int maxNama, int maxUmur, int maxAlamat)
{
if(kiri == 1) left->cariUmur(umur, maxNama, maxUmur, maxAlamat);
if(stoi(here->umur) > stoi(umur))
{
cout.setf(ios::left, ios::adjustfield);
cout << setw(maxNama) << here->nama << ' ' << setw(maxUmur) << here->umur << ' ' << setw(maxAlamat) << here->alamat << endl;
cout.unsetf(ios::adjustfield);
}
if(kanan == 1) right->cariUmur(umur, maxNama, maxUmur, maxAlamat);
}
void tree::cariAlamat(string alamat, int maxNama, int maxUmur, int maxAlamat)
{
if(kiri == 1) left->cariAlamat(alamat, maxNama, maxUmur, maxAlamat);
if(!strcmp(here->alamat.c_str(), alamat.c_str()))
{
cout.setf(ios::left, ios::adjustfield);
cout << setw(maxNama) << here->nama << ' ' << setw(maxUmur) << here->umur << ' ' << setw(maxAlamat) << here->alamat << endl;
cout.unsetf(ios::adjustfield);
}
if(kanan == 1) right->cariAlamat(alamat, maxNama, maxUmur, maxAlamat);
}
void tree::output()
{
if(kiri == 1) left->output();
ofstream file;
file.open("output.txt", ios_base::app);
file << here->nama << '\t' << here->umur << '\t' <<here->alamat << endl;
file.close();
if(kanan == 1) right->output();
}
void tree::hapustree()
{
if(diisi == 1) delete here;
if(kiri == 1)
{
left->hapustree();
delete left;
}
if(kanan == 1)
{
right->hapustree();
delete right;
}
}