Skip to content

Commit 7f875f8

Browse files
author
Raven G. Duran
committed
Refactored code and Updated documentation
1 parent 3dfe97d commit 7f875f8

File tree

3 files changed

+159
-98
lines changed

3 files changed

+159
-98
lines changed

avl.c

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ struct node {
1515
struct node *right;
1616
} *tRoot = NULL, *tLeaf = NULL;
1717

18+
// Menu functions
19+
void showMenu();
20+
void doInsert();
21+
void doDelete();
22+
void doSearch();
23+
void doInOrder();
24+
1825
// Functions for creating a BST structure
1926
struct node* newNode(int value,struct node *parent);
2027
struct node* insertN(struct node *root,int value,struct node *parent);
@@ -36,13 +43,6 @@ struct node* findMax(struct node *root);
3643
int getHeight(struct node *root); // Note: This is a generic getHeight function if you want
3744
// to do as ask by Ma'am Raboy for the project to have getHeight of root, left and right
3845
// then just use these: getHeight(root), getHeight(root->left), getHeight(root->right)
39-
// Menu functions
40-
void showMenu();
41-
void doInsert();
42-
void doDelete();
43-
void doSearch();
44-
void doInOrder();
45-
4646
// InOrder print
4747
void inOrder(struct node *root);
4848

@@ -214,99 +214,8 @@ struct node* insertN(struct node *root,int value,struct node *parent){
214214
}
215215
}
216216

217-
struct node* searchNode(struct node *root, int value){
218-
if (root == NULL) return NULL;
219-
else {
220-
if (root->value == value) return root;
221-
else {
222-
if (value < root->value) return searchNode(root->left,value);
223-
else return searchNode(root->right,value);
224-
}
225-
}
226-
}
227-
228-
int getHeight(struct node *root){
229-
if (root==NULL)
230-
return 0;
231-
else
232-
{
233-
/* compute the depth of each subtree */
234-
int lDepth = getHeight(root->left);
235-
int rDepth = getHeight(root->right);
236-
237-
/* use the larger one */
238-
if (lDepth > rDepth)
239-
return(lDepth+1);
240-
else return(rDepth+1);
241-
}
242-
}
243-
244-
struct node* findMin(struct node *root){
245-
// Go to left most part to get the minimum
246-
while (root->left != NULL) {
247-
root = root->left;
248-
}
249-
250-
return(root);
251-
}
252-
253-
struct node* findMax(struct node *root){
254-
// Go to right most part to get the maximum
255-
while (root->right != NULL) {
256-
root = root->right;
257-
}
258-
259-
return(root);
260-
}
261-
262-
void inOrder(struct node *root){
263-
if (root == NULL) return;
264-
else {
265-
inOrder(root->left);
266-
printf("~%d~\n",root->value);
267-
inOrder(root->right);
268-
}
269-
}
270-
271-
void bFactorInorder(struct node *root){
272-
if (root == NULL) return;
273-
else {
274-
bFactorInorder(root->left);
275-
bFactorInorder(root->right);
276-
277-
int rHeight = getHeight(root->right);
278-
int lHeight = getHeight(root->left);
279-
int bFactor = abs(rHeight - lHeight);
280-
printf("~Node %d~ \t lHeight: %d \t rHeight: %d \t Balance Factor: %d\n",root->value,lHeight,rHeight,bFactor);
281-
}
282-
}
283-
284217
// AVL Functions - As AVL tree is derived from Binary Search Trees
285218

286-
void balanceCheck(struct node *root){
287-
if ( getHeight(root->left) > (getHeight(root->right) + 1) ){
288-
printf("Imbalance! The Right sub-tree of node %d is taller!\n",root->value);
289-
if ( getHeight(root->left->right) > (getHeight(root->left->left)) ){
290-
printf("Rotating using LR Rotation...\n");
291-
rotateLR(root);
292-
} else {
293-
printf("Rotating using LL Rotation...\n");
294-
rotateLL(root);
295-
}
296-
297-
} else if ( getHeight(root->right) > (getHeight(root->left) + 1) ){
298-
printf("Imbalance! The Right sub-tree of node %d is taller!\n",root->value);
299-
300-
if ( getHeight(root->right->left) > (getHeight(root->right->right)) ){
301-
printf("Rotating using RL Rotation...\n");
302-
rotateRL(root);
303-
} else {
304-
printf("Rotating using RR Rotation...\n");
305-
rotateRR(root);
306-
}
307-
}
308-
}
309-
310219
void rotateRL(struct node *root){
311220
struct node *a = root;
312221
struct node *b = root->right;
@@ -396,6 +305,43 @@ void rotateLL(struct node *root){
396305
correctParents(a,b,c,f);
397306
}
398307

308+
void balanceCheck(struct node *root){
309+
if ( getHeight(root->left) > (getHeight(root->right) + 1) ){
310+
printf("Imbalance! The Right sub-tree of node %d is taller!\n",root->value);
311+
if ( getHeight(root->left->right) > (getHeight(root->left->left)) ){
312+
printf("Rotating using LR Rotation...\n");
313+
rotateLR(root);
314+
} else {
315+
printf("Rotating using LL Rotation...\n");
316+
rotateLL(root);
317+
}
318+
319+
} else if ( getHeight(root->right) > (getHeight(root->left) + 1) ){
320+
printf("Imbalance! The Right sub-tree of node %d is taller!\n",root->value);
321+
322+
if ( getHeight(root->right->left) > (getHeight(root->right->right)) ){
323+
printf("Rotating using RL Rotation...\n");
324+
rotateRL(root);
325+
} else {
326+
printf("Rotating using RR Rotation...\n");
327+
rotateRR(root);
328+
}
329+
}
330+
}
331+
332+
void bFactorInorder(struct node *root){
333+
if (root == NULL) return;
334+
else {
335+
bFactorInorder(root->left);
336+
bFactorInorder(root->right);
337+
338+
int rHeight = getHeight(root->right);
339+
int lHeight = getHeight(root->left);
340+
int bFactor = abs(rHeight - lHeight);
341+
printf("~Node %d~ \t lHeight: %d \t rHeight: %d \t Balance Factor: %d\n",root->value,lHeight,rHeight,bFactor);
342+
}
343+
}
344+
399345
void correctParents(struct node *a,struct node *b,struct node *c,struct node *f){
400346
if (a->left != NULL) a->left->parent = a;
401347
if (a->right != NULL) a->right->parent = a;
@@ -407,3 +353,57 @@ void correctParents(struct node *a,struct node *b,struct node *c,struct node *f)
407353
if (f != NULL) f->right->parent = f;
408354
}
409355

356+
struct node* searchNode(struct node *root, int value){
357+
if (root == NULL) return NULL;
358+
else {
359+
if (root->value == value) return root;
360+
else {
361+
if (value < root->value) return searchNode(root->left,value);
362+
else return searchNode(root->right,value);
363+
}
364+
}
365+
}
366+
367+
int getHeight(struct node *root){
368+
if (root==NULL)
369+
return 0;
370+
else
371+
{
372+
/* compute the depth of each subtree */
373+
int lDepth = getHeight(root->left);
374+
int rDepth = getHeight(root->right);
375+
376+
/* use the larger one */
377+
if (lDepth > rDepth)
378+
return(lDepth+1);
379+
else return(rDepth+1);
380+
}
381+
}
382+
383+
struct node* findMin(struct node *root){
384+
// Go to left most part to get the minimum
385+
while (root->left != NULL) {
386+
root = root->left;
387+
}
388+
389+
return(root);
390+
}
391+
392+
struct node* findMax(struct node *root){
393+
// Go to right most part to get the maximum
394+
while (root->right != NULL) {
395+
root = root->right;
396+
}
397+
398+
return(root);
399+
}
400+
401+
void inOrder(struct node *root){
402+
if (root == NULL) return;
403+
else {
404+
inOrder(root->left);
405+
printf("~%d~\n",root->value);
406+
inOrder(root->right);
407+
}
408+
}
409+

avl.exe

0 Bytes
Binary file not shown.

documentation.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ So I decided to document just the functions in a way you'll understand how it wo
99
the rest for you to debug and understand, this is a good way to really understand what's going on
1010
the code. (Don't worry, almost everything is discussed) - Raven G. Duran
1111

12+
#### Main Program flow
13+
```C
14+
void showMenu();
15+
void doInsert();
16+
void doDelete();
17+
void doSearch();
18+
void doInOrder();
19+
```
20+
These are program specific functions use in directing our porgram flow.
21+
* showMenu() : Shows the menu with waits for the user to enter a menu choice.
22+
* doInsert() : A menu function that will handle the insertion process of the AVL tree.
23+
* doDelete() : A menu function that will handle the deletion process of the AVL tree.
24+
* doInorder() : A menu function that will handle the printing of the tree in order traversal.
25+
1226
#### Node Structure
1327
```C
1428
struct node {
@@ -96,3 +110,50 @@ The balance factor is calculated as: abs(heightLeft - heightRight). abs means ge
96110
This function is a bit trivial. It just corrects the pointer to parent for each and every
97111
affected nodes in the rotation. (Parents of A, B , C, and F)
98112

113+
#### Helper functions
114+
```C
115+
struct node* searchNode(struct node *root, int value); // Return NULL if not found or Pointer to node if found
116+
struct node* findMin(struct node *root);
117+
struct node* findMax(struct node *root);
118+
int getHeight(struct node *root);
119+
```
120+
Helper functions are used in bigger functions to make it work. These functions are of big help
121+
in structuring the AVL tree.
122+
123+
* searchNode : Searches a node base by the value given to it.
124+
Returns (or equal to) NULL if the value is not found.
125+
Returns (or equal to) the pointer to that node if it is found. Parameters:
126+
1. root : Root of the tree you want to search the node on.
127+
2. value : The value you want to search for.
128+
* findMin : Finds the minimum number in the given tree. (Gets the successor) The paremeter is the root.
129+
* findMax : Finds the maximum number in the given tree. (Gets the predecessor) The parameter is the root.
130+
* getHegiht : This function gets the height of a given tree and return that in integer. The paremeter is the root.
131+
132+
#### InOrder
133+
```C
134+
void inOrder(struct node *root);
135+
```
136+
This is just a trivial function that in Orderly traverse a given tree.
137+
In Orderly traverse means that the whole tree will be traversed in ascending order.
138+
If the node it puts are arrange correctly (increasing), then that means our solution worked!
139+
140+
#### Finally
141+
######Comments? Suggestions? Problems?
142+
You can place an issue (by going to the Issues tab above) so that I could get to the problem more easily.
143+
You could also send me an email if anything bugs you at: [email protected]
144+
Or if you're an MUST student, you can ask for help personally at school.
145+
146+
Hope it helped :)
147+
148+
149+
P.S. : If you have a better algorithm or if there is a part in my solution that need
150+
to be refactored, then feel free to make a pull request! :)
151+
152+
153+
(Bisaya)
154+
Sa akong mga amigo nga nakakita ani nga solution : Walay libre dira? HAHA xD
155+
k-thnks-bye
156+
157+
158+
159+

0 commit comments

Comments
 (0)