Navigating a tree structure

I need help finding and returning a "node" in a general tree structure. Each node can have more than 2 children so it's not a binary tree. I've been given the following code, this Element object has a list to contain its children, I create one element node pointer in main and using that I have to add and search for children. This is for a school project but I'm not looking for answers (an answer wouldn't hurt). Any advice on how to go about this problem would be much appreciated, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include <iostream>
#include <list>
#include <sstream>

using namespace std;

class Element
{
  private:
  list<Element*> children;
  char* _tag;
  int _value;
  // private methods
  public:
  // default constructor
  Element();
  // non-default constructors
  Element( char* name); // value is set to -99 if not given
  Element(char* name, char* value);
  // destructor, must recursively destruct its children
  // and release the memory allocated for _tag
  ~Element();
  // ostream operator ( pre-order traversal)
  friend ostream&  operator << (ostream& out, const Element& E);
  void display_xml(); // print out the tree in xml-- format
  void addChild( Element* child); // add a child
  // Find the first element such that _tag == tag
  // returns “this” pointer of this element
  Element* findTag( char* tag);
  char* getName();
  int getValue();
  void setName(char* name);
  void setValue( int value);
  int height(); //b return the height
  int size(); // return the size
  // other methods
};


this is my best attempt at a solution, it has obvious problems but I'm new to all of this and some explanation on a proper solution, or some sample code would be very helpful!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Element* Element::findTag(char* tag)
{
    list<Element*> temp = children;
    int s = temp.size();

    if(getName() == tag)
    {
        return this;
    }
    else
    {
        for(int i = 0; i < s; i++)
        {
            findTag((*temp.front()).getName());
            temp.pop_front();
        }
    }

}
uhm uh... char* ... cstrings ... the cstring library should help you! :D

I think there is no operator== for char* but there is a function that compares cstrings: strcmp (short for: string compare)
strcmp returns 0 if both strings are the same :)
see: http://www.cplusplus.com/reference/cstring/strcmp/

Now to your loop...
+1 for using recursion! :DD

But you never return anything (except this) :o
Also: findTag((*temp.front()).getName()); there are to many brackets :b
And findTag should be used like this: node.findTag(tag);
so you would like to search your children for that tag, right?
temp.front().findTag(tag); is the way to go! :D

Now you could make it like this:
- when nothing is found you return a null-pointer (NULL) (or "nothing")
- when something is found you return the first node that matches the tag

Now, let's get to work! :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstring>

// ...

Element* Element::findTag(char* tag)
{
    list<Element*> temp = children;
    int s = temp.size();

    if(strcmp(getName(), tag) == 0) // returns 0 if both are the same ;)
    {
        return this;
    }

    Element* node = NULL;
    for(int i = 0; i < s; i++)
    {
        node = temp.front().findTag(tag); // search the first node for that Tag
        if(node != NULL) // if a node is found return that node
            return node;

        temp.pop_front();
    }
    return NULL;

    // return nullptr; -> use this only if you are familiar with c++11 
}

Note: untested code!
Topic archived. No new replies allowed.