fix documentations and cpplint

This commit is contained in:
Krishna Vedala
2020-06-04 23:15:43 -04:00
parent 6635c0a1af
commit aacaf9828c
6 changed files with 174 additions and 159 deletions

View File

@@ -1,8 +1,15 @@
/**
* \file
* \brief A simple tree implementation using nodes
*
* \todo update code to use C++ STL library features and OO structure
* \warning This program is a poor implementation and does not utilize any of
* the C++ STL features.
*/
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
typedef struct node {
int data;
int height;
@@ -10,10 +17,7 @@ typedef struct node {
struct node *right;
} node;
int max(int a, int b) { return a > b ? a : b; }
// Returns a new Node
/** Create and return a new Node */
node *createNode(int data) {
node *nn = new node();
nn->data = data;
@@ -23,20 +27,17 @@ node *createNode(int data) {
return nn;
}
// Returns height of tree
/** Returns height of tree */
int height(node *root) {
if (root == NULL)
return 0;
return 1 + max(height(root->left), height(root->right));
return 1 + std::max(height(root->left), height(root->right));
}
// Returns difference between height of left and right subtree
/** Returns difference between height of left and right subtree */
int getBalance(node *root) { return height(root->left) - height(root->right); }
// Returns Node after Right Rotation
/** Returns Node after Right Rotation */
node *rightRotate(node *root) {
node *t = root->left;
node *u = t->right;
@@ -45,8 +46,7 @@ node *rightRotate(node *root) {
return t;
}
// Returns Node after Left Rotation
/** Returns Node after Left Rotation */
node *leftRotate(node *root) {
node *t = root->right;
node *u = t->left;
@@ -55,16 +55,14 @@ node *leftRotate(node *root) {
return t;
}
// Returns node with minimum value in the tree
/** Returns node with minimum value in the tree */
node *minValue(node *root) {
if (root->left == NULL)
return root;
return minValue(root->left);
}
// Balanced Insertion
/** Balanced Insertion */
node *insert(node *root, int item) {
node *nn = createNode(item);
if (root == NULL)
@@ -86,8 +84,7 @@ node *insert(node *root, int item) {
return root;
}
// Balanced Deletion
/** Balanced Deletion */
node *deleteNode(node *root, int key) {
if (root == NULL)
return root;
@@ -118,14 +115,13 @@ node *deleteNode(node *root, int key) {
return root;
}
// LevelOrder (Breadth First Search)
/** LevelOrder (Breadth First Search) */
void levelOrder(node *root) {
queue<node *> q;
std::queue<node *> q;
q.push(root);
while (!q.empty()) {
root = q.front();
cout << root->data << " ";
std::cout << root->data << " ";
q.pop();
if (root->left)
q.push(root->left);
@@ -134,18 +130,19 @@ void levelOrder(node *root) {
}
}
/** Main function */
int main() {
// Testing AVL Tree
node *root = NULL;
int i;
for (i = 1; i <= 7; i++) root = insert(root, i);
cout << "LevelOrder: ";
std::cout << "LevelOrder: ";
levelOrder(root);
root = deleteNode(root, 1); // Deleting key with value 1
cout << "\nLevelOrder: ";
std::cout << "\nLevelOrder: ";
levelOrder(root);
root = deleteNode(root, 4); // Deletin key with value 4
cout << "\nLevelOrder: ";
std::cout << "\nLevelOrder: ";
levelOrder(root);
return 0;
}