Skip to content

Commit aacaf98

Browse files
committed
fix documentations and cpplint
1 parent 6635c0a commit aacaf98

6 files changed

+174
-159
lines changed

data_structures/avltree.cpp

+25-28
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
/**
2+
* \file
3+
* \brief A simple tree implementation using nodes
4+
*
5+
* \todo update code to use C++ STL library features and OO structure
6+
* \warning This program is a poor implementation and does not utilize any of
7+
* the C++ STL features.
8+
*/
9+
#include <algorithm>
110
#include <iostream>
211
#include <queue>
312

4-
using namespace std;
5-
613
typedef struct node {
714
int data;
815
int height;
916
struct node *left;
1017
struct node *right;
1118
} node;
1219

13-
int max(int a, int b) { return a > b ? a : b; }
14-
15-
// Returns a new Node
16-
20+
/** Create and return a new Node */
1721
node *createNode(int data) {
1822
node *nn = new node();
1923
nn->data = data;
@@ -23,20 +27,17 @@ node *createNode(int data) {
2327
return nn;
2428
}
2529

26-
// Returns height of tree
27-
30+
/** Returns height of tree */
2831
int height(node *root) {
2932
if (root == NULL)
3033
return 0;
31-
return 1 + max(height(root->left), height(root->right));
34+
return 1 + std::max(height(root->left), height(root->right));
3235
}
3336

34-
// Returns difference between height of left and right subtree
35-
37+
/** Returns difference between height of left and right subtree */
3638
int getBalance(node *root) { return height(root->left) - height(root->right); }
3739

38-
// Returns Node after Right Rotation
39-
40+
/** Returns Node after Right Rotation */
4041
node *rightRotate(node *root) {
4142
node *t = root->left;
4243
node *u = t->right;
@@ -45,8 +46,7 @@ node *rightRotate(node *root) {
4546
return t;
4647
}
4748

48-
// Returns Node after Left Rotation
49-
49+
/** Returns Node after Left Rotation */
5050
node *leftRotate(node *root) {
5151
node *t = root->right;
5252
node *u = t->left;
@@ -55,16 +55,14 @@ node *leftRotate(node *root) {
5555
return t;
5656
}
5757

58-
// Returns node with minimum value in the tree
59-
58+
/** Returns node with minimum value in the tree */
6059
node *minValue(node *root) {
6160
if (root->left == NULL)
6261
return root;
6362
return minValue(root->left);
6463
}
6564

66-
// Balanced Insertion
67-
65+
/** Balanced Insertion */
6866
node *insert(node *root, int item) {
6967
node *nn = createNode(item);
7068
if (root == NULL)
@@ -86,8 +84,7 @@ node *insert(node *root, int item) {
8684
return root;
8785
}
8886

89-
// Balanced Deletion
90-
87+
/** Balanced Deletion */
9188
node *deleteNode(node *root, int key) {
9289
if (root == NULL)
9390
return root;
@@ -118,14 +115,13 @@ node *deleteNode(node *root, int key) {
118115
return root;
119116
}
120117

121-
// LevelOrder (Breadth First Search)
122-
118+
/** LevelOrder (Breadth First Search) */
123119
void levelOrder(node *root) {
124-
queue<node *> q;
120+
std::queue<node *> q;
125121
q.push(root);
126122
while (!q.empty()) {
127123
root = q.front();
128-
cout << root->data << " ";
124+
std::cout << root->data << " ";
129125
q.pop();
130126
if (root->left)
131127
q.push(root->left);
@@ -134,18 +130,19 @@ void levelOrder(node *root) {
134130
}
135131
}
136132

133+
/** Main function */
137134
int main() {
138135
// Testing AVL Tree
139136
node *root = NULL;
140137
int i;
141138
for (i = 1; i <= 7; i++) root = insert(root, i);
142-
cout << "LevelOrder: ";
139+
std::cout << "LevelOrder: ";
143140
levelOrder(root);
144141
root = deleteNode(root, 1); // Deleting key with value 1
145-
cout << "\nLevelOrder: ";
142+
std::cout << "\nLevelOrder: ";
146143
levelOrder(root);
147144
root = deleteNode(root, 4); // Deletin key with value 4
148-
cout << "\nLevelOrder: ";
145+
std::cout << "\nLevelOrder: ";
149146
levelOrder(root);
150147
return 0;
151148
}

data_structures/binary_search_tree.cpp

+46-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
/**
2+
* \file
3+
* \brief A simple tree implementation using structured nodes
4+
*
5+
* \todo update code to use C++ STL library features and OO structure
6+
* \warning This program is a poor implementation - C style - and does not
7+
* utilize any of the C++ STL features.
8+
*/
19
#include <iostream>
2-
using namespace std;
310

411
struct node {
512
int val;
@@ -84,7 +91,7 @@ void Remove(node *p, node *n, int x) {
8491

8592
void BFT(node *n) {
8693
if (n != NULL) {
87-
cout << n->val << " ";
94+
std::cout << n->val << " ";
8895
enqueue(n->left);
8996
enqueue(n->right);
9097
BFT(dequeue());
@@ -93,7 +100,7 @@ void BFT(node *n) {
93100

94101
void Pre(node *n) {
95102
if (n != NULL) {
96-
cout << n->val << " ";
103+
std::cout << n->val << " ";
97104
Pre(n->left);
98105
Pre(n->right);
99106
}
@@ -102,7 +109,7 @@ void Pre(node *n) {
102109
void In(node *n) {
103110
if (n != NULL) {
104111
In(n->left);
105-
cout << n->val << " ";
112+
std::cout << n->val << " ";
106113
In(n->right);
107114
}
108115
}
@@ -111,7 +118,7 @@ void Post(node *n) {
111118
if (n != NULL) {
112119
Post(n->left);
113120
Post(n->right);
114-
cout << n->val << " ";
121+
std::cout << n->val << " ";
115122
}
116123
}
117124

@@ -121,45 +128,47 @@ int main() {
121128
int value;
122129
int ch;
123130
node *root = new node;
124-
cout << "\nEnter the value of root node :";
125-
cin >> value;
131+
std::cout << "\nEnter the value of root node :";
132+
std::cin >> value;
126133
root->val = value;
127134
root->left = NULL;
128135
root->right = NULL;
129136
do {
130-
cout << "\n1. Insert";
131-
cout << "\n2. Delete";
132-
cout << "\n3. Breadth First";
133-
cout << "\n4. Preorder Depth First";
134-
cout << "\n5. Inorder Depth First";
135-
cout << "\n6. Postorder Depth First";
137+
std::cout << "\n1. Insert"
138+
<< "\n2. Delete"
139+
<< "\n3. Breadth First"
140+
<< "\n4. Preorder Depth First"
141+
<< "\n5. Inorder Depth First"
142+
<< "\n6. Postorder Depth First";
136143

137-
cout << "\nEnter Your Choice : ";
138-
cin >> ch;
144+
std::cout << "\nEnter Your Choice : ";
145+
std::cin >> ch;
139146
int x;
140147
switch (ch) {
141-
case 1:
142-
cout << "\nEnter the value to be Inserted : ";
143-
cin >> x;
144-
Insert(root, x);
145-
break;
146-
case 2:
147-
cout << "\nEnter the value to be Deleted : ";
148-
cin >> x;
149-
Remove(root, root, x);
150-
break;
151-
case 3:
152-
BFT(root);
153-
break;
154-
case 4:
155-
Pre(root);
156-
break;
157-
case 5:
158-
In(root);
159-
break;
160-
case 6:
161-
Post(root);
162-
break;
148+
case 1:
149+
std::cout << "\nEnter the value to be Inserted : ";
150+
std::cin >> x;
151+
Insert(root, x);
152+
break;
153+
case 2:
154+
std::cout << "\nEnter the value to be Deleted : ";
155+
std::cin >> x;
156+
Remove(root, root, x);
157+
break;
158+
case 3:
159+
BFT(root);
160+
break;
161+
case 4:
162+
Pre(root);
163+
break;
164+
case 5:
165+
In(root);
166+
break;
167+
case 6:
168+
Post(root);
169+
break;
163170
}
164171
} while (ch != 0);
172+
173+
return 0;
165174
}

0 commit comments

Comments
 (0)