-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqtree-given.cpp
More file actions
86 lines (77 loc) · 2.09 KB
/
Copy pathqtree-given.cpp
File metadata and controls
86 lines (77 loc) · 2.09 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
/**
* @file qtree-given.cpp
* @description partial implementation of QTree class used for storing image data
* CPSC 221 PA3
*
* THIS FILE WILL NOT BE SUBMITTED
*/
#include "qtree.h"
/**
* Node constructor.
* Assigns appropriate values to all attributes.
*/
Node::Node(pair<unsigned int, unsigned int> ul, pair<unsigned int, unsigned int> lr, RGBAPixel a) {
upLeft = ul;
lowRight = lr;
avg = a;
NW = nullptr;
NE = nullptr;
SW = nullptr;
SE = nullptr;
}
/**
* QTree destructor.
* Destroys all of the memory associated with the
* current QTree. This function should ensure that
* memory does not leak on destruction of a QTree.
*/
QTree::~QTree() {
Clear();
}
/**
* Copy constructor for a QTree. GIVEN
* Since QTrees allocate dynamic memory (i.e., they use "new", we
* must define the Big Three). This depends on your implementation
* of the copy funtion.
*
* @param other The QTree we are copying.
*/
QTree::QTree(const QTree& other) {
Copy(other);
}
/**
* Counts the number of nodes in the tree
*/
unsigned int QTree::CountNodes() const {
return CountNodes(root);
}
/**
* Counts the number of leaves in the tree
*/
unsigned int QTree::CountLeaves() const {
return CountLeaves(root);
}
/**
* Private helper function for counting the total number of nodes in the tree. GIVEN
* @param nd the root of the subtree whose nodes we want to count
*/
unsigned int QTree::CountNodes(Node* nd) const {
if (nd == nullptr)
return 0;
else
return 1 + CountNodes(nd->NW) + CountNodes(nd->NE) + CountNodes(nd->SW) + CountNodes(nd->SE);
}
/**
* Private helper function for counting the number of leaves in the tree. GIVEN
* @param nd the root of the subtree whose leaves we want to count
*/
unsigned int QTree::CountLeaves(Node* nd) const {
if (nd == nullptr)
return 0;
else {
if (nd->NW == nullptr && nd->NE == nullptr && nd->SW == nullptr && nd->SE == nullptr)
return 1;
else
return CountLeaves(nd->NW) + CountLeaves(nd->NE) + CountLeaves(nd->SW) + CountLeaves(nd->SE);
}
}