Guest User

Untitled

a guest
Oct 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #ifndef BSTNODE_HPP
  2. #define BSTNODE_HPP
  3.  
  4.  
  5. template<typename Data>
  6. class BSTNode {
  7.  
  8. public:
  9.  
  10. /** Constructor. Initialize a BSTNode with the given Data item,
  11. * no parent, and no children.
  12. */
  13. BSTNode(const Data & d): data(d) {
  14. left = right = parent = 0;
  15. }
  16.  
  17. BSTNode<Data>* left;
  18. BSTNode<Data>* right;
  19. BSTNode<Data>* parent;
  20. int info; // instance variable used in advanced algorithms
  21. const Data data;
  22.  
  23. /** Return the inorder successor of this BSTNode in a BST, or 0 if none.
  24. * PRECONDITION: this BSTNode is a node in a BST.
  25. * POSTCONDITION: the BST is unchanged.
  26. * RETURNS: the BSTNode that is the inorder successor of this BSTNode,
  27. * or 0 if there is none.
  28. */ // TODO
  29. BSTNode<Data>* successor() {
  30.  
  31. BSTNode<Data>* leftCurrentNode;
  32. BSTNode<Data>* upCurrentNode;
  33.  
  34.  
  35. // This covers the case where there is a right child
  36. if (this->right != 0) {
  37. if (this->right->left !=0) {
  38. leftCurrentNode = this->right->left;
  39. while (leftCurrentNode != 0) {
  40. leftCurrentNode = leftCurrentNode->left;
  41. }
  42. return leftCurrentNode;
  43. }
  44. return this->right;
  45. }
  46.  
  47. // This covers the case where there is no right child
  48. else if (this->parent != 0) {
  49. if (this->parent->data < this->data) {
  50. upCurrentNode = this->parent;
  51. while (upCurrentNode->parent->data < this->data) {
  52. upCurrentNode = upCurrentNode->parent;
  53. }
  54. if (upCurrentNode->data > this->data) {
  55. return upCurrentNode;
  56. }
  57. }
  58. }
  59. return 0;
  60. }
  61.  
  62. };
  63.  
  64. #endif // BSTNODE_HPP
Add Comment
Please, Sign In to add comment