Merge branch 'binary-tree' of https://github.com/polarvoid/C-Plus-Plus into binary-tree

This commit is contained in:
Alvin Philips
2021-10-19 07:21:22 +05:30
2 changed files with 12 additions and 11 deletions

View File

@@ -241,6 +241,7 @@
* [Inorder Successor Of Bst](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/inorder_successor_of_bst.cpp)
* [Intersection Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/intersection_of_2_arrays.cpp)
* [Reverse A Linked List Using Recusion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_a_linked_list_using_recusion.cpp)
* [Reverse Binary Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_binary_tree.cpp)
* [Selectionsortlinkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/selectionsortlinkedlist.cpp)
* [Trie Multiple Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/trie_multiple_search.cpp)
* [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/union_of_2_arrays.cpp)

View File

@@ -36,10 +36,10 @@ struct Node {
/**
* @brief Creates a new Node with some initial data
*/
Node(int _data) {
data = _data; ///< Set value of Node data
left = NULL; ///< Initialize left child to NULL
right = NULL; ///< Initialize right child to NULL
explicit Node(int _data) {
data = _data; ///< Set value of Node data
left = nullptr; ///< Initialize left child to NULL
right = nullptr; ///< Initialize right child to NULL
}
};
@@ -61,7 +61,7 @@ class BinaryTree {
* @returns Node pointer to the root
*/
Node* insert(int data, Node* pivot) {
if (pivot == NULL) {
if (pivot == nullptr) {
return new Node(data); ///< Create new node
}
if (data <= pivot->data) {
@@ -80,7 +80,7 @@ class BinaryTree {
* @returns Node pointer to root node
*/
Node* reverseBinaryTree(Node* pivot) {
if (pivot == NULL) {
if (pivot == nullptr) {
return pivot; ///< Base case
}
Node* temp = pivot->left; ///< pointer to the left subtree
@@ -93,11 +93,11 @@ class BinaryTree {
/**
* @brief Creates a BinaryTree with a root pointing to NULL.
*/
BinaryTree() { root = NULL; }
BinaryTree() { root = nullptr; }
/**
* @brief Creates a BinaryTree with a root with an initial value.
*/
BinaryTree(int data) { root = new Node(data); }
explicit BinaryTree(int data) { root = new Node(data); }
/**
* @brief Adds a new Node to the Binary Tree
*/
@@ -118,7 +118,7 @@ class BinaryTree {
*/
std::vector<int> get_level_order() {
std::vector<int> data; ///< Result vector of int
if (root == NULL) {
if (root == nullptr) {
return data; ///< Return empty vector if root is Invalid
}
std::queue<Node*> nodes; ///< Queue of the nodes in the tree
@@ -127,10 +127,10 @@ class BinaryTree {
Node* temp = nodes.front(); ///< Copy the first element
data.push_back(temp->data); ///< Add the element to the data
nodes.pop(); ///< Remove element
if (temp->left != NULL) {
if (temp->left != nullptr) {
nodes.push(temp->left); ///< Insert left node
}
if (temp->right != NULL) {
if (temp->right != nullptr) {
nodes.push(temp->right); ///< Insert right node
}
} /// Add nodes while Tree is not empty