-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.cpp
More file actions
145 lines (124 loc) · 3.67 KB
/
Copy pathBST.cpp
File metadata and controls
145 lines (124 loc) · 3.67 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "BST.h"
Node *BST::leaf() {
return nullptr;
}
bool BST::isLeaf(Node *n) {
return (n == nullptr);
}
Node::Node(BST::KeyType newKey, BST::ItemType newItem) {
this->key = newKey;
this->item = newItem;
this->leftChild = nullptr;
this->rightChild = nullptr;
};
BST::ItemType *BST::lookup(KeyType soughtKey) {
return lookupRec(soughtKey, root);
}
BST::ItemType *BST::lookupRec(KeyType soughtKey, Node *currentNode) {
if (isLeaf(currentNode)) {
return nullptr;
}
else {
if (soughtKey < currentNode->key) {
ItemType* foundItem = lookupRec(soughtKey, currentNode->leftChild);
return foundItem;
}
else if (soughtKey > currentNode->key) {
ItemType* foundItem = lookupRec(soughtKey, currentNode->rightChild);
return foundItem;
}
else {
ItemType* foundItem = ¤tNode->item;
return foundItem;
}
}
}
void BST::insert(KeyType newKey, ItemType item) {
insertRec(newKey, item, root);
}
void BST::insertRec(KeyType k, ItemType i, Node *¤t) {
if (isLeaf(current)) {
current = new Node(k, i);
}
else {
if (k < current->key) {
insertRec(k, i, current->leftChild);
}
else if (k > current->key) {
insertRec(k, i, current->rightChild);
}
else {
//Replace Item here
current->item = i;
}
}
}
void BST::displayEntries() {
displayEntriesRec(root);
}
void BST::displayEntriesRec(Node *currentNode) {
if (currentNode == nullptr) {
return;
}
else {
std::cout << currentNode->key << ", " << currentNode->item << std::endl;
displayEntriesRec(currentNode->leftChild);
displayEntriesRec(currentNode->rightChild);
}
//in-order traversal code
//post-order traversal code
}
void BST::displayTree() {
std::string tabs = std::string();
displayTreeRec(root, tabs);
}
void BST::displayTreeRec(Node *currentNode, std::string spaces) {
if (currentNode == nullptr) {
std::cout << spaces << " *" << std::endl;
return;
}
else {
spaces += " ";
std::cout << spaces << currentNode->key << ", " << currentNode->item << std::endl;
displayTreeRec(currentNode->leftChild, spaces);
displayTreeRec(currentNode->rightChild, spaces);
}
}
void BST::remove(KeyType removeKey) {
removeRec(removeKey, root);
}
void BST::removeRec(KeyType removeNode, Node *¤tNode) {
if (isLeaf(currentNode)) {
return;
}
else if (removeNode < currentNode->key) {
removeRec(removeNode, currentNode->leftChild);
}
else if (removeNode > currentNode->key) {
removeRec(removeNode, currentNode->rightChild);
}
else {
//Both children are leaves
if (isLeaf(currentNode->leftChild) && isLeaf(currentNode->rightChild)) {
//delete currentNode;
currentNode = nullptr;
}
//One child is a leaf
else if (isLeaf(currentNode->leftChild) || isLeaf(currentNode->rightChild)) {
///adjust pointer to current Node child and delete the node
if (isLeaf(currentNode->leftChild)) {
//*currentNode = *currentNode->rightChild;
*currentNode->rightChild = *currentNode;
currentNode = nullptr;
}
else if (isLeaf(currentNode->rightChild)) {
//*currentNode = *currentNode->leftChild;
*currentNode->leftChild = *currentNode;
currentNode = nullptr;
}
}
//Neither child is a leaf
else {
}
}
}