Monday, 21 September 2015

General Tree in c/c++ Source Code, c++, source code,

General Tree in c/c++ Source Code, c++, source code,
General Tree in c/c++ Source Code, 

// asiment general tree.cpp : Defines the entry point for the console application.
//
 More :> Link List source code, Dynamic Array Source Code







#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define SIZE 9

typedef struct treeNode
{
        int item;
        struct treeNode *left;
        struct treeNode *mid;
        struct treeNode *right;

}treeNode;


typedef int (*comparer)(int, int);

int compare(int a,int b)
{
    if(a < b)
        return 1;
    if(b<a<=b+5)
        return 2;
    if (a>b+5)
        return 3;
    return 0;
}
treeNode* create_node(int value)
{
    treeNode *new_node = (treeNode*)malloc(sizeof(treeNode));
    if(new_node == NULL)
    {
        fprintf (stderr, "Out of memory!!! (create_node)\n");
        exit(1);
    }
    new_node->item = value;

    new_node->left = NULL;
    new_node->mid = NULL;
    new_node->right = NULL;
    return new_node;
}
typedef int (*comparer)(int, int);
void insertNode(treeNode *root,comparer compare,int item)
{
if (root == NULL){
       root = create_node(item);
    }

   else{
        int is_left = 0;
        int is_mid = 0;
        int is_right = 0;
        int r;
        treeNode* cursor = root;
        treeNode* prev = NULL;
        while(cursor != NULL){
            r = compare(item,cursor->item);
            prev = cursor;
            if (r==1){
                is_left = 1;
                cursor = cursor->left;
            }else if(r==2){
                is_mid = 1;
                cursor = cursor->mid;
            }else if(r==3){
                is_right = 1;
                cursor = cursor->right;
            }
            }
        if(is_left)
           prev->left = create_node(item);
         else if(is_mid)
           prev->mid = create_node(item);
         else if(is_right)
           prev->right = create_node(item);

    else{
        printf("duplicate values are not allowed!");
    }
    return root;
}
treeNode * search(treeNode *root,const int item,comparer compare)
{
 if (root == NULL)
    return NULL;

 int r;
 treeNode* cursor = root;
 while (cursor!=NULL){
    r=compare(item,cursor->item);
    if(r==1)
        cursor = cursor->left;
    else if(r == 2)
        cursor = cursor->mid;
    else if(r == 3)
        cursor = cursor->right;
 }
 return cursor;

}
              //delete a node
treeNode* delete_node(treeNode* root,int item,comparer compare)
{
    if(root==NULL)
        return NULL;
    treeNode *cursor;
    int r = compare(item,root->item);
    if(r==1)
        root->left = delete_node(root->left,item,compare);
    else if(r==2)
        root->mid = delete_node(root->mid,item,compare);
    else if(r==3)
        root->right = delete_node(root->right,item,compare);
    else{
        if(root->left == NULL && root->mid == NULL)
            {
            cursor = root->right;
            free(root);
            root = cursor;
            }
        else if(root->left == NULL && root->right==NULL){
            cursor = root->mid;
            free(root);
            root = cursor;
        }
        else if(root->right == NULL && root->mid == NULL){
            cursor = root->left;
            free(root);
            root = cursor;
        }

    }

  return root;
}
void dispose(treeNode* root)
{
    if(root != NULL)
    {
        dispose(root->left);
        dispose(root->mid);
        dispose(root->right);
        free(root);
    }
}

void display(treeNode* nd)
{
    if(nd != NULL)
        printf("%d ",nd->item);
}
void display_tree(treeNode* nd)
{
    if (nd == NULL)
        return;
    /* display node item */
    printf("%d",nd->item);
    if(nd->left != NULL)
        printf("(L:%d)",nd->left->item);
    if(nd->mid != NULL)
        printf("(R:%d)",nd->mid->item);
    if(nd->right != NULL)
        printf("(R:%d)",nd->right->item);
    printf("\n");

    display_tree(nd->left);
    display_tree(nd->mid);
    display_tree(nd->right);
}


int main(void)
{
    treeNode* root = NULL;
    comparer int_comp = compare;



    int a[SIZE] = {8,3,10,1,6,14,4,7,13,15,2,5,20};
    int i;
    printf("--- C Binary Search Tree ---- \n\n");
    printf("Insert: ");
    for(i = 0; i < SIZE; i++)
    {
        printf("%d ",a[i]);
        root = insert_node(root,int_comp,a[i]);
    }
    printf(" into the tree.\n\n");

    /* display the tree */
    display_tree(root);

    /* remove element */
int r;
    do
    {
        printf("Enter data to remove, (-1 to exit):");
        scanf("%d",&r);
        if(r == -1)
            break;
        root = delete_node(root,r,int_comp);
        /* display the tree */
        if(root != NULL)
            display_tree(root);
        else
            break;
    }
    while(root != NULL);
    /* search for a node */

    int key = 0;
    treeNode* s;
    while(key != -1)
    {
        printf("Enter item to search (-1 to exit):");
        scanf("%d",&key);

        s = search(root,key,int_comp);
        if(s != NULL)
        {
            printf("Found it %d",s->item);
            if(s->left != NULL)
                printf("(L: %d)",s->left->item);
            if(s->mid != NULL)
                printf("(R: %d)",s->mid->item);
            if(s->right != NULL)
                printf("(R: %d)",s->right->item);
            printf("\n");
        }
        else
        {
            printf("node %d not found\n",key);
        }
    }

    /* remove the whole tree */
    dispose(root);
    return 0;
}
}


Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com

useful tutorial keep it up...
EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)

LinkWithin