In C I have a problem with BSTs printTree in this function

In C , I have a problem with BSTs printTree, in this function, how could it print out the whole tree without putting the entire function into a loop?

void printTree(Tree t, int depth) { if (t != NULL) { depth++; printTree(t->left, depth); int i; for (i-1: ivalue); printTree (t->right, depth);

Solution

#include <stdio.h>

#include <stdlib.h>

struct node

{

                int data;

                struct node *left, *right;

};

void swap( int* a, int* b )

{

                int t = *a;

                *a = *b;

                *b = t;

}

struct node* newNode(int data)

{

                struct node* node = (struct node *)malloc(sizeof(struct node));

                node->data = data;

                node->left = NULL;

                node->right = NULL;

                return(node);

}

void correctBSTUtil( struct node* root, struct node** first,

                                                                                struct node** middle, struct node** last,

                                                                                struct node** prev )

{

                if( root )

                {

                                correctBSTUtil( root->left, first, middle, last, prev );

                                                                if (*prev && root->data < (*prev)->data)

                                {

                                               

                                                if ( !*first )

                                                {

                                                                *first = *prev;

                                                                *middle = root;

                                                }

                                                                else

                                                                *last = root;

                                }

                                *prev = root;

                                correctBSTUtil( root->right, first, middle, last, prev );

                }

}

void correctBST( struct node* root )

{

                struct node *first, *middle, *last, *prev;

                first = middle = last = prev = NULL;

                correctBSTUtil( root, &first, &middle, &last, &prev );

                if( first && last )

                swap( &(first->data), &(last->data) );

                else if( first && middle )

                                swap( &(first->data), &(middle->data) );

                }

void printInorder(struct node* node)

{

                if (node == NULL)

                                return;

                printInorder(node->left);

                printf(\"%d \", node->data);

                printInorder(node->right);

}

int main()

{

               

                struct node *root = newNode(6);

                root->left = newNode(10);

                root->right               = newNode(2);

                root->left->left = newNode(1);

                root->left->right = newNode(3);

                root->right->right = newNode(12);

                root->right->left = newNode(7);

                printf(\"Inorder Traversal of the original tree \ \");

                printInorder(root);

                correctBST(root);

                printf(\"\ Inorder Traversal of the fixed tree \ \");

                printInorder(root);

                return 0;

}

In C , I have a problem with BSTs printTree, in this function, how could it print out the whole tree without putting the entire function into a loop? void print
In C , I have a problem with BSTs printTree, in this function, how could it print out the whole tree without putting the entire function into a loop? void print
In C , I have a problem with BSTs printTree, in this function, how could it print out the whole tree without putting the entire function into a loop? void print

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site