-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.cpp
94 lines (82 loc) · 1.35 KB
/
bst.cpp
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
//Arafat Iqbal
//File for BST functions
//Stores all functions from the BST.h File
#include "bst.h"
//Functions for Node Class
node::node():left(0),right(0)
{
}
node::node(info * p):left(0),right(0)
{
zoom * ptr1 = dynamic_cast<zoom*> (p);
project * ptr2 = dynamic_cast<project*> (p);
lecture * ptr3 = dynamic_cast<lecture*> (p);
if(ptr1) {data = new zoom(*ptr1); return;}
if(ptr2) {data = new project(*ptr2); return;}
if(ptr3) {data = new lecture(*ptr3); return;}
}
node::~node()
{
delete left;
left = NULL;
delete right;
right = NULL;
}
node *& node::go_left()
{
return left;
}
node *& node::go_right()
{
return right;
}
info *& node::get_data(){return data;}
//Functions for BST Class
bst::bst():root(0)
{
}
bst::~bst()
{
delete_all(root);
}
void bst::delete_all(node *& root)
{
if(!root) return;
delete_all(root->go_left());
delete_all(root->go_right());
delete root;
root = NULL;
return;
}
int bst::input(info * p)
{
return input(root,p);
}
int bst::input(node *& root,info * p)
{
if(!root)
{
root = new node(p);
}
else if(*p < *root->get_data())
{
input(root->go_left(),p);
}
else
{
input(root->go_right(),p);
}
return 1;
}
void bst::display()
{
return display(root);
}
void bst::display(node * root)
{
if(!root) return;
display(root->go_left());
cout << *(root->get_data()) << endl;
display(root->go_right());
return;
}