Jump to content

# levelOrder em Array

## Recommended Posts

Boas!

Eu tenho de implementar o levelOrder num array, eu já fiz isto..

```public void levelOrder(int node, ArrayUnorderedList<T> tempList) throws Exception {
QueueADT<T> nodes = new LinkedQueue<T>();

T elementRemoved = null;
nodes.enqueue(tree[node]);
while (!nodes.isEmpty()) {
elementRemoved = nodes.dequeue();
if (elementRemoved != null) {
tempList.addToRear(elementRemoved);
if (tree[node * 2 + 1] != null && node * 2 + 1 < tree.length) {
nodes.enqueue(tree[node * 2 + 1]);
}
if (tree[(node + 1) * 2] != null && (node + 1) * 2 < tree.length) {
nodes.enqueue(tree[(node + 1) * 2]);
} else {
tempList.addToRear(null);
}
}
}

}
```

Mas entra em ciclo infinito e ainda não descobri porque?

#### Share this post

##### Share on other sites

Já não entra em ciclo infinito, mas não esta a devolver os elementos todos da árvore.

```public void levelOrder(int node, ArrayUnorderedList<T> tempList) throws Exception {
QueueADT<T> nodes = new LinkedQueue<T>();
T elementRemoved = null;
nodes.enqueue(tree[node]);
while (!nodes.isEmpty()) {
elementRemoved = nodes.dequeue();
if (elementRemoved != null) {
tempList.addToRear(elementRemoved);
if (tree[node * 2 + 1] != null && node * 2 + 1 < tree.length) {
nodes.enqueue(tree[node * 2 + 1]);

}
if (tree[(node + 1) * 2] != null && (node + 1) * 2 < tree.length) {
nodes.enqueue(tree[(node + 1) * 2]);

}

} else {
tempList.addToRear(null);
}
node++;
}
}
```

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now

×

• #### Revista PROGRAMAR

• Wiki
• IRC
×
• Create New...

## Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.