- From: Saied Tazari <Saied.Tazari@zgdv.de>
- Date: Tue, 08 May 2001 18:25:54 +0200
- To: Chris Cera <cera@drexel.edu>
- CC: www-rdf-interest@w3.org
- Message-ID: <3AF81E12.1030409@zgdv.de>
Chris Cera wrote: > I'm creating a program to read a rdfs file and say I get the > predicate: http://www.w3.org/2000/01/rdf-schema#subClassOf, and I > want to create two local objects in another language that handle a > class relationship. I basically want to create a Tree structure > of the class hiearchy defined in an rdf file. > > First I want to create an rdfsclass object, then link it to its > superclass. If the superclass wasn't already created, then I need > to create that. My problem occurs in finding out if my local > superclass object has been created already, I then need to > traverse through all the class objects that I've created to see if > this one exists yet. This process is to create a pointer to the > superclass object. I can't create a pointer in this programming > language if the other class doesn't exist yet, so searching of all > my class objects up until that point will need to be done to link > them properly. > > Since I'm trying to create a tree of the class hiearchy, I need the > root nodes to come first, this way I can assume it exists and have > no need to search another structure to see if I've created it > already. > > Am I oblivious to something else or is this a common problem? If > anyone has a proposed solution I would appreciate it if they could > please share it with me. Thanks. > Hi, attached you'll find a simple Java-solution for this problem. Hope, it's what you need. Regards, -- Saied Tazari
/**************************************************************************/ /* PrintTree.java */ /* */ /* Program to accept nodes of a tree in an arbitrary order and internally */ /* build and output the tree in the correct order. */ /* */ /* Description of the solution: */ /* */ /* A binary tree structure (using firstChild and sibling pointers) will */ /* hold the infos representing the nodes. Class "Node" is used to define*/ /* the structure and behavior of these nodes. */ /* */ /* Because of the arbitrary order of the nodes during the "add" */ /* operations, there would be some nodes that are temporarily "floating"*/ /* (i.e. their parents are missing). The idea is to think of the subtree*/ /* rooted at such a node, as an independent tree and each time that a */ /* new node is added, see if the "root" of one of the existing trees can*/ /* be displaced as a child of the new node. */ /* */ /* All the trees are kept in a single list. Class "TreeList" is used */ /* to define the structure and behavior of such a list. The data */ /* structure of the list is chosen to be a "Node" named "head" with id */ /* equal to 0, so that via "sibling" we have the list view and */ /* via "firstChild" we have a tree view, i.e. head is simultaneously */ /* both the head of a list and the super-root having the roots of all */ /* of the (tempoprary) trees as its children. This way, the code for */ /* TreeList::add has become very short. */ /**************************************************************************/ class Node { int id, pid; StringBuffer info; Node firstChild, sibling; Node (int id, String info, int pid) { this.id = id; this.info = new StringBuffer (); if (info != null && info.length () > 0) { this.info.append (info); } this.pid = pid; firstChild = null; sibling = null; } void addChild (Node child) { if (firstChild == null) { firstChild = child; } else { Node tempChild = firstChild; while (tempChild.sibling != null) { tempChild = tempChild.sibling; } tempChild.sibling = child; } } Node findNode (int id) { if (this.id == id) { return this; } if (firstChild != null) { Node temp = firstChild.findNode (id); if (temp != null) { return temp; } } if (sibling != null) { return (sibling.findNode (id)); } return null; } void printTree (String indent) { System.out.println (indent + info.toString ()); if (firstChild != null) { StringBuffer newIndent = new StringBuffer (); newIndent.append (indent + " "); firstChild.printTree (newIndent.toString ()); } if (sibling != null) { sibling.printTree (indent); } } } class TreeList { Node head; TreeList () { head = new Node (0, "List head", 0); } void addNode (int id, String info, int pid) { Node temp = head.findNode (id); if (temp != null) { System.out.println ("Duplicate id. Node ignored!"); return; } Node node = new Node (id, info, pid); temp = head.findNode (pid); if (temp == null || pid == 0) { head.addChild (node); head.sibling = head.firstChild; } else { temp.addChild (node); } Node listMember = head; while (listMember.sibling != null) { if (listMember.sibling.pid == id) { temp = listMember.sibling; listMember.sibling = temp.sibling; temp.sibling = null; node.addChild (temp); } else { listMember = listMember.sibling; } } } void printTrees () { int i=1; for (Node root=head.sibling; root!=null; root=root.sibling) { System.out.println ("Tree #" + i++ + ":"); root.printTree (" "); } } } public class PrintTree { public static void main(String[] args) { TreeList myList = new TreeList (); myList.addNode (23, "Tree1.Node23.ChildOf20", 20); myList.addNode (3, "Tree1.Node3.ChildOf1", 1); myList.addNode (18, "Tree1.Node18.ChildOf10", 10); myList.addNode (12, "Tree1.Node12.ChildOf5", 5); myList.addNode (7, "Tree1.Node7.ChildOf3", 3); myList.addNode (8, "Tree1.Node8.ChildOf3", 3); myList.addNode (9, "Tree1.Node9.ChildOf3", 3); myList.addNode (1, "Tree1.Node1", 0); myList.addNode (10, "Tree1.Node10.ChildOf5", 5); myList.addNode (4, "Tree1.Node4.ChildOf1", 1); myList.addNode (11, "Tree1.Node11.ChildOf5", 5); myList.addNode (2, "Tree1.Node2.ChildOf1", 1); myList.addNode (13, "Tree1.Node13.ChildOf6", 6); myList.addNode (14, "Tree1.Node14.ChildOf7", 7); myList.addNode (15, "Tree1.Node15.ChildOf8", 8); myList.addNode (16, "Tree1.Node16.ChildOf8", 8); myList.addNode (6, "Tree1.Node6.ChildOf2", 2); myList.addNode (19, "Tree1.Node19.ChildOf10", 10); myList.addNode (20, "Tree1.Node20.ChildOf10", 10); myList.addNode (21, "Tree1.Node21.ChildOf20", 20); myList.addNode (17, "Tree1.Node17.ChildOf10", 10); myList.addNode (22, "Tree1.Node22.ChildOf20", 20); myList.addNode (5, "Tree1.Node5.ChildOf2", 2); myList.printTrees (); } }
Received on Tuesday, 8 May 2001 12:27:14 UTC