-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRooted_Tree.cpp
105 lines (71 loc) · 2.2 KB
/
Rooted_Tree.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
//
// Created by Shubi & Eyal on 21/12/2019.
//
#include "Rooted_Tree.h"
#include "Queue.h"
void Rooted_Tree::Print_By_Layer(std::ostream &stream) const {
if(this->_root==NULL){
return;
}
// Queue declared as (tree_node, tree_node) to deal with template
// only the second tree_node is used!
// The first is just a placeholder with NULL.
Queue<Tree_Node*, Tree_Node*> Q = Queue<Tree_Node*, Tree_Node*>();
Q.Enqueue(NULL, this->_root);
unsigned max_distance = ZERO;
Tree_Node* u = NULL;
Tree_Node* v = NULL;
stream << this->_root->getKey();
while (!Q.is_empty()){
QueueItem<Tree_Node*, Tree_Node*>* queue_item = Q.Dequeue();
u = queue_item->getTreeNode();
delete queue_item;
v = u->getLeftChild();
while (v != NULL) {
if (max_distance < v->getDistance()) {
max_distance = v->getDistance();
stream << "\n";
}
else{
stream << ",";
}
stream << v->getKey();
Q.Enqueue(NULL, v);
v = v->getRightSibling();
}
}
}
void Rooted_Tree::Preorder_Print(std::ostream &stream) const {
unsigned from = PARENT_SIBLING;
Tree_Node* x = _root;
unsigned node_number = ZERO;
while(x != NULL){
if(from == PARENT_SIBLING) {
stream << x->getKey();
node_number++;
if(node_number != _number_of_nodes_in_tree){
stream <<",";
}
if (x->getLeftChild() != NULL) {
x = x->getLeftChild();
} else {
if (x->getRightSibling() != NULL) {
x = x->getRightSibling();
} else {
from = CHILD;
x = x->getParent();
}
}
} else{
if(x->getRightSibling()!= NULL){
from = PARENT_SIBLING;
x = x->getRightSibling();
} else{
x = x->getParent();
}
}
}
}
void Rooted_Tree::setNumberOfNodesInTree(unsigned int numberOfNodesInTree) {
_number_of_nodes_in_tree = numberOfNodesInTree;
}