forked from mooman/Raymond-Tree-Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RaymondTree.cpp
119 lines (101 loc) · 2.4 KB
/
RaymondTree.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
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
#include "main.h"
#include "queue.h"
#include "event.h"
#include "messenger.h"
#include "RaymondTree.h"
#include "simulator.h"
#include "site.h"
using namespace std;
// creates a new empty tree
RaymondTree::RaymondTree(Simulator * s, Messenger * m)
{
this->s = s;
this->m = m;
root_site = NULL;
tree_size = 0;
postorder_q = new Queue();
}
// destroy the tree
RaymondTree::~RaymondTree()
{
if (root_site != NULL)
delete(root_site);
}
// build a tree of given size
void RaymondTree::build_tree(int size){
int random_number = 0;
// ensure the size of the network entered by user is valid
if (size <= 0) {
cout << "The size of the network has to be 1 or greater" << endl;
return;
}
// generate random IDs for each site based on a seed provided by
// the system time
for (int i = 0; i < size; i++) {
random_number = rand() % 50;
// cout << "DEBUG: node id: " << random_number << endl;
root_site = insert_node(root_site, random_number, NULL);
}
// debug I/O
print_tree(root_site);
cout << "tree size: " << tree_size-1 << endl;
}
// do an insertion into the tree creating
// something similar to a binary tree
Site* RaymondTree::insert_node(Site* node, int data, Site* p_node){
// if it's the first time, input site will
// be the root site
if (node == NULL) {
node = new Site(this, this->s, this->m);
node->id = data;
node->left = NULL;
node->right = NULL;
if (tree_size == 0) {// root node
tree_size += 1;
node->parent = node; // make it it's own parent
}
node->parent = p_node; // point to the parent site
tree_size++;
return node;
}
// recursive insert
if (data < node->id ) {
node->left = insert_node(node->left, data, node);
}
else {
node->right = insert_node(node->right, data, node);
}
return node;
}
// return the root site
Site* RaymondTree::get_root(){
return root_site;
}
// set the root site
void RaymondTree::set_root(Site* s) {
root_site = s;
}
// print the network
void RaymondTree::print_tree(Site* node) {
// post order traversal
if (node->left != NULL) {
print_tree(node->left);
}
if (node->right != NULL) {
print_tree(node->right);
}
cout << node->id << " ";
return;
}
// construct a post order queue
void RaymondTree::traverse(Site* node) {
// post order traversal
if (node->left != NULL) {
traverse(node->left);
}
if (node->right != NULL) {
traverse(node->right);
}
postorder_q->enqueue(node);
return;
}