TheAlgorithms/C++ 1.0.0
All the 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.
 
 BinaryTree (const BinaryTree &)=delete
 
BinaryTreeoperator= (const BinaryTree &)=delete
 

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.

Definition at line 52 of file reverse_binary_tree.cpp.

Constructor & Destructor Documentation

◆ BinaryTree() [1/2]

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

Creates a BinaryTree with a root pointing to NULL.

Definition at line 101 of file reverse_binary_tree.cpp.

101{ root = nullptr; }

◆ 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.

Definition at line 105 of file reverse_binary_tree.cpp.

105{ root = new Node(data); }
int data[MAX]
test data

◆ ~BinaryTree()

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

Definition at line 107 of file reverse_binary_tree.cpp.

107 {
108 std::vector<Node*> nodes;
109 nodes.emplace_back(root);
110 while (!nodes.empty()) {
111 const auto cur_node = nodes.back();
112 nodes.pop_back();
113 if (cur_node) {
114 nodes.emplace_back(cur_node->left);
115 nodes.emplace_back(cur_node->right);
116 delete cur_node;
117 }
118 }
119 }

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.

Definition at line 124 of file reverse_binary_tree.cpp.

124{ 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.

◆ 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

Definition at line 139 of file reverse_binary_tree.cpp.

139 {
140 std::vector<int64_t> data;
141 if (root == nullptr) {
142 return data;
143 }
144 std::queue<Node*> nodes;
145 nodes.push(root);
146 while (!nodes.empty()) {
147 Node* temp = nodes.front();
148 data.push_back(temp->data);
149 nodes.pop();
150 if (temp->left != nullptr) {
151 nodes.push(temp->left);
152 }
153 if (temp->right != nullptr) {
154 nodes.push(temp->right);
155 }
156 }
157 return data;
158 }

◆ 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

Definition at line 65 of file reverse_binary_tree.cpp.

65 {
66 if (pivot == nullptr) {
67 return new Node(data);
68 }
70 pivot->left =
71 insert(data, pivot->left);
72 } else {
73 pivot->right =
74 insert(data, pivot->right);
75 }
76 return pivot;
77 }

◆ 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

Definition at line 164 of file reverse_binary_tree.cpp.

164 {
165 for (int i : get_level_order()) {
166 std::cout << i << " ";
167 }
168 std::cout << "\n";
169 }
std::vector< int64_t > get_level_order()
Level order traversal of a tree consists of visiting its elements, top to bottom, left to right....

◆ reverse()

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

Reverses the Binary Tree

Definition at line 128 of file reverse_binary_tree.cpp.

Node * reverseBinaryTree(Node *pivot)
Reverses a Binary Tree recursively by swapping the left and right subtrees and their children.

◆ 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

Definition at line 84 of file reverse_binary_tree.cpp.

84 {
85 if (pivot == nullptr) {
86 return pivot;
87 }
88 Node* temp = pivot->left;
89 pivot->left = reverseBinaryTree(pivot->right);
90 pivot->right = reverseBinaryTree(temp);
91 return pivot;
92 }

Member Data Documentation

◆ root

Node* operations_on_datastructures::reverse_binary_tree::BinaryTree::root
private

Pointer to root node of Binary Tree.

Definition at line 54 of file reverse_binary_tree.cpp.


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