DreamsCoder.com

We Code your Dreams
home > data-structure programs

Simple code in C for Binary tree traversal: Preorder, Inorder and Postorder





Simple code in C for Binary tree traversal: Preorder, Inorder and  Postorder

#include 
#include 

typedef struct BST
{
    int data;
    struct BST *left;
    struct BST *right;
} node;

node *create();
void insert(node *, node *);
void preorder(node *);
void postorder(node *);
void inorder(node *);

int main()
{
    int ch;
    node *root = NULL, *temp, *current;
    printf("
Enter the number of Nodes you want :	");
    scanf("%d", &ch);
    printf("
Enter %d Nodes data :
", ch);
    do
    {
        temp = create();
        if (root == NULL)
            root = temp;
        else
            insert(root, temp);
        ch--;

    } while (ch != 0);

    printf("
Preorder Traversal
");
    preorder(root);

    printf("
Inorder Traversal
");
    inorder(root);

    printf("
Postorder Traversal
");
    postorder(root);

    printf("
");

    return 0;
}

node *create()
{
    node *temp;

    temp = (node *)malloc(sizeof(node));
    scanf("%d", &temp->data);
    temp->left = temp->right = NULL;
    return temp;
}

void insert(node *root, node *temp)
{
    if (root == NULL)
    {
        root = temp;
    }
    else
    {

        if (temp->data < root->data)
        {
            if (root->left != NULL)
                insert(root->left, temp);
            else
                root->left = temp;
        }

        if (temp->data > root->data)
        {
            if (root->right != NULL)
                insert(root->right, temp);
            else
                root->right = temp;
        }
    }
}

void preorder(node *root)
{
    if (root != NULL)
    {
        printf("%d 	", root->data);
        preorder(root->left);
        preorder(root->right);
    }
}

void postorder(node *root)
{
    if (root != NULL)
    {
        postorder(root->left);
        postorder(root->right);
        printf("%d 	", root->data);
    }
}

void inorder(node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d 	", root->data);
        inorder(root->right);
    }
}

Label - data-structure





Printing the singly linked list in reverse order using recursion in C





Printing the singly linked list in reverse order using recursion in C



#include 
#include 

struct node
{
    int data;
    struct node *next;
} *temp, *head, *curr;

void ReversePrint(struct node *head);
void RegularPrint(struct node *curr);
void add();

void add()
{
    int num;
    int data, i;
    printf("Enter number of nodes :");
    scanf("%d", &num);
    for (i = 0; i < num; i++)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        printf("Enter the data :");
        scanf("%d", &data);
        temp->data = data;
        if (i == 0)
        {
            head = temp;
            curr = head;
        }
        else
        {
            curr->next = temp;
            curr = temp;
        }
    }
    printf("
RegularPrintlaying in regular order");
    curr = head;
    RegularPrint(curr);
    printf("RegularPrintlaying in reverse order");
    ReversePrint(head);
}

void RegularPrint(struct node *curr)
{
    if (curr == NULL)
        return;
    printf("%d 	", curr->data);
    curr = curr->next;
    RegularPrint(curr);
}

void ReversePrint(struct node *head)
{
    if (head == NULL)
        return;
    ReversePrint(head->next);
    printf("%d 	", head->data);
}

int main()
{
    add();

    return 0;
}

Label - data-structure





Singly Link List program in C along with code - [Explained in Video]





 Singly Link List program in C along with code - [Explained in Video]


You can Download code here - 
http://dreamscoder.com/examples/singlyll.c
OR 
Click above on Source code

    #include
    #include

    struct node {
        int data;
        struct node *next;
    } *head, *temp, *curr, *left;

/* To add new node in Link list */

    void add(int num) {
        int i=0;
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = num;

            if(head == NULL) {
                head = temp;
                head->next = NULL;
            }
            else
            {
                temp->next = head;                
                head = temp;
            }

    }//add

/* To add new node in Link list after the
 specified location */

    void add_after(int num, int loc)
    {
        int i=0;
        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = num;
        curr = head;
            if(head == NULL) {
                    head = temp;
                    head->next = NULL;
            }
           else {
                for(i=0; inext;
                }

                left->next = temp;
                temp->next = curr;
            }//else
    }

/* To append new node in Link list */

    void append(int data) {

        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = data;
        curr = head;

        if(curr == NULL) {
            head = temp; 
            head->next = NULL;
        }
        
        while(curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = temp;
        curr = temp;
        curr->next = NULL;
    }

/* To delete node in Link list which contains the 
specified number */

    void delete(int num) {
        curr = head;

        if(curr == NULL) {
            printf("Link List is empty");
        }
        else
        {
            while(curr != NULL) {
               
                if(curr->data == num) {

                    if(curr == head) {
                    head = curr->next;
                    free(curr);
                    break;
                }
                    left->next = curr->next;
                    free(curr);
                    break;
                }
                else {
                    left = curr;
                    curr = curr->next;
                }
            } //while loop
        }
    }

/* To delete the entire Link list */

    void deleteAll() {
        curr = head;
        if(curr == NULL) {
            printf("Link list is empty");
        }
        else {
            while(curr != NULL) {
                left = curr->next;
                free(curr);
                curr = left;
            }
            head = NULL;
            printf("Deleted Link list");
        }
    }

/* To display all nodes in Link list */

    void disp() {
        if(head == NULL) {
            printf("Link list is empty");
            return;
        }
        else { 
            curr = head;
                 printf("Data is");
                while(curr != NULL) {
                    printf("%d	 ",curr->data);
                    curr = curr->next;
                } //while
            }//else
        }


    int main() {

        int choice;
        int num,loc;

        while(1)
        {
            printf("1. Add");
            printf("2. Add after");
            printf("3. Append");
            printf("4. Delete");
            printf("5. Delete All");
            printf("6. Display");
            printf("7. Exit");
            printf("---------------------");
            printf("Enter choice");
            scanf("%d",&choice);
             printf("");
            

        switch(choice) {
            case 1: printf("Enter data");
                    scanf("%d",&num);
                    add(num);
                    break;
            case 2: printf("Enter data and location ");
                    scanf("%d%d",&num,&loc);
                    add_after(num,loc);
                    break;
            case 3: printf("Enter the data");
                    scanf("%d",&num);
                    append(num);
                    break;
            case 4: printf("Enter the number to delete ");
                    scanf("%d",&num);
                    delete(num);
                    break;
            case 5: deleteAll();
                    break;
            case 6: disp();
                    break;
            case 7: return 0;
            
            default: printf("Incorrect choice");
                    break;

        }
    }//while

        return 0;
    }

Label - data-structure





Breadth first search in C Language



#include 
#include 
 
struct tree
{
    struct tree *left;
    int data;
    struct tree *right;
};
 
struct queue
{
    struct tree *ptr;
    struct queue *next;
};
 
 
void Append(struct tree **root, int data)
{
    if(*root == NULL )
    {
        *root = malloc(sizeof(struct tree));
        (*root)->data = data;
        (*root)->left = (*root)->right = NULL;
    }
    else
    if( data data )
        Append(&(*root)->left, data);
    else
        Append(&(*root)->right, data);
}
 
void Display(struct tree *temp)
{
    if(temp)
    {
        Display(temp->left);
        printf("%d,",temp->data);
        Display(temp->right);
    }
}
 
void AppendQ(struct queue **front,
 struct queue **end, struct tree *ptr)
{
    struct queue *newnode;
    newnode = malloc(sizeof(struct queue));
    newnode->ptr = ptr;
    newnode->next = NULL;
    if( *front == NULL )
        *front = *end = newnode;
    else
    {
        (*end)->next = newnode;
        *end = newnode;
    }
}
 
struct tree * Delete(struct queue **front)
{
    struct queue *temp = *front;
    struct tree *ptr = temp->ptr;
    *front = temp->next;
    free(temp);
    return ptr;
}
 
void BFS(struct tree *temp)
{
    struct queue *front, *end;
    front = end = NULL;
    if( temp )
        AppendQ(&front,&end,temp);
    while(front)
    {
            temp = Delete(&front);
            printf("%d,",temp->data);
            if( temp->left )
                AppendQ(&front,&end,temp->left);
            if( temp->right)
                AppendQ(&front,&end,temp->right);
    }
}
 
main()
{
    struct tree *root = NULL;
    Append(&root,35);
    Append(&root,20);
    Append(&root,18);
    Append(&root,22);
    Append(&root,48);
    Append(&root,50);
    Display(root);
    printf("
");
    BFS(root);
    printf("
");
}

Label - data-structure





Program To show whether expression have matching parenthesis or not using C++



/*Header File - dreamscoder.h */

#include
class stk
{
   char stack[100],ch;
   int top;
   public:
   stk()
   {
     top=-1;
   }
   void push (char ch)
   {
       stack[++top]=ch;
    }
    char pop()
    {

       return(stack[top--]);
    }
   int isempty()
    {
       if(top==-1)
       {
	// printf("\nStack empty");
	  return(1);
       }
	return(0);
    }
int    isfull()
    {
       if(top>99)
       {
	  printf("\nStack full");
	 return(0);
       }
       return(1);
    }
};
___________________________________________

/*Main Program*/

#include
#include
#include"dreamscoder.h"

void main()
{
        stack s;
        boolean flag;
        char exp[100],ch,ch1;
        int i;

        clrscr();

        cout << "\nEnter An Expression : ";
        cin  >> exp;

        flag = true;

        for (i=0 ; exp[i] != '\0' ;i++)
        {
                ch = exp[i];
                if (ch == '(' || ch == '['
 || ch == '{')
                	s.push(ch);
                else
                if (ch == ')')
                {
                        ch1 = s.ret_top();
                        if (ch1 != '(')
                        {
                        	flag = false;
                                break;
                        } // if
                        else
                        	s.pop(ch1);
                } // else if
                else
                if (ch == ']')
                {
                        ch1 = s.ret_top();
                        if (ch1 != '[')
                        {
                        	flag = false;
                                break;
                        } // if
                        else
                        	s.pop(ch1);
                } // else if
                else
                if (ch == '}')
                {
                	ch1 = s.ret_top();
                        if (ch1 != '{')
                        {
                        	flag = false;
                                break;
                        } // if
                        else
                        	s.pop(ch1);
                } // else if
        } // for

        if (s.isempty() && flag)
        	cout << "\nThe Expression 
Is Balanced.";
        else
        	cout << "\nThe Expression 
Is Not Balanced.";

} // main

Label - data-structure





next page >






Privacy Policy
Copyright © 2018 by DreamsCoder. All Rights Reserved.
DreamsCoder Google Plus DreamsCoder Facebook



Latest Technology,Tricks and Tips