Inserting items in a non birany tree
-
Dear All, I would be grateful if any of you guys could guide me on this problem which has been driving me nuts and i am about to give up. I have constructed a none binary tree (in which a node might have more than 2 children). Now i want to insert an item at a specific level (N). To do that, i would first need to check on the other nodes which belong to the same level (depth), if the item does not exist, only then the new item should be created. My problem is as follows: how do i go through the nodes which belong to level N (i.e node of the same depth) ? I would be grateful if someone can provide some pseudo code / algorithm on how to do that. Please bear in mind that this is a none binary tree. lets assume node is the root of the tree, the children are stored in an arraylist of nodes. public void check(Node node, int level){ }
llp00na
-
Dear All, I would be grateful if any of you guys could guide me on this problem which has been driving me nuts and i am about to give up. I have constructed a none binary tree (in which a node might have more than 2 children). Now i want to insert an item at a specific level (N). To do that, i would first need to check on the other nodes which belong to the same level (depth), if the item does not exist, only then the new item should be created. My problem is as follows: how do i go through the nodes which belong to level N (i.e node of the same depth) ? I would be grateful if someone can provide some pseudo code / algorithm on how to do that. Please bear in mind that this is a none binary tree. lets assume node is the root of the tree, the children are stored in an arraylist of nodes. public void check(Node node, int level){ }
llp00na
public bool check(Node node, int level){
bool canInsert;
if (node.Level == level)
{
// do some checking, return false if not ok to insert.
}
else
{
if (canInsert) // check to see can still insert
{
foreach (childNode in node) { call check(childNode, level); }
}
}}
-
Dear All, I would be grateful if any of you guys could guide me on this problem which has been driving me nuts and i am about to give up. I have constructed a none binary tree (in which a node might have more than 2 children). Now i want to insert an item at a specific level (N). To do that, i would first need to check on the other nodes which belong to the same level (depth), if the item does not exist, only then the new item should be created. My problem is as follows: how do i go through the nodes which belong to level N (i.e node of the same depth) ? I would be grateful if someone can provide some pseudo code / algorithm on how to do that. Please bear in mind that this is a none binary tree. lets assume node is the root of the tree, the children are stored in an arraylist of nodes. public void check(Node node, int level){ }
llp00na
llp00na wrote:
how do i go through the nodes which belong to level N (i.e node of the same depth) ?
BFS
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus