-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.cpp
More file actions
96 lines (72 loc) · 2.25 KB
/
node.cpp
File metadata and controls
96 lines (72 loc) · 2.25 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
// Yusuf Pisan pisan@uw.edu
// 15 Jan 2018
// DO NOT compile this file
// This file is included in the binarynode.h file via #include statement
// Allows us to keep the templated class header and implementation separate
// BinaryNode to help with BinarySearchTree class
// Uses template so that ItemType can be any type
// getLeftChildPtr and setLeftChildPtr used to get/set child nodes
// getItem/setItem used to set the data stored in this node
// BinarySearchTree requires <, > relationships to be defined for ItemType
// << for BinaryNode is defined to print the ItemType as [BN: item ]
// binarynode.cpp file is included at the bottom of the .h file
// binarynode.cpp is part of the template, cannot be compiled separately
// default constructor, children set to nullptr as default
// item contained is undefined
template<class T>
Node<T>::Node() :left{nullptr}, right{nullptr}, parent{nullptr} {}
// destructor
template<class T>
Node<T>::~Node() {
// If suffering from memory leaks, uncomment next line
// std::cout << "Deleting " << *this << std::endl;
}
// constructor setting item
// left and right childPtr set to nullptr as default
template<class T>
Node<T>::Node(const T &item) :item{item}, left{nullptr}, right{nullptr}, parent{nullptr}{}
// true if no children, both leftPtr and rightPtr are nullptrs
template<class T>
bool Node<T>::isLeaf() const {
return this->left == nullptr && this->right == nullptr;
}
// getter for left child
template<class T>
Node<T> *Node<T>::getLeft() const {
return this->left;
}
// setter for left child
template<class T>
void Node<T>::setLeft(Node<T> *childPtr) {
this->left = childPtr;
}
// getter for right child
template<class T>
Node<T> *Node<T>::getRight() const {
return this->right;
}
// setter for left child
template<class T>
void Node<T>::setRight(Node<T> *childPtr) {
this->right = childPtr;
}
// getter for item stored at node
template<class T>
T Node<T>::getItem() const {
return this->item;
}
// setter for item stored at node
template<class T>
void Node<T>::setItem(const T &item) {
this->item = item;
}
// get parent ptr
template<class T>
Node<T>* Node<T>::getParent() const {
return this->parent;
}
// set parent ptr
template<class T>
void Node<T>::setParent(Node<T>* parent) {
this->parent = parent;
}