mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-12 23:15:52 +08:00
clang-format and clang-tidy fixes for 9d76f8bd
This commit is contained in:
@@ -10,27 +10,28 @@
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
|
||||
typedef struct node {
|
||||
using node = struct node {
|
||||
int data;
|
||||
int height;
|
||||
struct node *left;
|
||||
struct node *right;
|
||||
} node;
|
||||
};
|
||||
|
||||
/** Create and return a new Node */
|
||||
node *createNode(int data) {
|
||||
node *nn = new node();
|
||||
nn->data = data;
|
||||
nn->height = 0;
|
||||
nn->left = NULL;
|
||||
nn->right = NULL;
|
||||
nn->left = nullptr;
|
||||
nn->right = nullptr;
|
||||
return nn;
|
||||
}
|
||||
|
||||
/** Returns height of tree */
|
||||
int height(node *root) {
|
||||
if (root == NULL)
|
||||
if (root == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return 1 + std::max(height(root->left), height(root->right));
|
||||
}
|
||||
|
||||
@@ -57,52 +58,58 @@ node *leftRotate(node *root) {
|
||||
|
||||
/** Returns node with minimum value in the tree */
|
||||
node *minValue(node *root) {
|
||||
if (root->left == NULL)
|
||||
if (root->left == nullptr) {
|
||||
return root;
|
||||
}
|
||||
return minValue(root->left);
|
||||
}
|
||||
|
||||
/** Balanced Insertion */
|
||||
node *insert(node *root, int item) {
|
||||
if (root == NULL)
|
||||
if (root == nullptr) {
|
||||
return createNode(item);
|
||||
if (item < root->data)
|
||||
}
|
||||
if (item < root->data) {
|
||||
root->left = insert(root->left, item);
|
||||
else
|
||||
} else {
|
||||
root->right = insert(root->right, item);
|
||||
}
|
||||
int b = getBalance(root);
|
||||
if (b > 1) {
|
||||
if (getBalance(root->left) < 0)
|
||||
if (getBalance(root->left) < 0) {
|
||||
root->left = leftRotate(root->left); // Left-Right Case
|
||||
return rightRotate(root); // Left-Left Case
|
||||
}
|
||||
return rightRotate(root); // Left-Left Case
|
||||
} else if (b < -1) {
|
||||
if (getBalance(root->right) > 0)
|
||||
if (getBalance(root->right) > 0) {
|
||||
root->right = rightRotate(root->right); // Right-Left Case
|
||||
return leftRotate(root); // Right-Right Case
|
||||
}
|
||||
return leftRotate(root); // Right-Right Case
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/** Balanced Deletion */
|
||||
node *deleteNode(node *root, int key) {
|
||||
if (root == NULL)
|
||||
if (root == nullptr) {
|
||||
return root;
|
||||
if (key < root->data)
|
||||
}
|
||||
if (key < root->data) {
|
||||
root->left = deleteNode(root->left, key);
|
||||
else if (key > root->data)
|
||||
} else if (key > root->data) {
|
||||
root->right = deleteNode(root->right, key);
|
||||
|
||||
else {
|
||||
} else {
|
||||
// Node to be deleted is leaf node or have only one Child
|
||||
if (!root->right) {
|
||||
node *temp = root->left;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
root = nullptr;
|
||||
return temp;
|
||||
} else if (!root->left) {
|
||||
node *temp = root->right;
|
||||
delete (root);
|
||||
root = NULL;
|
||||
root = nullptr;
|
||||
return temp;
|
||||
}
|
||||
// Node to be deleted have both left and right subtrees
|
||||
@@ -131,18 +138,20 @@ void levelOrder(node *root) {
|
||||
root = q.front();
|
||||
std::cout << root->data << " ";
|
||||
q.pop();
|
||||
if (root->left)
|
||||
if (root->left) {
|
||||
q.push(root->left);
|
||||
if (root->right)
|
||||
}
|
||||
if (root->right) {
|
||||
q.push(root->right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main() {
|
||||
// Testing AVL Tree
|
||||
node *root = NULL;
|
||||
int i;
|
||||
node *root = nullptr;
|
||||
int i = 0;
|
||||
for (i = 1; i <= 7; i++) root = insert(root, i);
|
||||
std::cout << "LevelOrder: ";
|
||||
levelOrder(root);
|
||||
|
||||
Reference in New Issue
Block a user