Thread: Company employees (using binary trees)

  1. #1
    Registered User lailou's Avatar
    Join Date
    Dec 2013
    Posts
    1

    Company employees (using binary trees)

    Good evening everyone.

    My colleague and I have been given a task to create a list with the employes of a company using binary trees. Each employee/node has to contain the following information:

    - Employe name
    - Rank
    - ID Number
    - monthly Salary

    We have to write a C program (using Codeblocks) which builds the company tree with the information above and afterwards lists all the employes who have a salary bigger then a number specified by the user.

    So far so good. We've had a look on the examples on the internet and after finding one which looked friendly enough we started working on that one to suit our needs. Credits for the original source code to: C Program to Implement Binary Search Tree Traversal - Basic C Programs | C Programming Examples

    We've reached the point where we're able to insert the required information but being able to list all the employes who's salary is bigger then the number given by the user seems to be rather difficult to achieve. I'll include the code next, any kind of help will be greatly appreciated. We have left almost the entire pieces of the original code and we are aware that the are still things which are not required for our assignment, which only requires to enter the 4 information above and afterwards display the employes with the before mentioned condition concerning their salary.

    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <stdlib.h>
    
    typedef struct BST
    {
        int data;
    
        struct BST *lchild,*rchild;
    }node;
    
    char name;
    char position;
    int id;
    int salary;
    
    void insert(node *,node *);
    void inorder(node *);
    void preorder(node *);
    void postorder(node *);
    node *search(node *,int,node **);
    
    void main()
    {
     int choice;
     char ans='N';
     int key;
     node *new_node,*root,*tmp,*parent;
     node *get_node();
     root=NULL;
    
     printf("\nEmployees list \n");
     do
     {
       printf("\n1.Create");
       printf("\n2.Search");
       printf("\n3.Recursive Traversals");
       printf("\n4.Exit");
       printf("\n\nEnter your choice: ");
       scanf("%d",&choice);
    
       switch(choice)
       {
        case 1:
               do
                 {
                 new_node=get_node();
    
                 printf("\nInformation about employe with number: ");
                 scanf("%d",&new_node->data);
                 printf("\n    Name: ");
                 scanf("%s",&name);
                 printf("\n    Rank: ");
                 scanf("%s",&position);
                 printf("\n    Id. Number: ");
                 scanf("%d",&id);
                 printf("\n    Monthly salary: ");
                 scanf("%d",&salary);
    
                 if(root==NULL)   /* Tree is not Created */
                     root=new_node;
                 else
                     insert(root,new_node);
    
                 printf("\nDo you wish to enter a new employe?(y/n)");
                 ans=getch();
    
                 }while(ans=='y');
    
                 break;
    
         case 2:
                 printf("\nInsert the employe number you're looking for: ");
                 scanf("%s",&key);
    
                 tmp = search(root,key,&parent);
    
                 printf("\nParent of node %d is %d",
                                  tmp->data,parent->data);
                 break;
    
        case 3:
    
                if(root==NULL)
                    printf("Tree Is Not Created");
                else
                   {
                   printf("\nThe Inorder display: ");
                   inorder(root);
                   printf("\nThe Preorder display: ");
                   preorder(root);
                   printf("\nThe Postorder display: ");
                   postorder(root);
                   }
    
                break;
        }
     }while(choice!=4);
    }
    /*
      Get new Node
    */
    node *get_node()
     {
     node *temp;
     temp=(node *)malloc(sizeof(node));
     temp->lchild=NULL;
     temp->rchild=NULL;
     return temp;
     }
    /*
      This function is for creating a binary search tree
    */
    void insert(node *root,node *new_node)
    {
      if(new_node->data < root->data)
         {
         if(root->lchild==NULL)
             root->lchild = new_node;
         else
             insert(root->lchild,new_node);
         }
    
      if(new_node->data > root->data)
         {
         if(root->rchild==NULL)
             root->rchild=new_node;
         else
             insert(root->rchild,new_node);
         }
    }
    /*
    This function is for searching the node from
          binary Search Tree
    */
    node *search(node *root,int key,node **parent)
    {
     node *temp;
     temp=root;
        while(temp!=NULL)
        {
          if(temp->data==key)
             {
             printf("\n Employe with number %d is present",temp->data);
             return temp;
             }
          *parent=temp;
    
          if(temp->data>key)
             temp=temp->lchild;
          else
             temp=temp->rchild;
        }
     return NULL;
    }
    /*
    This function displays the tree in inorder fashion
    */
    void inorder(node *temp)
    {
       if(temp!=NULL)
        {
        inorder(temp->lchild);
        printf("%d",temp->data);
        inorder(temp->rchild);
        }
    }
    /*
    This function displays the tree in preorder fashion
    */
    void preorder(node *temp)
    {
     if(temp!=NULL)
        {
        printf("%d",temp->data);
        preorder(temp->lchild);
        preorder(temp->rchild);
        }
    }
    
    /*
    This function displays the tree in postorder fashion
    */
    void postorder(node *temp)
    {
     if(temp!=NULL)
        {
        postorder(temp->lchild);
        postorder(temp->rchild);
        printf("%d",temp->data);
        }
    }
    Last edited by lailou; 12-05-2013 at 07:30 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got lots of "if" statements in your code as it stands; surely, you can figure out how to write an if statement that only prints when the salary is greater than a given number.

    And you've got three ways of traveling through the entire tree at your disposal, so choose one of them to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  2. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  3. binary trees
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2004, 06:44 AM
  4. Binary Trees
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 04:55 PM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM

Tags for this Thread