Kalisperinos
Kalisperinos

Reputation: 11

Traverse NodeTree with html tags

I have created a tree node,which holds html tags inside and I want to traverse the tree and do some actions in each case to appear things in JTextPane.But I am having trouble to traverse the tree in order to keep the hierarchy.For example i have "h1" tag and "p" tag as its parent.I want to traverse the tree and appear the "h1" tag and then put it in a paragraph.Any idea how I can do that? I hope i was clear... Check Photo Here

I think i need something like this.

Upvotes: 0

Views: 214

Answers (2)

Ferit
Ferit

Reputation: 9647

You should use depth-first search. A good tutorial: http://www.geeksforgeeks.org/depth-first-traversal-for-a-graph/

Upvotes: 1

Anand Balakrishnan
Anand Balakrishnan

Reputation: 21

You would need each node in the tree to have a reference to its parent to be able to do a traversal up the tree.

EDIT: You can call a recursive function on the 'p' tag, and do a post-order iteration on each child, exclude the root and add each element encountered to the parent tag. Assuming that I'm understanding what you are saying correctly, that is.

Upvotes: 0

Related Questions