libUTL++
BinTreeNode.h
1 #pragma once
2 
4 
5 #include <libutl/Object.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
29 
32 {
33 public:
36  {
37  init();
38  }
39 
41  virtual ~BinTreeNode()
42  {
43  }
44 
49  BinTreeNode(const Object* object)
50  {
51  init(object);
52  }
53 
54  void dump(Stream& os, uint_t level);
55 
57  Object*
58  get() const
59  {
60  return _object;
61  }
62 
64  bool
65  isLeaf() const
66  {
67  return (_object == nullptr);
68  }
69 
71  bool
72  isLeftChild() const
73  {
74  return ((_parent != nullptr) && (_parent->_left == this));
75  }
76 
78  bool
79  isRightChild() const
80  {
81  return ((_parent != nullptr) && (_parent->_right == this));
82  }
83 
85  bool
86  isRoot() const
87  {
88  return (_parent == nullptr);
89  }
90 
93  left() const
94  {
95  return _left;
96  }
97 
99  const BinTreeNode*
100  leftMost() const
101  {
102  return const_cast_this->leftMost();
103  }
104 
106  BinTreeNode* leftMost();
107 
109  BinTreeNode* next() const;
110 
112  BinTreeNode*
113  parent() const
114  {
115  return _parent;
116  }
117 
119  BinTreeNode* prev() const;
120 
126  void remove(BinTreeNode*& x, BinTreeNode*& y);
127 
129  BinTreeNode*
130  right() const
131  {
132  return _right;
133  }
134 
136  const BinTreeNode*
137  rightMost() const
138  {
139  return const_cast_this->rightMost();
140  }
141 
143  BinTreeNode* rightMost();
144 
146  virtual void addFixup() = 0;
147 
149  virtual void removeFixup(const BinTreeNode* removedNode) = 0;
150 
152  virtual void swapWith(BinTreeNode* rhs);
153 
155  void rotateLeft();
156 
158  void rotateRight();
159 
161  void
162  set(const Object* object)
163  {
164  _object = const_cast<Object*>(object);
165  }
166 
168  void
170  {
171  _left = node;
172  _left->_parent = this;
173  }
174 
176  void
178  {
179  _right = node;
180  _right->_parent = this;
181  }
182 
184  BinTreeNode* sibling() const;
185 
186 protected:
187  BinTreeNode *_parent, *_left, *_right;
188  Object* _object;
189 
190 private:
191  void init(const Object* object = nullptr);
192  void
193  deInit()
194  {
195  }
196 };
197 
199 
200 UTL_NS_END;
bool isLeftChild() const
Determine whether self is a left child.
Definition: BinTreeNode.h:72
bool isRightChild() const
Determine whether self is a right child.
Definition: BinTreeNode.h:79
#define const_cast_this
Pointer to the object the method was invoked on (casting away const).
Definition: macros.h:41
void deInit()
De-initialize UTL++.
void setRight(BinTreeNode *node)
Set the right child.
Definition: BinTreeNode.h:177
BinTreeNode * parent() const
Return the parent node (nullptr if root).
Definition: BinTreeNode.h:113
const BinTreeNode * leftMost() const
Return the left-most child (smallest node reachable from self).
Definition: BinTreeNode.h:100
BinTreeNode()
Constructor.
Definition: BinTreeNode.h:35
void setLeft(BinTreeNode *node)
Set the left child.
Definition: BinTreeNode.h:169
virtual ~BinTreeNode()
Destructor.
Definition: BinTreeNode.h:41
BinTreeNode(const Object *object)
Constructor.
Definition: BinTreeNode.h:49
BinTreeNode * right() const
Return the right child.
Definition: BinTreeNode.h:130
bool isLeaf() const
Determine whether self is a leaf node.
Definition: BinTreeNode.h:65
Binary tree node.
Definition: BinTreeNode.h:31
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Stream I/O abstraction.
Definition: Stream.h:68
bool isRoot() const
Determine whether self is the root node.
Definition: BinTreeNode.h:86
BinTreeNode * left() const
Return the left child.
Definition: BinTreeNode.h:93
Root of UTL++ class hierarchy.
Definition: Object.h:52
void dump(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t level=uint_t_max, bool key=false, bool printClassName=false, uint_t indent=0, const char *separator=nullptr)
Dump objects to the given stream (with Object::dump()).
void init()
Initialize UTL++.
const BinTreeNode * rightMost() const
Return the right-most child (largest node reachable from self).
Definition: BinTreeNode.h:137