Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 23rd, 2006, 4:32 PM   #11
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 329
Rep Power: 4 cwl157 is on a distinguished road
yea i know i have to compare but i want to get the movement thing right first because i guess its supposed to tell you where the next node is supposed to be added and then i'll worry about nodes and comparing and all that
cwl157 is offline   Reply With Quote
Old Mar 23rd, 2006, 4:35 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
hbe02, as a new member, you need to read the forum's rules/FAQ. Ever hear of code tags? They preserve your indentation and formatting and ease the task of reading by your fellow members. Dumping ugly trash is ruder than I am, and that's pretty rude. These are HTML pages, they eat white space. Again, to the OP, odd/even is not how you organize a min heap.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 23rd, 2006, 4:43 PM   #13
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 329
Rep Power: 4 cwl157 is on a distinguished road
Quote:
odd/even is not how you organize a min heap.
Yea i know but i guess the thing with 1 and 0 is to just keep track of where to add the next entry. And then once its added another method will compare the new node to the rest of the tree and swap accordingly?
cwl157 is offline   Reply With Quote
Old Mar 23rd, 2006, 4:47 PM   #14
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
/ Max-heap class
template <class Elem, class Comp> class maxheap {
private:
  Elem* Heap;          // Pointer to the heap array
  int size;            // Maximum size of the heap
  int n;               // Number of elements now in the heap
  void siftdown(int);  // Put element in its correct place
  void siftup(int);
public:
  maxheap(Elem* h, int num, int max)     // Constructor
    { Heap = h;  n = num;  size = max;  buildHeap(); }
  int heapsize() const       // Return current heap size
    { return n; }
  bool isLeaf(int pos) const // TRUE if pos is a leaf
    { return (pos >= n/2) && (pos < n); }
  int leftchild(int pos) const
    { return 2*pos + 1; }    // Return leftchild position
  int rightchild(int pos) const
    { return 2*pos + 2; }    // Return rightchild position
  int parent(int pos) const  // Return parent position
    { return (pos-1)/2; }
  bool insert(const Elem&);  // Insert value into heap
  bool removemax(Elem&);     // Remove maximum value
  bool remove(int, Elem&);   // Remove from given position
  void buildHeap()           // Heapify contents of Heap
    { for (int i=n/2-1; i>=0; i--) siftdown(i); }
  void swap(Elem *, int, int);
  int find (const Elem& E)   //returns position of an element..to find a duplicate with 
	 {						 // the highest propability and return its position
	  int pos;
		  for (int i = 0 ; i < n; i++)
				 { 
					  if ( E.ID == Heap[i].ID )
					  {
						  pos = i ;
						 for (int j = i ; j < n; j++)
							{
							  if ( E.ID == Heap[j].ID )
							  {
									if(Heap[j].p > Heap[pos].p)
									{	
										pos = j ;
										break;
									}
							  }
							}
						break;
					  }
				 }
		  return pos;
	 }
  bool exist (const Elem& E)  // checks if element exists in the heap.. if it doesnt after delete, then it should also be deleted from the tree
  {
	  for (int i = 0 ; i < n; i++)
		 {
		  if( E.ID == Heap[i].ID  )
		  {  return true ; }

		}
	  return false ;
	}
  void print()
	  {
		   if (n == 0)
		   {
			   cout<<"HEAP IS EMPTY"<<endl;
			}
		   else
		   {
		   cout<<"Heap is arranged by order of priority \n\t  <ID,Priority> \n"<<endl;
			 for (int i = 0 ; i < n; i++)
				 { 
					 cout<<Heap[i];
				 }
			  cout<<endl; 
		   }

	  }
};

template <class Elem, class Comp> // Utility function
void maxheap<Elem, Comp>::siftup(int pos) {
while ((pos!=0) &&
         (Comp::gt(Heap[pos], Heap[parent(pos)]))) {
    swap(Heap, pos, parent(pos));
   	pos = parent(pos);
	}
}

template <class Elem, class Comp> // Utility function
void maxheap<Elem, Comp>::siftdown(int pos) {
  while (!isLeaf(pos)) {     // Stop if pos is a leaf
    int j = leftchild(pos);  int rc = rightchild(pos);
    if ((rc < n) && Comp::lt(Heap[j], Heap[rc]))
	{	
		j = rc;        // Set j to greater child's value
	}
    if (!Comp::lt(Heap[pos], Heap[j])) return; // Done
    swap(Heap, pos, j);
    pos = j;         // Move down
  }
}

template <class Elem, class Comp> // Insert element
bool maxheap<Elem, Comp>::insert(const Elem& val) {
  if (n >= size) return false; // Heap is full
  int curr = n++;
  Heap[curr] = val; // Start at end of heap
    siftup(curr) ;
  
return true;
}

template <class Elem, class Comp> // Remove max value
bool maxheap<Elem, Comp>::removemax(Elem& it) {
  if (n == 0) return false; // Heap is empty
  swap(Heap, 0, --n);       // Swap max with last value
  if (n != 0) siftdown(0);  // Siftdown new root val
  it = Heap[n];             // Return deleted value
  return true;
}

// Remove value at specified position
template <class Elem, class Comp>
bool maxheap<Elem, Comp>::remove(int pos, Elem& it) {
  if ((pos < 0) || (pos >= n)) return false; // Bad pos
  swap(Heap, pos, --n);           // Swap with last value
  while ((pos != 0) &&
         (Comp::gt(Heap[pos], Heap[parent(pos)])))
    swap(Heap, pos, parent(pos)); // Push up if large key
  siftdown(pos);      // Push down if small key
  it = Heap[n];
  return true;
}

template <class Elem, class Comp>
void maxheap<Elem, Comp>::swap(Elem * heap, int x, int y) {

	Elem temp = heap[x];
		 heap[x]=heap[y];
		 heap[y]=temp;	 

}
hbe02 is offline   Reply With Quote
Old Mar 23rd, 2006, 10:18 PM   #15
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 329
Rep Power: 4 cwl157 is on a distinguished road
ok so i got it where i am able to stop the loop in time now i just need to figure out how to ignore the first 1 and any 0 before that.
Here is the code:
#include <iostream>
using namespace std;

void insertSpot(int option)
{

  
  int arrayValue;
  int ar[32];
  for(int i = 32; i > 0; i--)
  {
      arrayValue = option % 2;
      cout << "arrayValue = " << arrayValue << endl;
      //cout << "option = " << option << endl;
      option = option / 2;
      ar[i] = arrayValue;
      if(arrayValue == 0)
        cout << "move left\n";
      if(arrayValue == 1)
        cout << "move right \n";
      if(option == 0)
        break;
      //cout << i;
  }
}

int main()
{
  int option;
  cout << "Please enter a number: ";
  cin >> option;
  insertSpot(option);
  return 0;
}
cwl157 is offline   Reply With Quote
Old Mar 23rd, 2006, 11:52 PM   #16
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 329
Rep Power: 4 cwl157 is on a distinguished road
ok i think i am close but not officially there. I am trying to make it where when it detects the first 1 it changes a flag to true. Then the printing starts when the flag is true. However, right now i think the checking is going on for EVERY 1 and not just the FIRST 1. How do i make it so the checking only happens for the first 1 and then it prints it ignoring the first 1? Also, it does not work if the number entered ends in zero like the number 10 or 20. This is because the first one checked is not a 1 so it does not set it to true so it never prints anything. I do not know how to fix this problem Here is the code:
#include <iostream>
using namespace std;

void insertSpot(int option)
{
  int arrayValue;
  bool isOne = false;
  int ar[32];
  for(int i = 32; i > 0; i--)
  {
      arrayValue = option % 2;
      cout << "arrayValue = " << arrayValue << endl;
      cout << "option = " << option << endl;
      option = option / 2;
      ar[i] = arrayValue;
      if(arrayValue == 1)
        isOne = true;
    if(isOne)
    {
      if(arrayValue == 0)
      {  
         cout << "move left\n";
         //cout << "Inside if arrayValue = 0 and option is " << option << endl;
      }
      if(arrayValue == 1)
      {
         cout << "move right \n";
         //cout << "inside if arrayVallue = 1 and option is " << option << endl;
      }
      if(option == 0)
      {
         break;
      }
    }
  }
}

int main()
{
  int option;
  cout << "Please enter a number: ";
  cin >> option;
  insertSpot(option);
  return 0;
}

Last edited by cwl157; Mar 24th, 2006 at 12:04 AM.
cwl157 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:18 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC