The Artima Developer Community
Sponsored Link

Java Answers Forum
Recursive function help.

2 replies on 1 page. Most recent reply: Jun 2, 2003 6:39 AM by Tiago Antao

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Recursive function help. Posted: Jun 1, 2003 10:34 PM
Reply to this message Reply
Advertisement
This is supposed to remove the values from the Hashtable from smallest to largest and push them onto the stack.

Here it is:

/****************************************************
	 Name: insertIntoQueue()
	 Parameters: HashTable ht
	 Declared variables: Stack queue, Iterator i, String temp
	 	Set s, Integer currentLow, currentItem.
	 Return value: void
	 Purpose: Takes a HashTable argument, remove the frequencies and 
	 	insert them into a priority queue;
	 
	 Algorithm:
	 	
	 	insertIntoQueue(hashTable h)
	 		Create a new queue, a new keySet for the items in the table
	 			and attach an iterator to the set.
	 			temp=first item.
	 			currentLow=temp
	 			
	 		while (not at the end)
	 			if currentLow is greaterthan temp(currentItem)
	 				currentLow=temp(currentItem);
	 				currentFlag=temp;
	 		end while
	 		remove currentFlag
	 		push value of currentFlag onto the Stack.
	 		recurse.
	 	end.
	*****************************************************/
	public void insertIntoQueue(Hashtable ht){
		
		//Function variables.
		queue=new Stack();
		Set s=ht.keySet();
		Iterator i=s.iterator();
		String temp, ci="";
		Integer currentLow;
		
		temp=(String)i.next();//Equals the current item we are looking at.
		currentLow=((Integer)ht.get(temp));
		
		while(i.hasNext()){
			
			temp = (String)i.next();//Updates the currentItem.			
			if(currentLow.intValue() >= ((Integer)ht.get(temp)).intValue()){
				currentLow=((Integer)ht.get(temp));	
				ci=temp;
			}
				
		}
		
		queue.push(currentLow);//Push it onto the Stack.
		ht.remove(ci);//Remove it from the Hashtable.
		
		insertIntoQueue(ht);//Recurse.
	}


Rahul

Posts: 52
Nickname: wildhorse
Registered: Oct, 2002

Re: Recursive function help. Posted: Jun 2, 2003 1:39 AM
Reply to this message Reply
What is the problem ?

If you are getting an exception when trying to call insertIntoQueue recursively, try checking the size of the hashtable before calling the method again. You might be getting a null pointer exception, i think.

Tiago Antao

Posts: 26
Nickname: tiago
Registered: Feb, 2003

Re: Recursive function help. Posted: Jun 2, 2003 6:39 AM
Reply to this message Reply
A recursive function has to have a base condition from which to return, your code is in an infinite loop (there is no return condition).

like

if (rt.isEmpty()) {
return;
}

Flat View: This topic has 2 replies on 1 page
Topic: implementing a huffman tree Previous Topic   Next Topic Topic: Applet not loaded in Windows 2000

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use