Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

AJBM

[Resolvido] levelOrder

Recommended Posts

AJBM

Boas!

Eu tenho que implementar este algoritmo

Create a queue called nodes

Create an unordered list called results

Enqueue the root onto the nodes queue

While the nodes queue is not empty {

Dequeue the first element from the queue

If that element is not null

! ! Add that element to the rear of the results list

Enqueue the children of the element on the nodes queue

Else

Add null on the result list

}

Return an iterator for the result list

public Iterator<T> levelorder() {
	ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
	QueueADT<T> queue = new LinkedQueue<T>();
	BinaryTreeNode<T> node = null;
	queue.enqueue(root.element);

	while (!queue.isEmpty()) {
		node.setElement(queue.dequeue());


		if (node != null) {
  tempList.addToRear(node.element);
			if (node.left != null) {
				queue.enqueue(node.left.element);
			}
			if (node.right != null) {
				queue.enqueue(node.right.element);
			}
		} else {
			tempList.addToRear(null);
		}
	}
	return tempList.iterator();
}

So que da-me este erro

Exception in thread "main" java.lang.NullPointerException

at estruturas_auxiliares.LinkedQueue.dequeue(LinkedQueue.java:132)

at ficha10.LinkedBinaryTree.levelorder(LinkedBinaryTree.java:202)

at estudar_exame.Estudar_Exame.main(Estudar_Exame.java:34)

Java Result: 1

Share this post


Link to post
Share on other sites
HappyHippyHippo

que linha é esta : at estudar_exame.Estudar_Exame.main(Estudar_Exame.java:34)


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
HappyHippyHippo

anda p'rai demasiado código para se poder responder só com essa informação

mas fundamentalmente o problema encontrasse aqui : at estruturas_auxiliares.LinkedQueue.dequeue(LinkedQueue.java:132)


IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites

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

×

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.