-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemTree.cpp
More file actions
117 lines (101 loc) · 3.02 KB
/
ItemTree.cpp
File metadata and controls
117 lines (101 loc) · 3.02 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
#include "ItemTree.h"
// Default private constructor (for sentinel node)
ItemTree::ItemTree() : fKey(nullptr), fLeft(nullptr), fRight(nullptr), purchasable(false) {}
// Constructor initializes fKey and sets left/right to NIL
ItemTree::ItemTree(ShopItem* aKey) : fKey(aKey), fLeft(&NIL), fRight(&NIL), purchasable(false) {}
// Check if the node is empty
bool ItemTree::isEmpty() const {
return this == &NIL;
}
// Return the key, throws an exception if the node is empty
ShopItem* ItemTree::key() const {
if (isEmpty())
throw std::domain_error("Empty node!");
return fKey;
}
// Access methods for left child
ItemTree* ItemTree::left() {
if (isEmpty())
throw std::domain_error("Empty Item Tree");
return fLeft; // Return left child
}
// Access methods for right child
ItemTree* ItemTree::right() {
if (isEmpty())
throw std::domain_error("Empty Item Tree");
return fRight; // Return right child
}
// Attach left child
void ItemTree::attachLeft(ItemTree* aTree) {
if (isEmpty())
throw std::domain_error("Empty Item Tree");
if (fLeft != &NIL)
throw std::domain_error("Non-empty left subtree");
fLeft = aTree;
}
// Attach right child
void ItemTree::attachRight(ItemTree* aTree) {
if (isEmpty())
throw std::domain_error("Empty Item Tree");
if (fRight != &NIL)
throw std::domain_error("Non-empty right subtree");
fRight = aTree;
}
// Detach left child
ItemTree* ItemTree::detachLeft() {
if (isEmpty())
throw std::domain_error("Empty Item Tree");
ItemTree* temp = fLeft;
fLeft = &NIL; // Set left to NIL
return temp; // Return the detached node
}
// Detach right child
ItemTree* ItemTree::detachRight() {
if (isEmpty())
throw std::domain_error("Empty Item Tree");
ItemTree* temp = fRight;
fRight = &NIL; // Set right to NIL
return temp; // Return the detached node
}
// Method to display the item tree
void ItemTree::display(int depth) const {
if (!isEmpty()) {
std::cout << std::string(depth, ' ') << key()->getName() << " (Cost: " << key()->getCost() << ")\n";
if (fLeft && fLeft != &NIL) {
fLeft->display(depth + 2); // Display left child
}
if (fRight && fRight != &NIL) {
fRight->display(depth + 2); // Display right child
}
}
}
// Check if the item is purchasable
bool ItemTree::isPurchasable() const {
return purchasable;
}
// Set the item as purchasable
void ItemTree::setPurchasable(bool status) {
purchasable = status;
}
// Mark children as purchasable
void ItemTree::unlockChildren() {
if (fLeft != &NIL) {
fLeft->setPurchasable(true);
}
if (fRight != &NIL) {
fRight->setPurchasable(true);
}
}
// Destructor
ItemTree::~ItemTree() {
delete fKey; // Deleting the ShopItem
// Delete left and right children if they are not NIL
if (fLeft != &NIL) {
delete fLeft;
}
if (fRight != &NIL) {
delete fRight;
}
}
// Sentinel implementation for ItemTree
ItemTree ItemTree::NIL;