Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
data_structures::tree_234::Node Class Reference

2-3-4 tree node class More...

Collaboration diagram for data_structures::tree_234::Node:
[legend]

Public Member Functions

 Node (int64_t item)
 Node constructor.
 
int8_t GetCount ()
 Get the item count that current saved in the node.
 
void SetCount (int8_t c)
 Set the item count of the node.
 
bool IsLeaf ()
 Check if node is a leaf.
 
bool IsFull ()
 Check if node is a full (4-node)
 
bool Is2Node ()
 Check if node is a 2-node.
 
bool Is34Node ()
 Check if node is a 3-node or 4-node, this is useful when we delete item from 2-3-4 tree.
 
bool Contains (int64_t item)
 Check if item is in the node.
 
int8_t GetItemIndex (int64_t item)
 Get the index of the item in the node, 0-based.
 
int64_t GetMaxItem ()
 Get max item (rightmost) in the current node.
 
int64_t GetMinItem ()
 get min item (leftmost) in the current node
 
int64_t GetItem (int8_t index)
 Get item of the \index index.
 
void SetItem (int8_t index, int64_t new_item)
 Set item value at position of index.
 
int InsertItem (int item)
 Insert item to the proper position of the node and return the position index.
 
void InsertItemByIndex (int8_t index, int64_t item, Node *with_child, bool to_left=true)
 Insert a value to the index position.
 
NodeRemoveItemByIndex (int8_t index, bool keep_left)
 Insert a value to the index position.
 
int8_t GetChildIndex (Node *child)
 Get the child's index of the children array.
 
NodeGetChild (int8_t index)
 Get the child pointer at position of index.
 
void SetChild (int8_t index, Node *child)
 Set child pointer to the position of index.
 
NodeGetRightmostChild ()
 Get rightmose child of the current node.
 
NodeGetLeftmostChild ()
 Get leftmose child of the current node.
 
NodeGetItemLeftChild (int8_t item_index)
 Get left child of item at item_index.
 
NodeGetItemRightChild (int8_t item_index)
 Get right child of item at item_index.
 
NodeGetNextPossibleChild (int64_t item)
 Get next node which is possibly contains item.
 

Private Attributes

std::array< int64_t, 3 > items
 store items
 
std::array< Node *, 4 > children
 store the children pointers
 
int8_t count = 0
 track the current item count
 

Detailed Description

2-3-4 tree node class

Constructor & Destructor Documentation

◆ Node()

data_structures::tree_234::Node::Node ( int64_t item)
inlineexplicit

Node constructor.

Parameters
itemthe first value we insert to the node
42 : count(1),
43 items({{item, 0, 0}}),
44 children({{nullptr, nullptr, nullptr, nullptr}}) {}
std::array< int64_t, 3 > items
store items
Definition tree_234.cpp:315
int8_t count
track the current item count
Definition tree_234.cpp:319
std::array< Node *, 4 > children
store the children pointers
Definition tree_234.cpp:317

Member Function Documentation

◆ Contains()

bool data_structures::tree_234::Node::Contains ( int64_t item)
inline

Check if item is in the node.

Parameters
itemitem to check
Returns
true if item in the node, otherwise false
92 {
93 for (int8_t i = 0; i < count; i++) {
94 if (item == items[i]) {
95 return true;
96 }
97 }
98 return false;
99 }

◆ GetChild()

Node * data_structures::tree_234::Node::GetChild ( int8_t index)
inline

Get the child pointer at position of index.

Parameters
indexindex of child to get
Returns
the child pointer
252{ return children[index]; }

◆ GetChildIndex()

int8_t data_structures::tree_234::Node::GetChildIndex ( Node * child)
inline

Get the child's index of the children array.

Parameters
childchild pointer of which to get the index
Returns
the index of child
237 {
238 for (int8_t i = 0; i < count + 1; i++) {
239 if (children[i] == child) {
240 return i;
241 }
242 }
243
244 return -1;
245 }

◆ GetCount()

int8_t data_structures::tree_234::Node::GetCount ( )
inline

Get the item count that current saved in the node.

Returns
item count
50{ return count; }

◆ GetItem()

int64_t data_structures::tree_234::Node::GetItem ( int8_t index)
inline

Get item of the \index index.

Parameters
indexthe item index to get
Returns
the item
133{ return items[index]; }

◆ GetItemIndex()

int8_t data_structures::tree_234::Node::GetItemIndex ( int64_t item)
inline

Get the index of the item in the node, 0-based.

Parameters
itemitem to check
Returns
0-based index of the item in the node, if not in the node, -1 is returned
107 {
108 for (int8_t i = 0; i < count; i++) {
109 if (items[i] == item) {
110 return i;
111 }
112 }
113 return -1;
114 }

◆ GetItemLeftChild()

Node * data_structures::tree_234::Node::GetItemLeftChild ( int8_t item_index)
inline

Get left child of item at item_index.

Parameters
item_indexindex of the item whose left child to be get
Returns
left child of items[index]'s
278 {
279 if (item_index < 0 || item_index > count - 1) {
280 return nullptr;
281 }
282
283 return children[item_index];
284 }

◆ GetItemRightChild()

Node * data_structures::tree_234::Node::GetItemRightChild ( int8_t item_index)
inline

Get right child of item at item_index.

Parameters
item_indexindex of the item whose right child to be get
Returns
right child of items[index]'s
291 {
292 if (item_index < 0 || item_index > count - 1) {
293 return nullptr;
294 }
295
296 return children[item_index + 1];
297 }

◆ GetLeftmostChild()

Node * data_structures::tree_234::Node::GetLeftmostChild ( )
inline

Get leftmose child of the current node.

Returns
the leftmost child
271{ return children[0]; }

◆ GetMaxItem()

int64_t data_structures::tree_234::Node::GetMaxItem ( )
inline

Get max item (rightmost) in the current node.

Returns
max item
120{ return items[count - 1]; }

◆ GetMinItem()

int64_t data_structures::tree_234::Node::GetMinItem ( )
inline

get min item (leftmost) in the current node

Returns
min item
126{ return items[0]; }

◆ GetNextPossibleChild()

Node * data_structures::tree_234::Node::GetNextPossibleChild ( int64_t item)
inline

Get next node which is possibly contains item.

Parameters
itemitem to search
Returns
the next node that possibly contains item
304 {
305 int i = 0;
306 for (i = 0; i < count; i++) {
307 if (items[i] > item) {
308 break;
309 }
310 }
311 return children[i];
312 }

◆ GetRightmostChild()

Node * data_structures::tree_234::Node::GetRightmostChild ( )
inline

Get rightmose child of the current node.

Returns
the rightmost child
265{ return children[count]; }

◆ InsertItem()

int data_structures::tree_234::Node::InsertItem ( int item)
inline

Insert item to the proper position of the node and return the position index.

This is a helper function we use during insertion. Please mind that when insert a item, we aslo need to take care of two child pointers. One is the original child pointer at the insertion position. It can be placed as new item's either left child or right child. And the other is the new child that should be added. For our dedicated situation here, we choose to use the original child as the new item's left child, and add a null pointer to its right child. So after use the function, please update these two children pointer manually.

Parameters
itemvalue to be inserted to the node
Returns
index where item is inserted, caller can use this index to update its left and right child
163 {
164 assert(!IsFull());
165
166 if (Contains(item)) {
167 return -1;
168 }
169
170 int8_t i = 0;
171 for (i = 0; i < count; i++) {
172 if (items[i] > item) {
173 break;
174 }
175 }
176
177 InsertItemByIndex(i, item, nullptr, true);
178 return i;
179 }
bool Contains(int64_t item)
Check if item is in the node.
Definition tree_234.cpp:92
void InsertItemByIndex(int8_t index, int64_t item, Node *with_child, bool to_left=true)
Insert a value to the index position.
Definition tree_234.cpp:189
bool IsFull()
Check if node is a full (4-node)
Definition tree_234.cpp:73
Here is the call graph for this function:

◆ InsertItemByIndex()

void data_structures::tree_234::Node::InsertItemByIndex ( int8_t index,
int64_t item,
Node * with_child,
bool to_left = true )
inline

Insert a value to the index position.

Parameters
indexindex where to insert item
itemvalue to insert
with_childnew added child pointer
to_lefttrue indicate adding with_child to new item's left child, otherwise to right child
190 {
191 assert(count < 3 && index >= 0 && index < 3);
192
193 for (int8_t i = count - 1; i >= index; i--) {
194 items[i + 1] = items[i];
195 }
196
197 items[index] = item;
198
199 int8_t start_index = to_left ? index : index + 1;
200
201 for (int8_t i = count; i >= start_index; i--) {
202 children[i + 1] = children[i];
203 }
204
205 children[start_index] = with_child;
206
207 count++;
208 }

◆ Is2Node()

bool data_structures::tree_234::Node::Is2Node ( )
inline

Check if node is a 2-node.

Returns
true if node is 2-node, otherwise false
79{ return count == 1; }

◆ Is34Node()

bool data_structures::tree_234::Node::Is34Node ( )
inline

Check if node is a 3-node or 4-node, this is useful when we delete item from 2-3-4 tree.

Returns
true if node is 3-node or 4-node, false otherwise
85{ return count == 2 || count == 3; }

◆ IsFull()

bool data_structures::tree_234::Node::IsFull ( )
inline

Check if node is a full (4-node)

Returns
true if node is full (4-node), false otherwise
73{ return count == 3; }

◆ IsLeaf()

bool data_structures::tree_234::Node::IsLeaf ( )
inline

Check if node is a leaf.

Returns
true if node is leaf, false otherwise
67{ return children[0] == nullptr; }

◆ RemoveItemByIndex()

Node * data_structures::tree_234::Node::RemoveItemByIndex ( int8_t index,
bool keep_left )
inline

Insert a value to the index position.

Parameters
indexindex of the item to remove
keep_leftwhich child of the item to keep, true keep the left child, false keep the right child
Returns
the removed child pointer
217 {
218 assert(index >= 0 && index < count);
219 Node *removed_child = keep_left ? children[index + 1] : children[index];
220 for (int8_t i = index; i < count - 1; i++) {
221 items[i] = items[i + 1];
222 }
223
224 for (int8_t i = keep_left ? index + 1 : index; i < count; i++) {
225 children[i] = children[i + 1];
226 }
227
228 count--;
229 return removed_child;
230 }
Definition linkedlist_implentation_usingarray.cpp:14

◆ SetChild()

void data_structures::tree_234::Node::SetChild ( int8_t index,
Node * child )
inline

Set child pointer to the position of index.

Parameters
indexchildren index
childpointer to set
259{ children[index] = child; }

◆ SetCount()

void data_structures::tree_234::Node::SetCount ( int8_t c)
inline

Set the item count of the node.

This is only used when we spliting and merging node where we need to do some raw operation manually. In common inserting and removing operation the count is maintained automatically.

Parameters
cthe count to set
61{ count = c; }

◆ SetItem()

void data_structures::tree_234::Node::SetItem ( int8_t index,
int64_t new_item )
inline

Set item value at position of index.

Parameters
indexthe index of the item to set
new_itemitem value
140 {
141 assert(index >= 0 && index <= 2);
142
143 items[index] = new_item;
144 }

The documentation for this class was generated from the following file: