An algorithm for a hierarchy
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: An algorithm for a hierarchy

  1. #1
    Join Date
    Sep 2009
    Posts
    14

    Post An algorithm for a hierarchy

    I have to create an algorithm for a hierarchy. Objects that compose that hierchy has 1 ID, 1 parent and as many childs as they want. Those childs can also have childs of their own, but still have only 1 parent, and so on. Through the use of this algorithm i have to remove from the hierchy 1 object, all of its childs and subchilds (if he has childs) and inform his parent that his gone. Each object has a "std::container<unsigned int> Obj" containing the IDs of the objects he has relations with. The first member of the Obj is the ID of the parent object, and the rest of the members are its childs. The IDs are unsigned ints, with a value unknown to you, but they all are unique (there is no such thing as 2 or more IDs with the same value), also all objects have their own ID as "unsigned int ID" and the objects can be accesed with the "std::container<Object*> Objs", also there are no private members inside Object class. A "std::container" is a "std::vector" with 1 more functions: "remove(void *dat)" that removes the first member found in the "std::vector" with the value equal with (const Ty&)dat, the function was tested and works without problems. The code below doesn't work, it was my first try... Can anyone please help me with this algorithm?

    Code:
    for (unsigned int iter=1; iter<Obj.size(); ++iter) {
    	unsigned int pos=0;
    	while (pos<Objs.size()) {
    		if (Objs.at(pos)->ID == Obj.at(iter)) {
    			Objs.at(pos)->Obj.erase(Objs.at(pos)->Obj.begin());
    			break;
    		}
    		else ++pos;
    	}
    }
    for (unsigned int iter=1; iter<Obj.size();) {
    	unsigned int pos=0;
    	while (pos<Objs.size()) {
    		if (Objs.at(pos)->ID == Obj.at(1)) {
    			Objs.at(pos)->~Cls2();
    			Obj.erase(Objs.at(pos)->Obj.begin()+1);
    			break;
    		}
    		else ++pos;
    	}
    }
    for (unsigned int iter=0; iter<Objs.size();) {
    	if (Objs.at(iter)->ID == Obj.at(0)) {
    		Object *t = Objs.at(iter);
    		for (unsigned int inter=1; inter<t->Obj.size();) {
    			if (t->Obj.at(inter) == ID) {
    				Objs.at(iter)->Obj.erase(t->Obj.begin()+inter);
    				break;
    			}
    			else ++inter;
    		}
    	}
    	else ++iter;
    }
    Thanks in advance...
    Last edited by BigHammer; October 20th, 2009 at 11:12 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center