Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7923115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:12:00+00:00 2026-06-03T17:12:00+00:00

I have specific confusion in implementing function within the existing binary tree that stores

  • 0

I have specific confusion in implementing function within the existing binary tree that stores pets’ names and kinds, first what I’ve done:

Declarations[tree.h]:

typedef struct item
{
    char petname[20];
    char petkind[20];
} Item;

#define MAXITEMS 10

typedef struct node
{
    Item item;
    struct node * left;    /* pointer to right branch  */
    struct node * right;   /* pointer to left branch   */
} Node;

typedef struct tree
{
    Node * root;           /* pointer to root of tree  */
    int size;              /* number of items in tree  */
} Tree;


void InitializeTree(Tree *ptree);

bool TreeIsEmpty(const Tree * ptree);

bool TreeIsFull(const Tree * ptree);

int TreeItemCount(const Tree * ptree);

bool AddItem(const Item * pi, Tree * ptree);

bool InTree(const Item * pi, const Tree * ptree);

bool DeleteItem(const Item * pi, Tree * ptree);

void Traverse (const Tree * ptree, void (* pfun)(Item item));

void DeleteAll(Tree * ptree);

Functions for adding nodes:

typedef struct pair {
    Node * parent;
    Node * child;
} Pair;


bool AddItem(const Item * pi, Tree * ptree)
{
     Node * new_node;

     if(TreeIsFull(ptree))
     {
          fprintf(stderr,"Tree is full\n");
          return false;
     }


          if(SeekItem(pi,ptree).child!=NULL)
          {
               fprintf(stderr,"Attempted to add duplicate item\n");
               return false;
          }

     new_node=MakeNode(pi);

     if(new_node==NULL)
     {
          fprintf(stderr,"Couldn't create node\n");
          return false;
     }

     ptree->size++;

     if(ptree->root==NULL)
          ptree->root=new_node;
     else
          AddNode(new_node,ptree->root);

     return true;
}

static void AddNode(Node * new_node, Node * root)
{
     if((strcmp(new_node->item.petname, root->item.petname))==0)
     {
          if(root->same==NULL)
               root->same=new_node;
          else
               AddNode(new_node, root->same);
     }
     else
     {
          if(ToLeft(&new_node->item,&root->item))
          {
               if(root->left==NULL)
                    root->left=new_node;
               else
                    AddNode(new_node, root->left);
          }
          else if(ToRight(&new_node->item,&root->item))
          {
               if(root->right==NULL)
                    root->right=new_node;
               else
                    AddNode(new_node, root->right);
          }
          else
          {
               fprintf(stderr,"location error in AddNode()\n");
               exit(1);
          }
     }
}

static bool ToLeft(const Item * i1, const Item * i2)
{
     int comp;

     if((comp=strcmp(i1->petname,i2->petname))<0)
          return true;
     else if(comp==0)
          return true;
     else
          return false;
} 

static bool ToRight(const Item * i1, const Item * i2)
{
     int comp;

     if((comp=strcmp(i1->petname,i2->petname))>0)
          return true;
     else if(comp==0)
          return true;
     else
          return false;
}

static Node * MakeNode(const Item * pi)
{
     Node * new_node;

     new_node=(Node *) malloc(sizeof Node);

     if(new_node!=NULL)
     {
          new_node->item=*pi;
          new_node->left=NULL;
          new_node->right=NULL;
          new_node->same=NULL;
     }
     return new_node;
}

(If you need more code I’ll post it since there are more functions)
The main confusion is how would I add all the pets with same names (different kinds) in the list within the same node and then find them just through typing the petname while retrieving their kind

Original task:
Modify the Pet Club program so that all pets with the same name are stored in a list in the same node. When the user chooses to find a pet, the program should request the pet name and then list all pets (along with their kinds) having that name.

Suggestion from the book:
*

For another possible variation, consider the Nerfville Pet Club. The
example ordered the tree by both name and kind, so it could hold Sam
the cat in one node, Sam the dog in another node, and Sam the goat in
a third node. You couldn’t have two cats called Sam, however. Another
approach is to order the tree just by name. Making that change alone
would allow for only one Sam, regardless of kind, but you could then
define Item to be a list of structures instead of being a single
structure. The first time a Sally shows up, the program would create a
new node, then create a new list, and then add Sally and her kind to
the list. The next Sally that shows up would be directed to the same
node and added to the list.

*

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T17:12:04+00:00Added an answer on June 3, 2026 at 5:12 pm

    You should already know about linked lists. Combine that knowledge with the tree you have here. Move the Item to a linked list and make Node store the list instead of Item directly.

    typedef struct itemlistElement
    {
        Item item;
        struct itemlistElement* nextItem;    /* pointer to next item on list */
    } ItemlistElement;
    
    typedef struct node
    {
        ItemlistElement* listOfItems; /* pointer to first element of a linked list of Item */
        struct node * left;    /* pointer to right branch  */
        struct node * right;   /* pointer to left branch   */
    } Node;
    

    You can figure out the rest – every time you traverse a tree, you’ll have extra step traversing the list. When adding an item, there will be 2 possibilities: either add new node with one item OR to add the item into existing node.
    That’s exactly what your book said:

    (…) you could then define Item to be a list of structures instead of
    being a single structure. The first time a Sally shows up, the program
    would create a new node, then create a new list, and then add Sally
    and her kind to the list. The next Sally that shows up would be
    directed to the same node and added to the list.

    First: create the list and make it work. Practice first on a separate ItemlistElement* (outside of the tree, you can even make the list and list traversal functions in another program).
    Then, modify your program to store Item in the lists, but using one-element list always. This should be very easy.
    Last move is to combine them both together. It’s the step with least coding, but is most challenging. Do all the thinking before typing. Make copies of both projects while they still work (tree and linked list) to keep as reference if you get confused and program gets too messy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a specific changeset that I want to rollback my Development branch to,
I have a specific requirement that all children of a particular JComponent have double
I have a specific question, that could use a general answer... When building Multi-Tier
I have a specific Silverlight application, that is fed with data by a WCF-Service.
I have a specific case in mind, but the question applies in general too.
I have a specific DB2 query, and I would like to execute this query
I have some specific id like 1,2,5,11,64589 in solr (int type) I want to
I have a specific object with image data. And I want read/write images (in
I have a specific information in a table I am looking for, this query
I have a specific issue with general console printing and I was wondering whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.