Commit 4eb19118ef5f5f0a196e8ca1c6b21ad54fa71bb7
1 parent
ecb75377cf
Exists in
master
Set temp node to 'this'
Showing 1 changed file with 2 additions and 2 deletions Inline Diff
BSTNode.hpp
View file @
4eb1911
#ifndef BSTNODE_HPP | 1 | 1 | #ifndef BSTNODE_HPP | |
#define BSTNODE_HPP | 2 | 2 | #define BSTNODE_HPP | |
#include <iostream> | 3 | 3 | #include <iostream> | |
#include <iomanip> | 4 | 4 | #include <iomanip> | |
5 | 5 | |||
6 | 6 | |||
/** This class template defines a node type for the BST container. | 7 | 7 | /** This class template defines a node type for the BST container. | |
* Note that all members are public. So, the BST implementation should | 8 | 8 | * Note that all members are public. So, the BST implementation should | |
* take care not to expose any BSTNode objects. | 9 | 9 | * take care not to expose any BSTNode objects. | |
*/ | 10 | 10 | */ | |
template<typename Data> | 11 | 11 | template<typename Data> | |
class BSTNode { | 12 | 12 | class BSTNode { | |
13 | 13 | |||
public: | 14 | 14 | public: | |
15 | 15 | |||
/** Member variables. */ | 16 | 16 | /** Member variables. */ | |
BSTNode<Data>* parent; | 17 | 17 | BSTNode<Data>* parent; | |
BSTNode<Data>* left; | 18 | 18 | BSTNode<Data>* left; | |
BSTNode<Data>* right; | 19 | 19 | BSTNode<Data>* right; | |
const Data data; // the const Data in this node | 20 | 20 | const Data data; // the const Data in this node | |
int info; // variable used in advanced algorithms | 21 | 21 | int info; // variable used in advanced algorithms | |
22 | 22 | |||
/** Constructor. Initialize a BSTNode with the given Data item, | 23 | 23 | /** Constructor. Initialize a BSTNode with the given Data item, | |
* no parent, and no children. | 24 | 24 | * no parent, and no children. | |
*/ | 25 | 25 | */ | |
BSTNode(const Data & d): data(d) { | 26 | 26 | BSTNode(const Data & d): data(d) { | |
left = right = parent = nullptr; | 27 | 27 | left = right = parent = nullptr; | |
} | 28 | 28 | } | |
29 | 29 | |||
30 | 30 | |||
/** Return the inorder successor of this BSTNode in a BST, | 31 | 31 | /** Return the inorder successor of this BSTNode in a BST, | |
* or nullptr if none. | 32 | 32 | * or nullptr if none. | |
* PRECONDITION: this BSTNode is a node in a BST. | 33 | 33 | * PRECONDITION: this BSTNode is a node in a BST. | |
* POSTCONDITION: the BST is unchanged. | 34 | 34 | * POSTCONDITION: the BST is unchanged. | |
* RETURNS: the BSTNode that is the inorder successor of this BSTNode, | 35 | 35 | * RETURNS: the BSTNode that is the inorder successor of this BSTNode, | |
* or nullptr if there is none. | 36 | 36 | * or nullptr if there is none. | |
*/ // TODO | 37 | 37 | */ // TODO | |
BSTNode<Data>* successor() { | 38 | 38 | BSTNode<Data>* successor() { | |
39 | 39 | |||
BSTNode<Data>* temp; | 40 | 40 | BSTNode<Data>* temp = this; | |
if(right){ | 41 | 41 | if(right){ | |
if(!left){ | 42 | 42 | if(!left){ | |
return right; | 43 | 43 | return right; | |
} | 44 | 44 | } | |
else{ | 45 | 45 | else{ | |
while(left){ | 46 | 46 | while(left){ | |
temp = left.left; | 47 | 47 | temp = left.left; | |
} | 48 | 48 | } | |
return temp; | 49 | 49 | return temp; | |
} | 50 | 50 | } | |
} | 51 | 51 | } | |
if(!right && this == parent.left){ | 52 | 52 | if(!right && this == parent.left){ | |
return parent; | 53 | 53 | return parent; | |
} | 54 | 54 | } | |
if(!right && this == parent.right){ | 55 | 55 | if(!right && this == parent.right){ | |
return parent.parent; | 56 | 56 | return parent.parent; | |
} | 57 | 57 | } | |
if(!right && !parent){ | 58 | 58 | if(!right && !parent){ | |
return 0; | 59 | 59 | return 0; | |
} | 60 | 60 | } | |
61 | 61 | |||
}; | 62 | 62 | }; | |
63 | 63 | |||
/** Overload operator<< to insert a BSTNode's fields in an ostream. */ | 64 | 64 | /** Overload operator<< to insert a BSTNode's fields in an ostream. */ | |
template <typename Data> | 65 | 65 | template <typename Data> | |
std::ostream & operator<<(std::ostream& stm, const BSTNode<Data> & n) { | 66 | 66 | std::ostream & operator<<(std::ostream& stm, const BSTNode<Data> & n) { | |
stm << '['; | 67 | 67 | stm << '['; | |
stm << std::setw(10) << &n; // address of the BSTNode | 68 | 68 | stm << std::setw(10) << &n; // address of the BSTNode |