-
February 17th, 2013, 06:46 AM
#1
Heap class
Im new wiith handling trees, ive been trying to implement this heap class with just 2 memeber functions, Insert() and DeleteMin() using pointers but i dont know how to implement it keeping the complexity of logn for insert. amy ideas of how this should be implmented it? i know that u should add a new node to the last position in the tree and then bubble up swaping till i reach the position where the elemn must reside.
however, im hacving trouble putting that into code using pointers.
so far i have this.
//Define a node object
class Node {
public:
Node();
~Node();
private:
int data;
Node *parent;
Node *lchild;
Node *rchild;
friend class PointerHeap;
};
//Node methods
Node::Node() {}
Node::~Node() {}
//PointerHeap class
class PointerHeap
{
public:
PointerHeap()
{
sz = 0;
root = NULL;
}
void insert(int elem);
private:
int sz;
Node *root;
};
//this function is not completed having problems implementing it.
void PointerHeap::insert(int elem)
{
sz++;
int position = sz;
Node *temp;
temp->parent->data = elem;
temp->lchild = NULL;
temp->rchild = NULL;
if(root == NULL)root = temp;
if(position % 2 == 1)
{
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|