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

  • SEARCH
  • Home
  • 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 8493297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:57:56+00:00 2026-06-10T22:57:56+00:00

I’ve created a simple test program as follows (error checks are omitted). My Tree_Find()

  • 0

I’ve created a simple test program as follows (error checks are omitted).

My Tree_Find() and Node_Find() functions seem to work correctly but I’d like to know if there are more efficient ways to achieve the same thing.

Node.h

typedef struct _Node Node;

Node* Node_Create(int nTag);
void Node_Destroy(Node **ppNode);
void Node_Append(Node **ppParent, Node *pChild);
Node* Node_Find(Node *pNode, int nTag);

Tree.h

#include "Node.h"

typedef struct _Tree Tree;

Tree* Tree_Create(void);
void Tree_Destroy(Tree **ppTree);
void Tree_Init(Tree *pTree);
Node* Tree_Find(Tree *pTree, int nTag);

Node.c

#include "Node.h"

struct _Node
{
    int nTag;
    struct _Node *pFirstChild;
    struct _Node *pNextSibling;
};

Node* Node_Create(int nTag)
{
    Node *pNode = malloc(sizeof(*pNode));
    pNode->nTag = nTag;
    pNode->pFirstChild = NULL;
    pNode->pNextSibling = NULL;

    return pNode;
}

void Node_Destroy(Node **ppNode)
{
    Node *pNode = NULL;

    if (!ppNode)
        return;

    if ((pNode = *ppNode) == NULL)
        return;

    Node_Destroy(&(pNode->pFirstChild));
    Node_Destroy(&(pNode->pNextSibling));

    free(pNode);
    *ppNode = NULL;
}

void Node_Append(Node **ppParent, Node *pChild)
{
    Node *pLastChild = NULL;

    if (!(*ppParent))
        return;

    if (!((*ppParent)->pFirstChild))
    {
        (*ppParent)->pFirstChild = pChild;

        return;
    }

    pLastChild = (*ppParent)->pFirstChild;
    while (pLastChild->pNextSibling)
        pLastChild = pLastChild->pNextSibling;

    pLastChild->pNextSibling = pChild;
}

Node* Node_Find(Node *pNode, int nTag)
{
    Node *pNodeFound = NULL;

    if (!pNode)
        return NULL;

    if (pNode->nTag == nTag)
        return pNode;

    if ((pNodeFound = Node_Find(pNode->pFirstChild, nTag)) == NULL)
        pNodeFound = Node_Find(pNode->pNextSibling, nTag);

    return pNodeFound;
}

Tree.c

#include "Tree.h"

struct _Tree
{
    Node *pRoot;
};

Tree* Tree_Create(void)
{
    Tree *pTree = malloc(sizeof(*pTree));

    pTree->pRoot = NULL;

    return pTree;
}

void Tree_Destroy(Tree **ppTree)
{
    Tree *pTree = NULL;

    if (!ppTree)
        return;

    if ((pTree = *ppTree) == NULL)
        return;

    Node_Destroy(&(pTree->pRoot));

    free(pTree);
    *ppTree = NULL;
}

void Tree_Init(Tree *pTree)
{
    Node *p1 = Node_Create(1);
    Node *p2 = Node_Create(2);
    Node *p3 = Node_Create(3);
    Node *p4 = Node_Create(4);
    Node *p5 = Node_Create(5);

    Node_Append(&p1, p2);
    Node_Append(&p1, p3);
    Node_Append(&p3, p4);
    Node_Append(&p3, p5);

    pTree->pRoot = p1;
}

Node* Tree_Find(Tree *pTree, int nTag)
{
    if (!pTree)
        return NULL;

    return Node_Find(pTree->pRoot, nTag);
}

main.c

#include "Tree.h"

int main(void)
{
    Node *pNodeToFind = NULL;

    Tree *pTree = Tree_Create();

    Tree_Init(pTree);

    pNodeToFind = Tree_Find(pTree, 1);
    pNodeToFind = Tree_Find(pTree, 2);
    pNodeToFind = Tree_Find(pTree, 3);
    pNodeToFind = Tree_Find(pTree, 4);
    pNodeToFind = Tree_Find(pTree, 5);
    pNodeToFind = Tree_Find(pTree, 6); // Not found!

    Tree_Destroy(&pTree);

    return 0;
}
  • 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-10T22:57:58+00:00Added an answer on June 10, 2026 at 10:57 pm

    If your goal is to build and seach arbitrary trees with no order on keys, then you’ve done just about all you can do except one thing: Your append method will have O(n^2) run time where n is the number of children a single node might have. If you maintain a queue instead of a list, you can append in unit time. There is a nice trick for implementing a queue with only one pointer in the header rather than separate head and tail pointers. Make the child list circular and have the parent point to the tail of the circular list. Then parent->children is the tail element and parent->children->sibling is the head. With this setup, you can push onto the tail and both push and pop from the head of the list of children. The code for these ops can be quite elegant, and your append will be constant time.

    Alternately you can store children in an auto-resized array. There is a slight storage penalty (you need to store a pointer and child count per node), but random access to children can be handy in some cases.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is

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.