TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
A Binary Tree class that implements a Binary Search Tree (BST) by default. More...
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 | |
Node * | insert (int64_t data, Node *pivot) |
inserts a node in the Binary Tree, with the behaviouur of a Binary Search Tree. | |
Node * | reverseBinaryTree (Node *pivot) |
Reverses a Binary Tree recursively by swapping the left and right subtrees and their children. | |
BinaryTree (const BinaryTree &)=delete | |
BinaryTree & | operator= (const BinaryTree &)=delete |
Private Attributes | |
Node * | root |
Pointer to root node of Binary Tree. | |
A Binary Tree class that implements a Binary Search Tree (BST) by default.
Definition at line 52 of file reverse_binary_tree.cpp.
|
inline |
Creates a BinaryTree with a root pointing to NULL.
Definition at line 101 of file reverse_binary_tree.cpp.
|
inlineexplicit |
Creates a BinaryTree with a root with an initial value.
Definition at line 105 of file reverse_binary_tree.cpp.
|
inline |
Definition at line 107 of file reverse_binary_tree.cpp.
|
inline |
Adds a new Node to the Binary Tree.
Definition at line 124 of file reverse_binary_tree.cpp.
|
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.
< 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.
|
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))
data | The data/value of the Node to be inserted |
pivot | A pointer to the root node of the (sub)tree |
< Create new node
< Insert Node to the left
< Insert node to the right
Definition at line 65 of file reverse_binary_tree.cpp.
|
inline |
Prints all of the elements in the tree to stdout level-by-level, using the get_level_order() function.
Print each element in the tree
Print newline
Definition at line 164 of file reverse_binary_tree.cpp.
|
inline |
Reverses the Binary Tree
Definition at line 128 of file reverse_binary_tree.cpp.
|
inlineprivate |
Reverses a Binary Tree recursively by swapping the left and right subtrees and their children.
pivot | A reference to the root of the (sub)tree |
< Base case
< pointer to the left subtree
< Swap
< Swap
Definition at line 84 of file reverse_binary_tree.cpp.
|
private |
Pointer to root node of Binary Tree.
Definition at line 54 of file reverse_binary_tree.cpp.