-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTNode.h
More file actions
109 lines (94 loc) · 3.44 KB
/
BSTNode.h
File metadata and controls
109 lines (94 loc) · 3.44 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
// From the software distribution accompanying the textbook
// "A Practical Introduction to Data Structures and Algorithm Analysis,
// Third Edition (C++)" by Clifford A. Shaffer.
// Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.
// 1/26/22 pragma once modification by Prof Sipantzi
// This is the file to include for access to the complete binary node
// template implementation
#include "book.h"
#include "BinNode.h"
#pragma once
// Simple binary tree node implementation
template <typename Key, typename E>
class BSTNode : public BinNode<E> {
private:
Key k; // The node's key
E it; // The node's value
BSTNode* lc; // Pointer to left child
BSTNode* rc; // Pointer to right child
/* Bitfield to store whether the lc and rc are threads
0: LC RC
1: LT RC
2: LC RT
3: LT RT
Even = LC
Odd = LT
>1 = RT
<=1 = RC */
unsigned int threadBitField : 2;
public:
// Two constructors -- with and without initial values
BSTNode() { lc = rc = NULL; threadBitField = 0; }
BSTNode(Key K, E e, BSTNode* l = NULL, BSTNode* r = NULL)
{
k = K; it = e; lc = l; rc = r;
}
~BSTNode() {} // Destructor
// Functions to set and return the value and key
E& element() { return it; }
void setElement(const E& e) { it = e; }
Key& key() { return k; }
void setKey(const Key& K) { k = K; }
// Functions to set and return the children
inline BSTNode* left() const { return lc; }
void setLeft(BinNode<E>* b) { lc = (BSTNode*)b; }
inline BSTNode* right() const { return rc; }
void setRight(BinNode<E>* b) { rc = (BSTNode*)b; }
/* Returns ptr to the left child if it is not a thread, else returns NULL.
Time Complexity = Constant, O(1) */
inline BSTNode* safeLeft() const { return (getLThr()) ? NULL : left(); }
/* Returns ptr to the right child if it is not a thread, else returns NULL.
Time Complexity = Constant, O(1) */
inline BSTNode* safeRight() const { return (getRThr()) ? NULL : right(); }
// Thread boolean getters and setters
/* Returns true if the left child is a thread.
Time Complexity = Constant, O(1) */
bool getLThr() const {
return (threadBitField % 2 == 1);// Return true if odd
}
/* Returns true if the right child is a thread.
Time Complexity = Constant, O(1) */
bool getRThr() const {
return (threadBitField > 1);// Return true if greater than 1
}
/* Sets the left thread bool to the parameter.
Time Complexity = Constant, O(1) */
void setLThr(bool b) {
if (threadBitField > 1)// 2 or 3
if (b)
threadBitField = 3;
else
threadBitField = 2;
else// 0 or 1
if (b)
threadBitField = 1;
else
threadBitField = 0;
}// End of setLThr
/* Sets the right thread bool to the parameter.
Time Complexity = Constant, O(1) */
void setRThr(bool b) {
if (threadBitField % 2 == 0)// 0 or 2
if (b)
threadBitField = 2;
else
threadBitField = 0;
else// 1 or 3
if (b)
threadBitField = 3;
else
threadBitField = 1;
}// End of setRThr
// Return true if it is a leaf, false otherwise
bool isLeaf() { return (lc == NULL) && (rc == NULL); }
};