Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 6th, 2006, 9:04 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
new thread for min heap

ok i am making a new thread for my min heap because the other one was getting out of control. So here is the deal. I have a tree implementation of a min heap. The insert and print work fine. I have to have a method that deletes the root and replaces it with the last value and then rebalances the tree to keep the minimum value as the root. So my delete method gives a huge number when you delete and it and puts a big value in for the root. I then have a method that purcolates down and swaps the values as needed. I want to focus on just deleting the right value right now and then i will worry about that. Does anyone know why it says the value being deleted is a very big number and why it puts that number as root? Also, if i just enter one number and then delete it it does not say the deleted value but it did delete it because when i print the heap there is nothing there. This program has to be done by midnight so any help would be greatly appreciated...
// add, print and delete a number from a binary min heap

#include <iostream>
using namespace std;

// node containing int value and left and right pointers
// constructor, get value, get right, get left, set value, set left, set right
class heapNode
{
  private:
  int value;

  public:
  heapNode *left;
  heapNode *right;

  //constructor
  heapNode(int newValue)
  {
     value = newValue;
     left = NULL; 
     right = NULL;
  }
 
  //get value
  int getValue()
  {
    return value;
  }
  
  //get right
  heapNode* getRight()
  {
    return right;
  }

  //get left
  heapNode* getLeft()
  {
    return left;
  }

  //set value
  void setValue(int newValue)
  {
    value = newValue;
  }

  // set right
  void setRight(heapNode *newRight)
  {
    right = newRight;
  }

  // set left
  void setLeft(heapNode *newLeft)
  {
    left = newLeft;
  }
  
}; // end of heapNode class

//insert print and delete methods for the min heap
class heap
{
  private:
  heapNode *root;
  int counter;

public:
  //constructor
  heap()
  {
    counter = 0;
    root = NULL;
  }

  //insert new value, check to see if swap is needed
  void realInsert(int ar[], int pos, heapNode *parent, heapNode *newNode)
  {
    if(ar[pos] == 0)
    {
      if (parent->getLeft() == NULL)
	parent->setLeft(newNode);
      else
	realInsert(ar, pos-1, parent->left, newNode);
      if(parent->getLeft()->getValue() < parent->getValue())
      {
	int tempParentLeft = parent->getLeft()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentLeft);
        parent->left->setValue(tempParent);
      }
    }
    else if(ar[pos] == 1)
    {
	if (parent->getRight() == NULL)
	  parent->setRight(newNode);
	else
	  realInsert(ar, pos-1, parent->right, newNode);
      if(parent->getRight()->getValue() < parent->getValue())
      {
        int tempParentRight = parent->getRight()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentRight);
        parent->right->setValue(tempParent);
      }
    }
  }

  // find the spot to put new value
  void insertSpot(int option)
  {
    heapNode *newNode = new heapNode(option);
    counter++;
    if(root == NULL)
    {
      root = newNode;
    }

    else
    {
      int arrayValue;
      bool isOne = false;
      int ar[32];
      int i;
      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar[i] = arrayValue;
      }
 
      for(i = 0; i < 32; i++)
        cout << ar[i] << " ";
      cout << endl;

      i=31;
      while(i >= 0)
      {
        if(ar[i] == 1)
          isOne = true;
        if(isOne == true)
          break;
        i--;
      }
      i--;
      cout << "Broke loop with i = " << i << endl;
      realInsert(ar, i, root, newNode);
    }
  }
   
  //call printNode
  void print()
  {
    cout << "Printing the heap...\n";
    printNode(root, 0);
  }
  
  //print tree with spaces for levels of the tree
  void printNode(heapNode *node, int level)
  {
    if(node != NULL)
    {
       printNode(node->getLeft(), level + 1);
      for(int i = 0; i < level; i++)
        cout << "\t";
      cout << node->getValue();
      cout << "\n";
      printNode(node->getRight(), level + 1);
     }
    }

  // get root
  heapNode* getRoot()
  {
    return root;
  }

  int realDelete(int ar[], int pos, heapNode *parent)
  {
    heapNode *tempNode;
    if(parent->getLeft() == NULL)
    {
      if (ar[pos] == 0)
      {
	tempNode = parent;
        parent = NULL;
      }
      else
	realDelete(ar, pos-1, parent->left);
    }
    else if(parent->getRight() == NULL)
    {
	if (ar[pos] == 1)
	{
	  tempNode = parent;
          parent = NULL;
        }
	else
	  realDelete(ar, pos-1, parent->right);
    }
    return tempNode->getValue();
    }


  /*//perculate down to reorganize tree
  void purculateDown(heapNode *parent)
  {
    if(parent->getValue() > parent->left->getValue())
    {
        int tempParentLeft = parent->getLeft()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentLeft);
        parent->left->setValue(tempParent);
    }
    else if(parent->getValue() > parent->right->getValue())
    {
       int tempParentRight = parent->getRight()->getValue();
       int tempParent = parent->getValue();
       parent->setValue(tempParentRight);
       parent->right->setValue(tempParent);
    }
    }*/ 

  //delete the root
  /*int realDelete(int ar[], int pos, heapNode *parent)
  {
    heapNode *tempNode = NULL;
    if(pos == 0)
    {
      if (ar[pos] == 0)
      {
        tempNode = parent;
        parent = NULL;
      }
       if(ar[pos} != 0)
        realDelete(ar, pos-1, parent->left);
      
       if(ar[pos] == 1)
      {
	 tempNode = parent;
         parent = NULL;
      }
       if(ar[pos] != 1)
       realDelete(ar, pos-1, parent->right);
    }

      return tempNode->getValue();
      }*/

  // find the the last spot for deletion
  void deleteSpot()
  {

    if (root == NULL) 
      cout << "Error.  The tree is empty.\n";
    else if(counter == 1)
      {
	root = NULL;
      }
    else
    {
      int arrayValue;
      bool isOne = false;
      int ar[32];
      int i;
      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar[i] = arrayValue;
      }
 
      for(i = 0; i < 32; i++)
        cout << ar[i] << " ";
      cout << endl;

      i=31;
      while(i >= 0)
      {
        if(ar[i] == 1)
          isOne = true;
        if(isOne == true)
          break;
        i--;
      }
      i--;
      cout << "Broke loop with i = " << i << endl;
      realDelete(ar, i, root);
      root->setValue(realDelete(ar, i, root));
      cout << "Deleted Value = " <<  root->getValue() << endl;
      //purculateDown(root);
      counter--;
    
    }
  }

  }; // end of heap class

// menu with add, print, delete choices  call methods from heap class
int main()
{
  int option;
  int value;
  heap *myHeap;
  myHeap = new heap();
 

  while(option != 4)
  {
    cout << "1. Add a value. \n"
         << "2. Print the heap. \n"
         << "3. Delete the root. \n"
         << "4. Quit. \n"
         << "Please make a selection: ";
    cin >> option;

    if(option == 1)
    {
      cout << "Please enter a value: ";
      cin >> value;
      myHeap->insertSpot(value);
    }

    if(option == 2)
      myHeap->print(); 

    if(option == 3)
      myHeap->deleteSpot();   
  }
  return 0;
}
cwl157 is offline   Reply With Quote
Old Apr 6th, 2006, 9:21 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Out of control? Seemed highly directed to your problem, to me, once The Dark determined what you were doing. Probably a record length, or course.
__________________
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 Apr 6th, 2006, 9:27 PM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
well i think i figured out why it was printing the big number. It was printing like the memory address or something of tempNode because i put a constructor in there and whatever that is thats the number i print. So how do i change it so tempNode is equal to the parent and then print the value of that? Also, i do not think it is actually deleting anything because i made the tempNode heapNode take the root value in its constructor and it just keeps shooting back that value but when you print the tree it looks the same. I do not know how to fix this problem of making tempNode equal to root and then i need to make root equal to parent ( which is the last node that needs to be deleted) and then i can purcolate down to fix the tree.
// add, print and delete a number from a binary min heap

#include <iostream>
using namespace std;

// node containing int value and left and right pointers
// constructor, get value, get right, get left, set value, set left, set right
class heapNode
{
  private:
  int value;

  public:
  heapNode *left;
  heapNode *right;

  //constructor
  heapNode(int newValue)
  {
     value = newValue;
     left = NULL; 
     right = NULL;
  }
 
  //get value
  int getValue()
  {
    return value;
  }
  
  //get right
  heapNode* getRight()
  {
    return right;
  }

  //get left
  heapNode* getLeft()
  {
    return left;
  }

  //set value
  void setValue(int newValue)
  {
    value = newValue;
  }

  // set right
  void setRight(heapNode *newRight)
  {
    right = newRight;
  }

  // set left
  void setLeft(heapNode *newLeft)
  {
    left = newLeft;
  }
  
}; // end of heapNode class

//insert print and delete methods for the min heap
class heap
{
  private:
  heapNode *root;
  int counter;

public:
  //constructor
  heap()
  {
    counter = 0;
    root = NULL;
  }

  //insert new value, check to see if swap is needed
  void realInsert(int ar[], int pos, heapNode *parent, heapNode *newNode)
  {
    if(ar[pos] == 0)
    {
      if (parent->getLeft() == NULL)
	parent->setLeft(newNode);
      else
	realInsert(ar, pos-1, parent->left, newNode);
      if(parent->getLeft()->getValue() < parent->getValue())
      {
	int tempParentLeft = parent->getLeft()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentLeft);
        parent->left->setValue(tempParent);
      }
    }
    else if(ar[pos] == 1)
    {
	if (parent->getRight() == NULL)
	  parent->setRight(newNode);
	else
	  realInsert(ar, pos-1, parent->right, newNode);
      if(parent->getRight()->getValue() < parent->getValue())
      {
        int tempParentRight = parent->getRight()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentRight);
        parent->right->setValue(tempParent);
      }
    }
  }

  // find the spot to put new value
  void insertSpot(int option)
  {
    heapNode *newNode = new heapNode(option);
    counter++;
    if(root == NULL)
    {
      root = newNode;
    }

    else
    {
      int arrayValue;
      bool isOne = false;
      int ar[32];
      int i;
      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar[i] = arrayValue;
      }
 
      for(i = 0; i < 32; i++)
        cout << ar[i] << " ";
      cout << endl;

      i=31;
      while(i >= 0)
      {
        if(ar[i] == 1)
          isOne = true;
        if(isOne == true)
          break;
        i--;
      }
      i--;
      cout << "Broke loop with i = " << i << endl;
      realInsert(ar, i, root, newNode);
    }
  }
   
  //call printNode
  void print()
  {
    cout << "Printing the heap...\n";
    printNode(root, 0);
  }
  
  //print tree with spaces for levels of the tree
  void printNode(heapNode *node, int level)
  {
    if(node != NULL)
    {
       printNode(node->getLeft(), level + 1);
      for(int i = 0; i < level; i++)
        cout << "\t";
      cout << node->getValue();
      cout << "\n";
      printNode(node->getRight(), level + 1);
     }
    }

  // get root
  heapNode* getRoot()
  {
    return root;
  }

  int realDelete(int ar[], int pos, heapNode *parent)
  {
    heapNode *tempNode = new heapNode(root->getValue());
    if(pos == 0)
    {
      if (ar[pos] == 0)
      {
	tempNode->setValue(parent->getValue());
        tempNode->setRight(parent->getRight());
        tempNode->setLeft(parent->getLeft());
        parent = NULL;
      }
      else
	realDelete(ar, pos-1, parent->left);
    }
    else if(pos == 0)
    {
	if (ar[pos] == 1)
	{
	  tempNode = parent;
          parent = NULL;
        }
	else
	  realDelete(ar, pos-1, parent->right);
    }
    return tempNode->getValue();
    }


  /*//perculate down to reorganize tree
  void purculateDown(heapNode *parent)
  {
    if(parent->getValue() > parent->left->getValue())
    {
        int tempParentLeft = parent->getLeft()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentLeft);
        parent->left->setValue(tempParent);
    }
    else if(parent->getValue() > parent->right->getValue())
    {
       int tempParentRight = parent->getRight()->getValue();
       int tempParent = parent->getValue();
       parent->setValue(tempParentRight);
       parent->right->setValue(tempParent);
    }
    }*/ 

  //delete the root
  /*int realDelete(int ar[], int pos, heapNode *parent)
  {
    heapNode *tempNode = NULL;
    if(pos == 0)
    {
      if (ar[pos] == 0)
      {
        tempNode = parent;
        parent = NULL;
      }
       if(ar[pos} != 0)
        realDelete(ar, pos-1, parent->left);
      
       if(ar[pos] == 1)
      {
	 tempNode = parent;
         parent = NULL;
      }
       if(ar[pos] != 1)
       realDelete(ar, pos-1, parent->right);
    }

      return tempNode->getValue();
      }*/

  // find the the last spot for deletion
  void deleteSpot()
  {

    if (root == NULL) 
      cout << "Error.  The tree is empty.\n";
    else if(counter == 1)
      {
	root = NULL;
      }
    else
    {
      int arrayValue;
      bool isOne = false;
      int ar[32];
      int i;
      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar[i] = arrayValue;
      }
 
      for(i = 0; i < 32; i++)
        cout << ar[i] << " ";
      cout << endl;

      i=31;
      while(i >= 0)
      {
        if(ar[i] == 1)
          isOne = true;
        if(isOne == true)
          break;
        i--;
      }
      i--;
      cout << "Broke loop with i = " << i << endl;
      realDelete(ar, i, root);
      root->setValue(realDelete(ar, i, root));
      cout << "Deleted Value = " <<  root->getValue() << endl;
      //purculateDown(root);
      counter--;
    
    }
  }

  }; // end of heap class

// menu with add, print, delete choices  call methods from heap class
int main()
{
  int option;
  int value;
  heap *myHeap;
  myHeap = new heap();
 

  while(option != 4)
  {
    cout << "1. Add a value. \n"
         << "2. Print the heap. \n"
         << "3. Delete the root. \n"
         << "4. Quit. \n"
         << "Please make a selection: ";
    cin >> option;

    if(option == 1)
    {
      cout << "Please enter a value: ";
      cin >> value;
      myHeap->insertSpot(value);
    }

    if(option == 2)
      myHeap->print(); 

    if(option == 3)
      myHeap->deleteSpot();   
  }
  return 0;
}
cwl157 is offline   Reply With Quote
Old Apr 6th, 2006, 9:28 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Wow, what a lot of posts. I'm having a look at it now. Just a couple of quick points:
1. You need to initialise option in your main, otherwise it might be randomly set to 4 on startup and your program will stop straight away.
2. You call realDelete twice in a row from deleteSpot, one of them should be removed (this might be causing your problems).
3. You dont seem to be actually calling delete on the node you are removing, which causes a memory leak.

I will have a look a bit further...
The Dark is offline   Reply With Quote
Old Apr 6th, 2006, 9:36 PM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
alright thanks sorry for all the posts on the other one i was just trying pretty much anything i could and then reupdating the post since i only have a few hours until this has to be done. But now i think i know what is going on just not sure how to fix it. The number it says is being deleted is always the root and it never actually deletes the last entry and changes the roots value to the last entry.
cwl157 is offline   Reply With Quote
Old Apr 6th, 2006, 9:44 PM   #6
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
here let me post the code i think its all working except nothing is actually changed in the tree for some reason. The last value should be gone and the root should have the value of the last entry but it does not seem too work. It does say the value deleted is the root though which is correct.
// add, print and delete a number from a binary min heap

#include <iostream>
using namespace std;

// node containing int value and left and right pointers
// constructor, get value, get right, get left, set value, set left, set right
class heapNode
{
  private:
  int value;

  public:
  heapNode *left;
  heapNode *right;

  //constructor
  heapNode(int newValue)
  {
     value = newValue;
     left = NULL; 
     right = NULL;
  }
 
  //get value
  int getValue()
  {
    return value;
  }
  
  //get right
  heapNode* getRight()
  {
    return right;
  }

  //get left
  heapNode* getLeft()
  {
    return left;
  }

  //set value
  void setValue(int newValue)
  {
    value = newValue;
  }

  // set right
  void setRight(heapNode *newRight)
  {
    right = newRight;
  }

  // set left
  void setLeft(heapNode *newLeft)
  {
    left = newLeft;
  }
  
}; // end of heapNode class

//insert print and delete methods for the min heap
class heap
{
  private:
  heapNode *root;
  int counter;

public:
  //constructor
  heap()
  {
    counter = 0;
    root = NULL;
  }

  //insert new value, check to see if swap is needed
  void realInsert(int ar[], int pos, heapNode *parent, heapNode *newNode)
  {
    if(ar[pos] == 0)
    {
      if (parent->getLeft() == NULL)
	parent->setLeft(newNode);
      else
	realInsert(ar, pos-1, parent->left, newNode);
      if(parent->getLeft()->getValue() < parent->getValue())
      {
	int tempParentLeft = parent->getLeft()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentLeft);
        parent->left->setValue(tempParent);
      }
    }
    else if(ar[pos] == 1)
    {
	if (parent->getRight() == NULL)
	  parent->setRight(newNode);
	else
	  realInsert(ar, pos-1, parent->right, newNode);
      if(parent->getRight()->getValue() < parent->getValue())
      {
        int tempParentRight = parent->getRight()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentRight);
        parent->right->setValue(tempParent);
      }
    }
  }

  // find the spot to put new value
  void insertSpot(int option)
  {
    heapNode *newNode = new heapNode(option);
    counter++;
    if(root == NULL)
    {
      root = newNode;
    }

    else
    {
      int arrayValue;
      bool isOne = false;
      int ar[32];
      int i;
      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar[i] = arrayValue;
      }
 
      for(i = 0; i < 32; i++)
        cout << ar[i] << " ";
      cout << endl;

      i=31;
      while(i >= 0)
      {
        if(ar[i] == 1)
          isOne = true;
        if(isOne == true)
          break;
        i--;
      }
      i--;
      cout << "Broke loop with i = " << i << endl;
      realInsert(ar, i, root, newNode);
    }
  }
   
  //call printNode
  void print()
  {
    cout << "Printing the heap...\n";
    printNode(root, 0);
  }
  
  //print tree with spaces for levels of the tree
  void printNode(heapNode *node, int level)
  {
    if(node != NULL)
    {
       printNode(node->getLeft(), level + 1);
      for(int i = 0; i < level; i++)
        cout << "\t";
      cout << node->getValue();
      cout << "\n";
      printNode(node->getRight(), level + 1);
     }
    }

  // get root
  heapNode* getRoot()
  {
    return root;
  }

  //deleting the last entry
  int realDelete(int ar[], int pos, heapNode *parent)
  {
    heapNode *tempNode = new heapNode(parent->getValue());
    if(pos == 0)
    {
      if (ar[pos] == 0)
      {
	tempNode->setValue(parent->getValue());
        tempNode->setRight(parent->getRight());
        tempNode->setLeft(parent->getLeft());
        parent = NULL;
      }
      else
	realDelete(ar, pos-1, parent->left);
    }
    else if(pos == 0)
    {
	if (ar[pos] == 1)
	{
	  tempNode->setValue(parent->getValue());
          tempNode->setRight(parent->getRight());
          tempNode->setLeft(parent->getLeft());
          parent = NULL;
          root->setValue(tempNode->getValue());
        }
	else
	  realDelete(ar, pos-1, parent->right);
    }
        return tempNode->getValue();
   }


  /*//perculate down to reorganize tree
  void purculateDown(heapNode *parent)
  {
    if(parent->getValue() > parent->left->getValue())
    {
        int tempParentLeft = parent->getLeft()->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempParentLeft);
        parent->left->setValue(tempParent);
    }
    else if(parent->getValue() > parent->right->getValue())
    {
       int tempParentRight = parent->getRight()->getValue();
       int tempParent = parent->getValue();
       parent->setValue(tempParentRight);
       parent->right->setValue(tempParent);
    }
    }*/ 

  // find the the last spot for deletion
  void deleteSpot()
  {

    if (root == NULL) 
      cout << "Error.  The tree is empty.\n";
    else if(counter == 1)
      {
	root = NULL;
      }
    else
    {
      int arrayValue;
      bool isOne = false;
      int ar[32];
      int i;
      int temp = counter;
      for(i = 0; i < 32; i++)
      {
          arrayValue = temp % 2;
          temp = temp / 2;
          ar[i] = arrayValue;
      }
 
      for(i = 0; i < 32; i++)
        cout << ar[i] << " ";
      cout << endl;

      i=31;
      while(i >= 0)
      {
        if(ar[i] == 1)
          isOne = true;
        if(isOne == true)
          break;
        i--;
      }
      i--;
      cout << "Broke loop with i = " << i << endl;
      //realDelete(ar, i, root);
      root->setValue(realDelete(ar, i, root));
      cout << "Deleted Value = " <<  root->getValue() << endl;
      //purculateDown(root);
      counter--;
    
    }
  }

  }; // end of heap class

// menu with add, print, delete choices  call methods from heap class
int main()
{
  int option = 0;
  int value;
  heap *myHeap;
  myHeap = new heap();
 

  while(option != 4)
  {
    cout << "1. Add a value. \n"
         << "2. Print the heap. \n"
         << "3. Delete the root. \n"
         << "4. Quit. \n"
         << "Please make a selection: ";
    cin >> option;

    if(option == 1)
    {
      cout << "Please enter a value: ";
      cin >> value;
      myHeap->insertSpot(value);
    }

    if(option == 2)
      myHeap->print(); 

    if(option == 3)
      myHeap->deleteSpot();   
  }
  return 0;
}
cwl157 is offline   Reply With Quote
Old Apr 6th, 2006, 9:53 PM   #7
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
ok i did some print statments and i do not think it ever gets into the if statement that checks if pos == 0 so that means pos never equals 0. The pos being equal to zero is supposed to be the check for if the last entry is reached. I was told that when the last entry was reached the pos would be zero. I guess thats not the case whats another check to know im at the last entry? Because none of the satements inside the if statement are being reached therefore its not changing any values. I tried changing it to say if(pos == 1) and then it gets into the if statment but i get a segmentation fault.
cwl157 is offline   Reply With Quote
Old Apr 6th, 2006, 9:58 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Here is how I got it to work (haven't looked at youyr last post yet).
In the realDelete function, you need to be looking at the ar array to determine whether to go left or right, not checking for NULL in the left and right values.
The following code only finds the last entry, swaps its value with the root node and deletes the node from the tree and from memory. It uses the fact that when you get to pos == 0, the "parent" parameter has the parent of the last child.
Note that I removed the return as it wasnt needed (see below).
  void realDelete(int ar[], int pos, heapNode *parent)
  {
    if (pos == 0)
    {
      // We have reached the parent of the node we want, swap it with the child with the root and remove the child
      heapNode *tempNode;
      if (ar[pos] == 0)
      {
        tempNode = parent->getLeft();
        parent->setLeft(NULL);
      }
      else
      {
        tempNode = parent->getRight();
        parent->setRight(NULL);
      }
      root->setValue(tempNode->getValue());
      delete tempNode;
    }
    else
    {
      if (ar[pos] == 0)
      	realDelete(ar, pos-1, parent->left);
      else
        realDelete(ar, pos-1, parent->right);
    }
  }

Once you have swapped the values and removed the node, you need to percolate the new root value down the tree. I used your existing function (fixed the spelling in the name), but had to rewrite it a bit as when you are percolating, you need to swap with the smaller child node, not just the first one you check against. It also needs to be recursive, so that the value will keep percolating down.
There is a lot of if tests in there as either or both of the left and right could be NULL. (technically I guess, the left can't be NULL if the right is not NULL, but it is clearer this way).
  //perculate down to reorganize tree
  void percolateDown(heapNode *parent)
  {
    // Find smaller child
    heapNode *smallerChild = NULL;
    if (parent->getLeft() != NULL)
    {
      if (parent->getRight() != NULL)
      {
        if (parent->getLeft()->getValue() <= parent->getRight()->getValue())
          smallerChild = parent->getLeft();
        else
          smallerChild = parent->getRight();
      }
      else
      {
        smallerChild = parent->getLeft();
      }
    }
    else if (parent->getRight() != NULL)
    {
      smallerChild = parent->getRight();
    }

    if (smallerChild != NULL)
    {
      if (smallerChild->getValue() < parent->getValue())
      {
        int tempChildVal = smallerChild->getValue();
        int tempParent = parent->getValue();
        parent->setValue(tempChildVal);
        smallerChild->setValue(tempParent);
        percolateDown(smallerChild);
      }
    }
  }

in deleteSpot, you want to delete the root node from memory as well:
    else if(counter == 1)
      {
        delete root;
        root = NULL;
      }

When you call realDelete, you need to then call perolateDown:
      cout << "Broke loop with i = " << i << endl;
      realDelete(ar, i, root);
      cout << "Deleted Value = " <<  root->getValue() << endl;
      print();
      percolateDown(root);
      counter--;
You can remove the print(), I just had it there for debugging.
The Dark is offline   Reply With Quote
Old Apr 6th, 2006, 10:04 PM   #9
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
thanks so much im going to try it now. I also need to print out the root being deleted thats why i had it return the int but if there is another way to do that i dont think it matters. O nevermind i see it now.
cwl157 is offline   Reply With Quote
Old Apr 6th, 2006, 10:11 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
When you are deleting the minimum value from a min heap, it is always the root. The rest of it is just rearranging the tree to set the root back to the miniumum.
So if you want to print the minimum value before the delete, just print the value of root->GetValue() before you call realDelete (check for a NULL root as well)
The Dark 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 1:26 AM.

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