Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
operations_on_datastructures::reverse_binary_tree::BinaryTree Class Reference

A Binary Tree class that implements a Binary Search Tree (BST) by default. More...

Collaboration diagram for operations_on_datastructures::reverse_binary_tree::BinaryTree:
[legend]

Public Member Functions

 BinaryTree ()
 Creates a BinaryTree with a root pointing to NULL.
 
 BinaryTree (int64_t data)
 Creates a BinaryTree with a root with an initial value.
 
void add (int64_t data)
 Adds a new Node to the Binary Tree.
 
void reverse ()
 
std::vector< int64_t > get_level_order ()
 Level order traversal of a tree consists of visiting its elements, top to bottom, left to right. This function performs level order traversal and returns the node datas as a vector.
 
void print ()
 Prints all of the elements in the tree to stdout level-by-level, using the get_level_order() function.
 

Private Member Functions

Nodeinsert (int64_t data, Node *pivot)
 inserts a node in the Binary Tree, with the behaviouur of a Binary Search Tree.
 
NodereverseBinaryTree (Node *pivot)
 Reverses a Binary Tree recursively by swapping the left and right subtrees and their children.
 

Private Attributes

Noderoot
 Pointer to root node of Binary Tree.
 

Detailed Description

A Binary Tree class that implements a Binary Search Tree (BST) by default.

Constructor & Destructor Documentation

◆ BinaryTree() [1/2]

operations_on_datastructures::reverse_binary_tree::BinaryTree::BinaryTree ( )
inline

Creates a BinaryTree with a root pointing to NULL.

98{ root = nullptr; }
Node * root
Pointer to root node of Binary Tree.
Definition reverse_binary_tree.cpp:54

◆ BinaryTree() [2/2]

operations_on_datastructures::reverse_binary_tree::BinaryTree::BinaryTree ( int64_t data)
inlineexplicit

Creates a BinaryTree with a root with an initial value.

102{ root = new Node(data); }
int data[MAX]
test data
Definition hash_search.cpp:24
Definition linkedlist_implentation_usingarray.cpp:14

Member Function Documentation

◆ add()

void operations_on_datastructures::reverse_binary_tree::BinaryTree::add ( int64_t data)
inline

Adds a new Node to the Binary Tree.

106{ root = insert(data, root); }
Node * insert(int64_t data, Node *pivot)
inserts a node in the Binary Tree, with the behaviouur of a Binary Search Tree.
Definition reverse_binary_tree.cpp:65
Here is the call graph for this function:

◆ get_level_order()

std::vector< int64_t > operations_on_datastructures::reverse_binary_tree::BinaryTree::get_level_order ( )
inline

Level order traversal of a tree consists of visiting its elements, top to bottom, left to right. This function performs level order traversal and returns the node datas as a vector.

The function uses a queue to append and remove elements as they are visited, and then adds their children, if any. This ensures that the elements are visited layer-by-layer, starting from the root of the Tree.

Returns
vector<int64_t> of nodes of the tree.

< Result vector of int

< Return empty vector if root is Invalid

< Queue of the nodes in the tree

< Insert root into the queue

< Copy the first element

< Add the element to the data

< Remove element

< Insert left node

< Insert right node

Add nodes while Tree is not empty

121 {
122 std::vector<int64_t> data; ///< Result vector of int
123 if (root == nullptr) {
124 return data; ///< Return empty vector if root is Invalid
125 }
126 std::queue<Node*> nodes; ///< Queue of the nodes in the tree
127 nodes.push(root); ///< Insert root into the queue
128 while (!nodes.empty()) {
129 Node* temp = nodes.front(); ///< Copy the first element
130 data.push_back(temp->data); ///< Add the element to the data
131 nodes.pop(); ///< Remove element
132 if (temp->left != nullptr) {
133 nodes.push(temp->left); ///< Insert left node
134 }
135 if (temp->right != nullptr) {
136 nodes.push(temp->right); ///< Insert right node
137 }
138 } /// Add nodes while Tree is not empty
139 return data;
140 }
T empty(T... args)
T front(T... args)
T pop(T... args)
T push(T... args)
Here is the call graph for this function:

◆ insert()

Node * operations_on_datastructures::reverse_binary_tree::BinaryTree::insert ( int64_t data,
Node * pivot )
inlineprivate

inserts a node in the Binary Tree, with the behaviouur of a Binary Search Tree.

Nodes with smaller values are inserted in the left subtree, and Nodes with larger values are inserted into the right subtree recursively. Time Complexity: O(log(n))

Parameters
dataThe data/value of the Node to be inserted
pivotA pointer to the root node of the (sub)tree
Returns
Node pointer to the root

< Create new node

< Insert Node to the left

< Insert node to the right

65 {
66 if (pivot == nullptr) {
67 return new Node(data); ///< Create new node
68 }
69 if (data <= pivot->data) {
70 pivot->left =
71 insert(data, pivot->left); ///< Insert Node to the left
72 } else {
73 pivot->right =
74 insert(data, pivot->right); ///< Insert node to the right
75 }
76 return pivot;
77 }
Here is the call graph for this function:

◆ print()

void operations_on_datastructures::reverse_binary_tree::BinaryTree::print ( )
inline

Prints all of the elements in the tree to stdout level-by-level, using the get_level_order() function.

Returns
void

Print each element in the tree

Print newline

146 {
147 for (int i : get_level_order()) {
148 std::cout << i << " "; /// Print each element in the tree
149 }
150 std::cout << "\n"; /// Print newline
151 }
std::vector< int64_t > get_level_order()
Level order traversal of a tree consists of visiting its elements, top to bottom, left to right....
Definition reverse_binary_tree.cpp:121
Here is the call graph for this function:

◆ reverse()

void operations_on_datastructures::reverse_binary_tree::BinaryTree::reverse ( )
inline

Reverses the Binary Tree

Node * reverseBinaryTree(Node *pivot)
Reverses a Binary Tree recursively by swapping the left and right subtrees and their children.
Definition reverse_binary_tree.cpp:84
Here is the call graph for this function:

◆ reverseBinaryTree()

Node * operations_on_datastructures::reverse_binary_tree::BinaryTree::reverseBinaryTree ( Node * pivot)
inlineprivate

Reverses a Binary Tree recursively by swapping the left and right subtrees and their children.

Parameters
pivotA reference to the root of the (sub)tree
Returns
Node pointer to root node

< Base case

< pointer to the left subtree

< Swap

< Swap

84 {
85 if (pivot == nullptr) {
86 return pivot; ///< Base case
87 }
88 Node* temp = pivot->left; ///< pointer to the left subtree
89 pivot->left = reverseBinaryTree(pivot->right); ///< Swap
90 pivot->right = reverseBinaryTree(temp); ///< Swap
91 return pivot;
92 }
Here is the call graph for this function:

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