-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
1634 lines (1352 loc) · 53.6 KB
/
Copy pathfunctions.cpp
File metadata and controls
1634 lines (1352 loc) · 53.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <limits>
#include <chrono>
#include <thread>
#include <fstream>
#include "functions.h"
#include "driver_code.h"
using std::cout;
using std::endl;
using std::cin;
using std::string;
using namespace std::chrono;
using namespace std::this_thread;
/***************************************************************************************************************************************/
// Base functions
/**
* Creates a new node with the given data and nullptr as left and right nodes.
*
* @param iData The data to be stored in the new node.
*
* @return A pointer to the new node.
*/
struct TreeNode* newTreeNode(int iData)
{
// memory allocation
struct TreeNode* newNode = (struct TreeNode*) malloc(sizeof(struct TreeNode));
// initialization
newNode->iData = iData;
newNode->ptrLeft = nullptr;
newNode->ptrRight = nullptr;
return newNode;
};
/**
* Traverses the tree in pre-order, printing it along the way.
*
* @param ptrStartingNode A pointer to the starting node of the tree.
*
* @return Void.
*/
void traversePreOrder(struct TreeNode* ptrStartingNode)
{
// if the tree is empty, return
if (ptrStartingNode == nullptr) return;
// if the tree is not empty, print current node and traverse its subtrees
else
{
cout << ptrStartingNode->iData << "\t";
traversePreOrder(ptrStartingNode->ptrLeft);
traversePreOrder(ptrStartingNode->ptrRight);
}
}
/**
* Inserts a new node with the given data into the tree.
*
* @param ptrRoot A pointer to the root node of the tree.
* @param iData The data to be stored in the new node.
*
* @return Void.
*/
void insertNode(struct TreeNode** ptrRoot, int iData)
{
if (*ptrRoot == nullptr)
{
// if the tree is empty, the inserted node becomes the root
(*ptrRoot) = newTreeNode(iData);
}
else if (iData < (*ptrRoot)->iData)
{
// if the inserted value is less than the root, insert it in the left subtree
insertNode(&((*ptrRoot)->ptrLeft), iData);
}
else
{
// if the inserted value is greater than the root, insert it in the right subtree
insertNode(&((*ptrRoot)->ptrRight), iData);
}
}
/**
* Deletes the entire tree from bottom up to avoid memory leakage.
*
* @param ptrRoot A pointer to the root node of the tree to be deleted.
*
* @return Void.
*/
void deleteTree(struct TreeNode** ptrRoot)
{
// Base case: If the tree is empty (root is nullptr), return
if (*ptrRoot == nullptr) return;
// Delete left and right subtrees
deleteTree(&(*ptrRoot)->ptrLeft);
deleteTree(&(*ptrRoot)->ptrRight);
// Delete the current node
free(*ptrRoot);
// Set the pointer to `nullptr`
*ptrRoot = nullptr;
}
/**
* Finds the smallest leaf of a tree.
*
* @param ptrRoot A pointer to the root node of the tree.
*
* @return A pointer to the smallest leaf of the tree.
*/
struct TreeNode* getSmallestChild(struct TreeNode* ptrRoot)
{
// if the tree is empty, return nullptr
if (ptrRoot == nullptr) return ptrRoot;
else
{
// traverse the left subtree until the smallest leaf is found
while (ptrRoot->ptrLeft != nullptr) ptrRoot = ptrRoot->ptrLeft;
return ptrRoot;
}
}
/**
* Deletes a node with the given data from the tree.
*
* @param ptrRoot A pointer to the root node of the tree.
* @param iData The data to be deleted from the tree.
*
* @return A pointer to the root node of the modified tree.
*/
struct TreeNode* deleteNode(struct TreeNode* ptrRoot, int iData)
{
// if the tree is empty, return nullptr
if (ptrRoot == nullptr) return nullptr;
// if the tree is not empty, traverse it
else if (iData < ptrRoot->iData) ptrRoot->ptrLeft = deleteNode(ptrRoot->ptrLeft, iData);
else if (iData > ptrRoot->iData) ptrRoot->ptrRight = deleteNode(ptrRoot->ptrRight, iData);
// if the node to be deleted is found
else if (iData == ptrRoot->iData)
{
struct TreeNode* ptrNewRoot = nullptr;
// if the node has no left child, replace it with its right child
if (ptrRoot->ptrLeft == nullptr)
{
ptrNewRoot = ptrRoot->ptrRight;
free(ptrRoot);
return ptrNewRoot;
}
// if the node has no right child, replace it with its left child
else if (ptrRoot->ptrRight == nullptr)
{
ptrNewRoot = ptrRoot->ptrLeft;
free(ptrRoot);
return ptrNewRoot;
}
// if the node has two children, replace it with the smallest child of its right subtree
else
{
// get the smallest child of the right subtree
struct TreeNode* ptrSuccessor = getSmallestChild(ptrRoot->ptrRight);
struct TreeNode* ptrNewRoot = newTreeNode(ptrSuccessor->iData);
// copy the left subtree of the node to be deleted
ptrNewRoot->ptrLeft = ptrRoot->ptrLeft;
// copy the right subtree, deleting the child from its original position
ptrNewRoot->ptrRight = deleteNode(ptrRoot->ptrRight, ptrNewRoot->iData);
free(ptrRoot);
return ptrNewRoot;
}
}
return ptrRoot;
}
/**
* Searches the tree for a node with the given data.
*
* @param ptrStartingNode A pointer from which to start the search.
* @param iData The data to be searched for.
*
* @return A pointer to the first node with the given data, or nullptr if not found.
*/
struct TreeNode* searchNode(struct TreeNode* ptrStartingNode, int iData)
{
// if the tree is empty, return nullptr
if (ptrStartingNode == nullptr) return nullptr;
// if the tree is not empty, traverse it, searching along the way
else if (iData == ptrStartingNode->iData) return ptrStartingNode;
else if (iData < ptrStartingNode->iData) return searchNode(ptrStartingNode->ptrLeft, iData);
else return searchNode(ptrStartingNode->ptrRight, iData);
}
/**
* Gets the height of the tree.
*
* @param ptrRoot A pointer to the root node of the tree.
*
* @return The height of the tree.
*/
int getHeight(struct TreeNode* ptrRoot)
{
// if the tree is empty, return 0
if (ptrRoot == nullptr) return 0;
// if the tree has no children, return 1
else if (ptrRoot->ptrLeft == nullptr && ptrRoot->ptrRight == nullptr) return 1;
// if the tree has children, return 1 + the height of the tallest subtree
else return (1 + std::max(getHeight(ptrRoot->ptrLeft), getHeight(ptrRoot->ptrRight)));
}
/**
* Gets the height of a node in the tree.
*
* @param ptrRoot A pointer to the root node of the tree.
*
* @return The height of the node.
*/
int getNodeHeight(struct TreeNode* ptrRoot, struct TreeNode* ptrNode)
{
// if the tree is empty, return 0
if (ptrRoot == nullptr) return 0;
// if the node is the root, return 1
else if (ptrRoot == ptrNode) return 1;
// if the node is in the left subtree, return 1 + the height of the node in the left subtree
else if (ptrNode->iData < ptrRoot->iData) return (1 + getNodeHeight(ptrRoot->ptrLeft, ptrNode));
// if the node is in the right subtree, return 1 + the height of the node in the right subtree
else return (1 + getNodeHeight(ptrRoot->ptrRight, ptrNode));
}
/**
* Gets the number of nodes in the tree.
*
* @param ptrRoot A pointer to the root node of the tree.
*
* @return The number of nodes in the tree.
*/
int getNumberOfNodes(struct TreeNode* ptrRoot)
{
// if the tree is empty, return 0
if (ptrRoot == nullptr) return 0;
// if the tree is not empty, return 1 + the number of nodes in its subtrees
else return (1 + getNumberOfNodes(ptrRoot->ptrLeft) + getNumberOfNodes(ptrRoot->ptrRight));
}
/**
* Checks if the tree is perfect.
*
* @param ptrRoot A pointer to the root node of the tree.
* @param iTreeHeight The height of the tree.
* @param iBranchHeight The height of the current branch.
*
* @return True if the tree is perfect, false otherwise.
*/
bool isPerfect(struct TreeNode* ptrRoot, int iTreeHeight, int iBranchHeight)
{
// if the tree is empty, return true
if (ptrRoot == nullptr) return true;
// if the tree has no children, return true if the branch height is equal to the tree height
else if (ptrRoot->ptrLeft == nullptr && ptrRoot->ptrRight == nullptr) return (iTreeHeight == iBranchHeight);
// if the tree has only one child, return false
else if (ptrRoot->ptrLeft == nullptr || ptrRoot->ptrRight == nullptr) return false;
// if the tree has two children, return true if both subtrees are perfect
else return (isPerfect(ptrRoot->ptrLeft, iTreeHeight, iBranchHeight + 1) && isPerfect(ptrRoot->ptrRight, iTreeHeight, iBranchHeight + 1));
}
/**
* Checks if the tree is complete.
*
* @param ptrRoot A pointer to the root node of the tree.
* @param iTreeHeight The height of the tree.
* @param iBranchHeight The height of the current branch.
*
* @return True if the tree is complete, false otherwise.
*/
bool isComplete(struct TreeNode* ptrRoot, int iTreeHeight, int iBranchHeight)
{
// if the tree is empty, return true
if (ptrRoot == nullptr) return true;
// if the tree has no children, return true if we are on the max depth or one level above it
else if (ptrRoot->ptrLeft == nullptr && ptrRoot->ptrRight == nullptr) return (iTreeHeight == iBranchHeight || iTreeHeight == iBranchHeight + 1);
// if the tree has a right child but no left child, return false
else if (ptrRoot->ptrLeft == nullptr && ptrRoot->ptrRight != nullptr) return false;
// if the tree has a left child but no right child, check if the left subtree is complete and if we are just above the max depth
else if (ptrRoot->ptrLeft != nullptr && ptrRoot->ptrRight == nullptr) return (isComplete(ptrRoot->ptrLeft, iTreeHeight, iBranchHeight + 1) && (iTreeHeight == iBranchHeight + 1));
// if the tree has two children, there are two possibilities:
// 1. the left subtree is perfect and the right subtree is complete
// 2. the left subtree is complete and the right subtree is perfect, but is one level shallower than the left subtree
else return ((isPerfect(ptrRoot->ptrLeft, iTreeHeight, iBranchHeight + 1) && isComplete(ptrRoot->ptrRight, iTreeHeight, iBranchHeight + 1)) ||
(isComplete(ptrRoot->ptrLeft, iTreeHeight, iBranchHeight + 1) && isPerfect(ptrRoot->ptrRight, iTreeHeight, iBranchHeight + 2)));
}
/***************************************************************************************************************************************/
// BFS functions
/**
* Creates a new node for the doubly linked list with the given data and nullptr as prev and next pointers.
*
* @param iData The data to be stored in the new node.
*
* @return A pointer to the new node.
*/
struct ListNode* newListNode(int iData)
{
// memory allocation
struct ListNode* newNode = (struct ListNode*) malloc(sizeof(struct ListNode));
// initialization
newNode->iData = iData;
newNode->ptrPrev = nullptr;
newNode->ptrNext = nullptr;
return newNode;
}
/**
* Inserts a new node with the given data into the doubly linked list.
*
* @param ptrHead A pointer to the head of the list.
* @param iData The data to be stored in the new node.
*
* @return A pointer to the head of the updated list.
*/
struct ListNode* insertNode(struct ListNode* ptrHead, int iData)
{
struct ListNode* newNode = newListNode(iData);
// if the list is empty, return the ListNode.
if (ptrHead == nullptr)
{
return newNode;
}
// if the node has no Next, add the newNode.
if (ptrHead->ptrNext == nullptr)
{
newNode->ptrPrev = ptrHead;
ptrHead->ptrNext = newNode;
}
else
{ // use recursion, passing the ptrHead->ptrNext as ptrHead.
ptrHead->ptrNext = insertNode(ptrHead->ptrNext, iData);
}
// return the ptrHead of the new list with the new node.
return ptrHead;
}
/**
* Prints the contents of the doubly linked list.
*
* @param ptrHead A pointer to the head of the list.
*
* @return Void.
*/
void printList(struct ListNode* ptrHead)
{
struct ListNode* ptrCurrent = ptrHead;
// if the list is empty.
if (ptrCurrent == nullptr)
{
cout << "Empty List" << endl;
}
else
{
while (ptrCurrent != nullptr)
{
// traverse the list by printing the nodes.
cout << ptrCurrent->iData << "\t";
ptrCurrent = ptrCurrent->ptrNext;
}
}
cout << endl;
}
/**
* Recursively inserts the nodes of a binary tree at the given level them to the doubly linked list.
*
* @param ptrHead A pointer to the head of the doubly linked list.
* @param ptrRoot A pointer to the root of the binary tree.
* @param iLevel The current level of the tree being processed.
*
* @return A pointer to the head of the updated doubly linked list.
*/
struct ListNode* insertCurrentLevel(struct ListNode* ptrHead, struct TreeNode* ptrRoot, int iLevel)
{
// if the level is empty
if (ptrRoot == nullptr)
{
return ptrHead;
}
// if the level is the first
if (iLevel == 1)
{
ptrHead = insertNode(ptrHead, ptrRoot->iData);
}
// if level is superior than 1
else if (iLevel > 1)
{
ptrHead = insertCurrentLevel(ptrHead, ptrRoot->ptrLeft, iLevel - 1);
ptrHead = insertCurrentLevel(ptrHead, ptrRoot->ptrRight, iLevel - 1);
}
// return the ptrHead
return ptrHead;
}
/**
* Creates a doubly linked list by inserting the nodes of a binary tree in breadth-first order.
*
* @param ptrRoot A pointer to the root of the binary tree.
*
* @return A pointer to the head of the created doubly linked list.
*/
struct ListNode* insertBFS(struct TreeNode* ptrRoot)
{
// initialize th head of the list as nullptr.
struct ListNode* ptrHead = nullptr;
// get the depth of the binary tree.
int iDepth = getHeight(ptrRoot);
for (int iLevel = 0; iLevel < iDepth; iLevel++)
{
// insert the nodes of the current level into the list.
ptrHead = insertCurrentLevel(ptrHead, ptrRoot, iLevel + 1);
}
// return the head of the list.
return ptrHead;
}
/**
* Prints the binary tree in breadth-first order.
*
* @param ptrRoot A pointer to the root of the binary tree.
*
* @return Void.
*/
void traverseBFS(struct TreeNode* ptrRoot)
{
// if the tree is empty.
if (ptrRoot == nullptr)
{
cout << "Empty Tree" << endl;
}
// get the depth of the tree.
int iDepth = getHeight(ptrRoot);
for (int iLevel = 0; iLevel < iDepth; iLevel++)
{
// print the nodes of the current level.
printCurrentLevel(ptrRoot, iLevel + 1);
cout << endl;
}
}
/**
* Calculates the length of a doubly linked list.
*
* @param ptrHead A pointer to the head of the doubly linked list.
*
* @return The length of the doubly linked list.
*/
int getLength(struct ListNode* ptrHead)
{
// initialization.
struct ListNode* ptrCurrent = ptrHead;
int iLength = 1;
// traverse the list until reaching the end.
while (ptrCurrent->ptrNext != nullptr)
{
// tranverse the list count the elements.
ptrCurrent = ptrCurrent->ptrNext;
iLength++;
}
// return the lenght of the list.
return iLength;
}
/**
* Retrieves a node from a linked list by its index.
*
* @param ptrHead A pointer to the head of the linked list.
* @param iIndex The index of the desired node (starting from 0).
*
* @return A pointer to the node at the specified index.
*/
struct ListNode* getNodeByIndex(struct ListNode* ptrHead, int iIndex)
{
// inizialization.
struct ListNode* ptrCurrent = ptrHead;
// go through the list until you find the index
for (int i = 0; i < iIndex; i++)
{
ptrCurrent = ptrCurrent->ptrNext;
}
// return the node of the index
return ptrCurrent;
}
/**
* Prints the nodes at the specified level of the binary tree.
*
* @param ptrRoot A pointer to the root of the binary tree.
* @param iLevel The current level of the tree being processed.
*
* @return Void.
*/
void printCurrentLevel(struct TreeNode* ptrRoot, int iLevel)
{
// if the node is empty return.
if (ptrRoot == nullptr)
{
return;
}
// if the node is the first.
if (iLevel == 1)
{
cout << ptrRoot->iData << "\t";
}
// if the level is greater than 1.
else if (iLevel > 1)
{
// let's go down to the children, first to the left child, second to the right child.
printCurrentLevel(ptrRoot->ptrLeft, iLevel - 1);
printCurrentLevel(ptrRoot->ptrRight, iLevel - 1);
}
}
/**
* Creates a new node for the queue.
*
* @param ptrTreeNode A pointer to the node of the binary tree.
*
* @return A pointer to the new node.
*/
struct QueueNode* newQueueNode(struct TreeNode* ptrTreeNode)
{
// Allocate memory for new node
struct QueueNode* ptrNewNode = (struct QueueNode*) malloc(sizeof(struct QueueNode));
// Assign data to this node
ptrNewNode->ptrTreeNode = ptrTreeNode;
ptrNewNode->ptrNext = nullptr;
return ptrNewNode;
}
/**
* Creates a new queue.
*
* @return A pointer to the new queue.
*/
struct Queue* newQueue()
{
// Allocate memory for new queue
struct Queue* ptrQueue = (struct Queue*) malloc(sizeof(struct Queue));
// Assign data to this queue
ptrQueue->ptrFront = nullptr;
ptrQueue->ptrRear = nullptr;
return ptrQueue;
}
/**
* Adds a node to the end of the queue.
*
* @param ptrQueue A pointer to the queue.
*
* @return Void.
*/
void enQueue(struct Queue* ptrQueue, struct TreeNode* ptrTreeNode)
{
// Create a new node
struct QueueNode* ptrNewNode = newQueueNode(ptrTreeNode);
// If queue is empty, then new node is front and rear both
if (ptrQueue->ptrRear == nullptr)
{
ptrQueue->ptrFront = ptrNewNode;
ptrQueue->ptrRear = ptrNewNode;
return;
}
// else, add new node at the end of queue and change rear
ptrQueue->ptrRear->ptrNext = ptrNewNode;
ptrQueue->ptrRear = ptrNewNode;
}
/**
* Removes the first node from the queue and returns it.
*
* @param ptrQueue A pointer to the queue.
*
* @return A pointer to the first node in the queue.
*/
struct TreeNode* deQueue(struct Queue* ptrQueue)
{
// If queue is empty, there is nothing to dequeue
if (ptrQueue->ptrFront == nullptr)
{
return nullptr;
}
// Store previous front and move front one node ahead
struct TreeNode* ptrTreeNode = ptrQueue->ptrFront->ptrTreeNode;
struct QueueNode* ptrTemp = ptrQueue->ptrFront;
ptrQueue->ptrFront = ptrQueue->ptrFront->ptrNext;
// if queue becomes empty, then change rear to nullptr as well
if (ptrQueue->ptrFront == nullptr)
{
ptrQueue->ptrRear = nullptr;
}
// Free memory and return the dequeued node
free(ptrTemp);
return ptrTreeNode;
}
/**
* Prints the binary tree in breadth-first order using a queue.
*
* @param ptrRoot A pointer to the root of the binary tree.
*
* @return Void.
*/
void efficientBFS(struct TreeNode* ptrRoot)
{
// If tree is empty, return
if (ptrRoot == nullptr)
{
cout << "Empty Tree" << endl;
return;
}
// Create an empty queue for level order traversal
struct Queue* ptrQueue = newQueue();
enQueue(ptrQueue, ptrRoot);
int iFrontHeight = getNodeHeight(ptrRoot, ptrQueue->ptrFront->ptrTreeNode);
int iCurrentHeight = iFrontHeight;
// Loop until queue is empty
while (ptrQueue->ptrFront != nullptr)
{
// Get and remove front of queue
struct TreeNode* ptrCurrent = deQueue(ptrQueue);
// Get height of current node
iCurrentHeight = getNodeHeight(ptrRoot, ptrCurrent);
// If the current node's height is greater than the previous' height, we have reached a new level
if (iCurrentHeight > iFrontHeight)
{
cout << endl;
cout << ptrCurrent->iData << "\t";
iFrontHeight = iCurrentHeight;
}
else
{
cout << ptrCurrent->iData << "\t";
}
// Enqueue left child
if (ptrCurrent->ptrLeft != nullptr)
{
enQueue(ptrQueue, ptrCurrent->ptrLeft);
}
// Enqueue right child
if (ptrCurrent->ptrRight != nullptr)
{
enQueue(ptrQueue, ptrCurrent->ptrRight);
}
}
cout << endl;
}
/***************************************************************************************************************************************/
// Sorting functions
/**
* Swaps two nodes in a doubly linked list.
* Assumes nodeA comes before nodeB
*
* @param ptrHead A pointer to the pointer to the head of the linked list.
* @param ptrNodeA A pointer to the first node to be swapped.
* @param ptrNodeB A pointer to the second node to be swapped.
*
* @return Void.
*/
void swapNodes(struct ListNode** ptrHead, struct ListNode* ptrNodeA, struct ListNode* ptrNodeB)
{
// rearrange the nexts of node A, node B, node A prev, and node B prev, we also need to change the prevs of node A, node B, node A next, and node B next.
// if NodeA is the head.
if (ptrNodeA == *ptrHead)
{
// if ptrNodeA->ptrNext is the NodeB.
if (ptrNodeA->ptrNext == ptrNodeB)
{
ptrNodeA->ptrNext = ptrNodeB->ptrNext;
// if NodeB is not the tail.
if (ptrNodeB->ptrNext != nullptr) ptrNodeB->ptrNext->ptrPrev = ptrNodeA;
// if NodeB is the tail.
ptrNodeA->ptrPrev = ptrNodeB;
ptrNodeB->ptrNext = ptrNodeA;
ptrNodeB->ptrPrev = nullptr;
(*ptrHead) = ptrNodeB;
}
// if ptrNodeA->ptrNext is not the NodeB.
else
{
// save the adjacent nodes of NodeB in the news ListNodes.
struct ListNode* ptrCurrentBPrev = ptrNodeB->ptrPrev;
struct ListNode* ptrCurrentBNext = ptrNodeB->ptrNext;
// rearrange nodes.
ptrNodeB->ptrNext = ptrNodeA->ptrNext;
ptrNodeB->ptrPrev = nullptr;
ptrCurrentBPrev->ptrNext = ptrNodeA;
if (ptrCurrentBNext != nullptr) ptrCurrentBNext->ptrPrev = ptrNodeA;
ptrNodeA->ptrNext->ptrPrev = ptrNodeB;
ptrNodeA->ptrNext = ptrCurrentBNext;
ptrNodeA->ptrPrev = ptrCurrentBPrev;
(*ptrHead) = ptrNodeB;
}
}
// NodeA is not the head.
else
{
// if ptrNodeA->ptrNext is the NodeB.
if (ptrNodeA->ptrNext == ptrNodeB)
{
ptrNodeA->ptrNext = ptrNodeB->ptrNext;
// if NodeB is not the tail.
if (ptrNodeB->ptrNext != nullptr) ptrNodeB->ptrNext->ptrPrev = ptrNodeA;
// if NodeB is the tail.
ptrNodeB->ptrPrev = ptrNodeA->ptrPrev;
ptrNodeA->ptrPrev->ptrNext = ptrNodeB;
ptrNodeB->ptrNext = ptrNodeA;
ptrNodeA->ptrPrev = ptrNodeB;
}
// if ptrNodeA->ptrNext is not the NodeB.
else
{
// save the adjacent nodes of NodeB and Node A in the news ListNodes.
struct ListNode* ptrCurrentAPrev = ptrNodeA->ptrPrev;
struct ListNode* ptrCurrentBPrev = ptrNodeB->ptrPrev;
struct ListNode* ptrCurrentANext = ptrNodeA->ptrNext;
struct ListNode* ptrCurrentBNext = ptrNodeB->ptrNext;
// rearrange the nodes prevs and nexts.
ptrNodeA->ptrPrev = ptrNodeB->ptrPrev;
ptrNodeB->ptrPrev = ptrCurrentAPrev;
ptrCurrentAPrev->ptrNext = ptrNodeB;
ptrCurrentBPrev->ptrNext = ptrNodeA;
ptrNodeA->ptrNext = ptrNodeB->ptrNext;
ptrNodeB->ptrNext = ptrCurrentANext;
ptrCurrentANext->ptrPrev = ptrNodeB;
// if NodeB is the tail.
if (ptrCurrentBNext != nullptr) ptrCurrentBNext->ptrPrev = ptrNodeA;
}
}
}
/**
* Sorts a doubly linked list using the selection sort algorithm.
*
* @param ptrHead A pointer to the pointer to the head of the linked list.
*
* @return Void.
*/
void selectionSort(struct ListNode** ptrHead)
{
// initialization.
struct ListNode* ptrCurrent = *ptrHead;
int iLength = getLength(*ptrHead);
for (int iIndex = 0; iIndex < iLength; iIndex++)
{
// ptrCurrent receive the index node.
ptrCurrent = getNodeByIndex(*ptrHead, iIndex);
// initialization, the "min" and "min candidate" being the current.
struct ListNode* ptrMin = ptrCurrent;
struct ListNode* ptrCandidate = ptrCurrent;
// traversing the successor nodes and comparing.
while (ptrCandidate != nullptr)
{
if (ptrMin->iData > ptrCandidate->iData)
{
// if candidate is smaller than min node, update the min node.
ptrMin = ptrCandidate;
}
// just candidate update.
ptrCandidate = ptrCandidate->ptrNext;
}
//if there is a smaller than the current, change the current with the min.
if (ptrMin != ptrCurrent)
{
swapNodes(ptrHead, ptrCurrent, ptrMin);
// Print the list vertically with a 0.5-second delay
printVertical(*ptrHead);
sleep_for(milliseconds(200));
system("clear");
}
}
}
/**
* Sorts a doubly linked list using the insertion sort algorithm.
*
* @param ptrHead A pointer to the pointer to the head of the linked list.
*
* @return Void.
*/
void insertionSort(struct ListNode** ptrHead)
{
// initialization.
struct ListNode* ptrCurrent1 = *ptrHead;
struct ListNode* ptrCurrent2 = *ptrHead;
int iLength = getLength(ptrCurrent1);
// growing loop, ptrCurrent1 is the index Node iOuterLoop.
for (int iOuterLoop = 1; iOuterLoop < iLength; iOuterLoop++)
{
// loop comparing with the previous ones and changing if there is a smaller one.
for (int iInnerLoop = iOuterLoop; iInnerLoop > 0; iInnerLoop--)
{
ptrCurrent1 = getNodeByIndex(*ptrHead, iInnerLoop);
ptrCurrent2 = getNodeByIndex(*ptrHead, iInnerLoop - 1);
if (ptrCurrent1->iData < ptrCurrent2->iData)
{
swapNodes(ptrHead, ptrCurrent2, ptrCurrent1);
// Print the list vertically with a 0.5-second delay
printVertical(*ptrHead);
sleep_for(milliseconds(200));
system("clear");
}
}
}
}
/**
* Sorts a doubly linked list using the shell sort algorithm.
*
* @param ptrHead A pointer to the pointer to the head of the linked list.
*
* @return Void.
*/
void shellSort(struct ListNode** ptrHead)
{
struct ListNode* ptrCurrent1 = *ptrHead;
int iLength = getLength(*ptrHead);
int iGap = 1;
// Knuth's sequence
while (iGap < iLength) iGap = 3 * iGap + 1;
// calculate the initial gap and updates the value of gap.
for (iGap = (iGap - 1) / 3; iGap > 0; iGap = (iGap - 1) / 3)
{
for (int iOuterLoop = iGap; iOuterLoop < iLength; iOuterLoop++)
{
for (int iInnerLoop = iOuterLoop; iInnerLoop >= iGap; iInnerLoop -= iGap)
{
ptrCurrent1 = getNodeByIndex(*ptrHead, iInnerLoop);
struct ListNode* ptrCurrent2 = getNodeByIndex(*ptrHead, iInnerLoop - iGap);
// comparisons between elements with distance gap
if (ptrCurrent1->iData < ptrCurrent2->iData)
{
swapNodes(ptrHead, ptrCurrent2, ptrCurrent1);
// Print the list vertically with a 0.5-second delay
printVertical(*ptrHead);
sleep_for(milliseconds(200));
system("clear");
}
}
}
}
}
/**
* Sorts a doubly linked list using the bubble sort algorithm.
*
* @param ptrHead A pointer to the pointer to the head of the linked list.
*
* @return Void.
*/
void bubbleSort(struct ListNode** ptrHead)
{
struct ListNode* ptrRoot = *ptrHead;
int iLength = getLength(*ptrHead);
bool bUnordered = true;
// loop to decrease where we left off the inner loop.
for (int iOuterLoop = 0; iOuterLoop < iLength; iOuterLoop++)
{
bUnordered = false;
for (int iInnerLoop = 0; iInnerLoop < iLength - 1 - iOuterLoop; iInnerLoop++)
{
// comparing two by two and switching when necessary
struct ListNode* ptrTempA = getNodeByIndex(*ptrHead, iInnerLoop);
struct ListNode* ptrTempB = getNodeByIndex(*ptrHead, iInnerLoop + 1);
if (ptrTempA->iData > ptrTempB->iData)
{
swapNodes(ptrHead, ptrTempA, ptrTempB);
bUnordered = true;
// Print the list vertically with a 0.5-second delay
printVertical(*ptrHead);
sleep_for(milliseconds(200));
system("clear");
}
}
// if we do not perform any changes in a loop, we have our ordered list
if (!bUnordered) break;
}
}
/**
* Prints a doubly linked list vertically with strings of 'O' characters representing each node
*
* @param ptrHead A pointer to the pointer to the head of the linked list.
*
* @return Void.
*/
void printVertical(struct ListNode* ptrHead) {
// If the head is nullptr, do nothing.
if (ptrHead == nullptr)
{
return;
}
// Iterate over the list printing the payload and the corresponding quantity of "O" characters
for (struct ListNode* ptrTemp = ptrHead; ptrTemp != nullptr; ptrTemp = ptrTemp->ptrNext)
{
cout << ptrTemp->iData << "\t";
for (int i = 0; i < ptrTemp->iData; i++)
{
cout << "O";
}
cout << endl;
}
}
/***************************************************************************************************************************************/
// Main loop of the program
/**